Search code examples
androidlistviewonclickonitemclick

fire OnItemClick on listview when button is clicked


I have a ListView that contains button on each item. I want that the item click will cause the OnItemClick event of the list to fire, as that event contains more info (id,position) than the OnClick event of the button.

This is an important action on the list, that's why I need the button and don't want a regular item click will cause it to fire (I guess making the button focusable will do the trick).

Is this possible?


Solution

  • try to make your own adapter for the list.In getView() method do your necessory requirements.

    setListAdapter(new ArrayAdapter<string>(this, R.layout.list_item, strings) {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    View row;
    
    if (null == convertView) {
    row = mInflater.inflate(R.layout.list_item, null);
    } else {
    row = convertView;
    }
    
    Button bt1 = (Button) row.findViewById(android.R.id.bt1);
    bt1.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v){
           //you will get position and do necessary method 
           }
    });
    
    return row;
    }
    });