如何让的OnClick在ListView的适配器呼叫活动的功能 [英] How to let OnClick in ListView's Adapter call Activity's function

查看:130
本文介绍了如何让的OnClick在ListView的适配器呼叫活动的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序与它一个ListView,在ListView将设置很好,但现在我想在ListView一个图像点击。我这样做是通过使用2类,活动类(父)和ArrayAdapter填充列表。在一个ArrayAdapter我实现一个OnClickListener的,我想可以点击列表中的图像。

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.

到目前为止,所有的作品。

So far it all works.

但现在我想从活动类的onClick,在列表中的图像,运行时运行的功能,但我不知道怎么办。下面是我用的是2类。

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.

首先Activity类:

First the Activity class:

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
    }
}

和从那里我想调用从活动类的函数一个ArrayAdapter:

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

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;
    }
}

如果有人知道如何在活动类从一个ArrayAdapter运行功能或如何设置图像onClickListener在活动课,我将非常apriciate的帮助。

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.

推荐答案

的onClick()做这样的事情:

((ParentClass) context).functionToRun();

这篇关于如何让的OnClick在ListView的适配器呼叫活动的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆