简体   繁体   中英

Android: limit selected item on list to 1 in ListView

i have a ListView and i want to select one item from the list and change the background color to gray. Currently, if i select one from the list, i can also select another... i want to limit my selection to 1 and when i select an item i will not be able to select another item.

here's my code:

myadapter =  new MyAdapter(Main.this, object_list);
mylist.setAdapter(myadapter);

mylist.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                view.setBackgroundColor(Color.LTGRAY);

            }
        });

android:choiceMode="singleChoice"到 layout-xml 中的ListView

I think you are using a custom adapter and so you can use the method isEnabled(int position) of the baseadapter class and make the item that is clicked enabled and the remaining disabled. But i wonder what's the reason for making the other items unfit for selection.

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