从未调用过BaseAdapter OnItemClickListener [英] BaseAdapter OnItemClickListener never called

查看:144
本文介绍了从未调用过BaseAdapter OnItemClickListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 ListView 我有一个自定义的 BaseAdapter ,其中我实现了 AdapterView.OnItemClickListener

I have a custom BaseAdapter for my ListView in which i implements AdapterView.OnItemClickListener.

问题是 onItemClick(AdapterView<?>,View,int,long)方法。我想我需要在我的主活动中实现接口而不是我的 ListView 自定义适配器。

The problem is the onItemClick(AdapterView<?>, View, int, long) method is never called. I guess I need to implements that interface in my main Activity and not in my ListView custom adapter.

MyListViewAdapter.java

public class MyListViewAdapter extends BaseAdapter implements AdapterView.OnItemClickListener
{
    private Context context;
    private ArrayList<MyListViewRow> rowsList = new ArrayList<MyListViewRow>(); // All one row items

    private TextView a;


    public MyListViewAdapter(Context context,ArrayList<MyListViewRow> rowsList)
    {
        this.context = context;
        this.rowsList = rowsList;
    }

    @Override
    public int getCount()
    {
        return this.rowsList.size();
    }

    @Override
    public Object getItem(int position)
    {
        return this.rowsList.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    /**
     * Returns a new row to display in the list view.
     *
     * Position - position of the current row.
     *
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        /**
         * Inflating the root view and all his children and set them to a View Object.
         *
         */
        View row = layoutInflater.inflate(R.layout.list_view_row,null);

        // Get all the views in my row
        this.a = (TextView) row.findViewById(R.id.a_id;


        MyListViewRow myListViewRow = rowsList.get(position);

        // Set values to all the views in my row
        this.a.setText(myListViewRow.getA());



        return row;
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        Toast.makeText(context, "onItemClick LV Adapter called", Toast.LENGTH_LONG).show();

    }


} // End of MyListViewAdapter class

我是对的吗?

推荐答案

为什么在适配器内部有OnItemClickListener?在适配器内部有OnClickListener,或者最佳做法是将OnItemClickListener设置为ListView或您在activity / fragment中使用的任何AdapterView。
你现在这样做的方式,由于以下几个原因不起作用:

Why would you have an OnItemClickListener inside the Adapter?! Either you have OnClickListener inside the Adapter, or the best practice is to set OnItemClickListener to the ListView or whatever AdapterView you use in your activity/fragment. The way you are doing it right now, it won't work for several reasons:


  1. 你没有设置听取视图。

  2. 不能将OnItemClickListener设置为TextView。

  3. 将onClickListener设置为TextView里面适配器类,意味着您必须单击TextView本身,否则,将调用侦听器

  1. You don't set the listener to the view.
  2. You CAN NOT set an OnItemClickListener to a TextView.
  3. Setting onClickListener to the TextView inside the adapter class, means that you will have to click the TextView itself, otherwise, the listener WILL NOT be invoked.

这篇关于从未调用过BaseAdapter OnItemClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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