简体   繁体   中英

Custom List Array Adapter is not clickable

When I used a custom list array adapter for list view, the data are shown on the list but are not clickable. I already set a listener on a listview item click, but it's not working.

Also, I found another solution to make rows clickable and set background drawable on linear layout list view item layout but it's a bad trick to use this solution.

Is there any other solution for this?

Can any body tell me why does this happen? In list view which default functionality not working on custom array adapter

Here is my code:

public class Model {
    String name;
    String id;
    String price;
}

mArrayList = new ArrayList<Model>();
// mArrayList has some data in it 

mListiView.setsetAdapter(new myCoustomAdapter(mContext, mArrayList));
mListiView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(getBaseContext(), ""+position, Toast.LENGTH_LONG).show();
    }
});

public class myCoustomAdapter extends ArrayAdapter<Model> {
    ArrayList<Model> mList;

    public myCoustomAdapter(Context context, ArrayList<Model> list) {
        super(context, R.layout.item);
        this.mList=list;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // layout data here 
        return view;
    }
}

Nothing happens on item click.

The first thing what you have to note here is, whenever there are Clickable elements like Buttons or ImageButtons present in your ListView element, they take the control of click events. And so your ListView won't get the chance to accept the click event.

What you simply have to do is, set the focusable attribute to false for the Button or ImageButton you have in your ListView. But still they will work without any problem and also your ListView's onListItemClick will also work.

Try this,

    <Button  android:id="@+id/textsize_increaser"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/back_button"
    android:focusable="false"
    android:text=" A + "/>

Here I have added this android:focusable="false" and it works fine. try it.

Here is link for reference Click is not working on the Listitem Listview android

很可能您的列表视图行的布局包含可点击的元素,这会“窃取”您的点击。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM