BaseAdapter OnItemClickListener never called

2019-03-06 03:04发布

问题:

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

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

Am I right?

回答1:

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


回答2:

With custom adapter OnItemClickListener does not work. You must register onClickListener on view in getView method.

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());
        row.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // do your stuff
        }
    });


        return row;
    }