Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);

            }
        });
like image 241
oyan11 Avatar asked Dec 17 '25 02:12

oyan11


2 Answers

Add android:choiceMode="singleChoice" to your ListView in your layout-xml.

like image 97
Ridcully Avatar answered Dec 19 '25 15:12

Ridcully


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.

like image 23
G_S Avatar answered Dec 19 '25 14:12

G_S



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!