BaseAdapter OnItemClickListener never called

后端 未结 2 1585
一整个雨季
一整个雨季 2021-01-28 20:49

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

The problem is the onItem

相关标签:
2条回答
  • 2021-01-28 21:08

    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.
    0 讨论(0)
  • 2021-01-28 21:08

    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;
        }
    
    0 讨论(0)
提交回复
热议问题