ListView setOnItemClickListener not working in custom list view

前端 未结 3 823
感动是毒
感动是毒 2021-01-05 14:15

I have a list view with two text view and one edit text in each row , list view setOnItemClickListener() is not working.

Here My Java Code.

相关标签:
3条回答
  • 2021-01-05 14:15

    This custom adapter is for you here you can achieve tha both onclick and on item click listener also....Here i used the custom listener which is used to pass the object of the selected item..That's all If you have any query comment...All the best

    public class CustomAdapter extends ArrayAdapter<Sample> {
    
    public ArrayList<Sample> mlist;
    public Context context;
    public LayoutInflater inflater;
    
    public CustomAdapter(Context context, int resource, ArrayList<Sample> mlist) {
        super(context, resource);
        this.mlist = mlist;
        this.context = context;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
    @Override
    public int getPosition(Sample item) {
        return super.getPosition(item);
    }
    
    @Override
    public Sample getItem(int position) {
        return mlist.get(position);
    }
    
    @Override
    public int getCount() {
        return mlist.size();
    }
    
    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = inflater.inflate(R.layout.listitem, null);
        LinearLayout layout = (LinearLayout)convertView.findViewById(R.id.linearlayoutSample);;
        TextView text1 = (TextView) convertView.findViewById(R.id.item1);
        TextView text2 = (TextView) convertView.findViewById(R.id.item2);
        layout.setBackgroundColor(Color.GREEN);
        text1.setText(mlist.get(position).getListitem1());
        text2.setText(mlist.get(position).getListitem2());
        text2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // you just put your Logic here And use this custom adapter to
                // load your Data By using this particular custom adapter to
                // your listview
    
            }
        });
        convertView.setOnClickListener(new ListenerT(Model m) {
    
            @Override
            public void onClick(View v) {
                Model m = study;
    
            }
        });
        return convertView;
    }
    private class ListenerT implements OnClickListener {
    
        Model study;
    
        public ListenerT(Model nm) {
            study = nm;
        }
    
        @Override
        public void onClick(View v) {
    
        }
    }
    
      }
    
    0 讨论(0)
  • 2021-01-05 14:37

    In my opinion, because there is Button or ImageButton in your custom list item layout.

    So there is two solution,

    1. Replace the button to other elements, and remove onClick listener in the Adapter code. This is the easy way to solve this.

    2. Disable the button focus, like this,

    android:focusable="false"
    android:focusableInTouchMode="false"
    

    Please refer this ListView setOnItemClickListener not working by adding button

    Have fun. @.@

    0 讨论(0)
  • 2021-01-05 14:42

    Set these properties

     android:focusable="false"
     android:focusableInTouchMode="false"
    

    for your all UI elements in your create_list_item xml file.

    Also remove that properties from ListView.

    So your ListView will be

     <ListView
       android:id="@+id/createlist"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:clickable="true"
       android:cacheColorHint="#00000000"
       android:divider="#adb8c2"
       android:dividerHeight="1dp"
       android:scrollingCache="false"
       android:smoothScrollbar="true">
     </ListView>
    
    0 讨论(0)
提交回复
热议问题