Android 如何让ListView的Adapter中的OnClick调用Activity的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10072495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:23:05  来源:igfitidea点击:

How to let OnClick in ListView's Adapter call Activity's function

androidonclickandroid-listviewcustom-adapter

提问by user1321683

I have an Android application with a ListView in it, the ListView will setup fine but now I want a image in the ListView to be clickable. I do this by using 2 classes, the Activity class (parent) and an ArrayAdapter to fill the list. In the ArrayAdapter I implement a OnClickListener for the image in the list that I want to be clickable.

我有一个带有 ListView 的 Android 应用程序,ListView 将设置得很好,但现在我希望 ListView 中的图像可点击。我通过使用 2 个类来完成此操作,Activity 类(父类)和一个 ArrayAdapter 来填充列表。在 ArrayAdapter 中,我为列表中我想要可点击的图像实现了一个 OnClickListener。

So far it all works.

到目前为止,一切正常。

But now I want to run a function from the activity class when the onClick, for the image in the list, is run but I do not know how. Below are the 2 classes that I use.

但是现在我想在运行列表中图像的 onClick 时从活动类运行一个函数,但我不知道如何运行。下面是我使用的 2 个类。

First the Activity class:

首先是Activity类:

public class parent_class extends Activity implements OnClickListener, OnItemClickListener
{
    child_class_list myList;
    ListView myListView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // setup the Homelist data
        myList     = new child_class_list (this, Group_Names, Group_Dates);
        myListView = (ListView) findViewById(R.id.list);

        // set the HomeList
        myListView.setAdapter( myList );
        myListView.setOnItemClickListener(this);
    }

    void function_to_run()
    {
        // I want to run this function from the LiscView Onclick
    }

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
    {
        // do something
    }
}

And the ArrayAdapter from where I want to call a function from the Activity class:

以及我想从 Activity 类调用函数的 ArrayAdapter:

public class child_class_list extends ArrayAdapter<String>
{
    // private
    private final Context context;
    private String[]        mName;
    private String[]        mDate;

    public child_class_list (Context context, String[] Name, String[] Date) 
    {
        super(context, R.layout.l_home, GroupName);
        this.context        = context;
        this.mName      = Name;
        this.mDate  = Date;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.l_home, parent, false);

        ImageView selectable_image = (ImageView) rowView.findViewById(R.id.l_selectable_image);
        selectable_image.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v) 
            {
                // I want to run the function_to_run() function from the parant class here
            }
        }
        );

        // get the textID's
        TextView tvName = (TextView) rowView.findViewById(R.id.l_name);
        TextView tvDate = (TextView) rowView.findViewById(R.id.l_date);

        // set the text
        tvName.setText      (mName[position]);
        tvDate.setText  (mDate[position]);

        return rowView;
    }
}

If anyone knows how to run the function in the activity class from the arrayadapter or how to set the image onClickListener in the Activity Class I would greatly apriciate the help.

如果有人知道如何从 arrayadapter 运行活动类中的函数或如何在活动类中设置图像 onClickListener,我会非常感谢帮助。

回答by Adil Soomro

Inside onClick()Do something like this:

在里面onClick()做这样的事情:

((ParentClass) context).functionToRun();

回答by jamesc

Just for clarity to expand on provided answers In a BaseAdapter you can get the parent class by calling this.getActivity();If you then typecast this to the actual activity class you can then call a function as per @AdilSoomro answer below so you actually end up with something like this

只是为了清楚地扩展所提供的答案在 BaseAdapter 中,您可以通过调用来获取父类this.getActivity();如果然后将其类型转换为实际的活动类,则可以按照下面的@AdilSoomro 答案调用一个函数,这样您实际上最终会得到这样的结果

public class MyAdapter extends BaseAdapter<Long> {
    public MyAdapter(Activity activity,
            TreeStateManager<Long> treeStateManager, int numberOfLevels) {
        super(activity, treeStateManager, numberOfLevels);
    }

    @Override
    public void handleItemClick(final View view, final Object id) {
        ((MyActivity) this.activity).someFunction();
    }
}

Then just declare someFunction in MyActivity to do what you want

然后只需在 MyActivity 中声明 someFunction 即可执行您想要的操作

    protected void someFunction(){
//    Do something here
    }