在项目上单击侦听器以扩展ArrayAdapter的列表视图 [英] On Item Click Listener for List View extending ArrayAdapter

查看:61
本文介绍了在项目上单击侦听器以扩展ArrayAdapter的列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过的事情:

public class EntryAdapter extends ArrayAdapter<Item> {

private Context context;
private ArrayList<Item> items;
private LayoutInflater vi;

public EntryAdapter(Context context,ArrayList<Item> items) {
    super(context,0, items);
    this.context = context;
    this.items = items;
    vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(int position, final View convertView, ViewGroup parent) {

    // // // //  NON-FUNCTIONING CODE BELOW 

    AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent intent = new Intent(getContext(), NewsItemActivity.class);
            convertView.getContext().startActivity(intent);

        }
    };
}

AdapterView.onItemClickListener 不会产生任何错误,但似乎没有任何作用.

The AdapterView.onItemClickListener doesnt yield any errors but doesnt seem to function whatsoever.

设置此onClick侦听器的正确方法是什么?

What is the proper way of setting this onClick Listener?

注意:由于我自己的原因,我必须在此适配器类中进行设置,而不是在主类中进行设置.

Note: I have to set it in this adapter class, not the main class for my own reasons.

推荐答案

除非您在行布局内的特定视图上设置了侦听器,否则您不应在 getView()内实现侦听器

You shuldn't implement a listener inside the getView() unless you what set a listener on a particular view inside your row layout.

您应该在 ListView 上使用 setOnItemClickListener()方法:

ListView lv = (ListView) findViewById(R.id.your_listview);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
         Intent intent = new Intent(context, NewsItemActivity.class);
         context.startActivity(intent);
     } 
});

编辑:

如果对于onclick内的每个操作,您都需要驻留在对象(项目)中的信息,则可以通过以下方式获取它:

If, for each action inside the onclick, you need information that resides in your Objects (Item) then you can get it in this way:

Item item = (Item)listview.getAdapter().getItem(position);

onItemClick()方法内部

这篇关于在项目上单击侦听器以扩展ArrayAdapter的列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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