How to disable the particular list item in list-view in android?
Asked Answered
U

6

16

How to disable the particular list item in list-view in android? I mean if once i selected any one of item from a list-view,that item suppose to be disabled which means that item should not be select-able again. How to do this?

Suggestions please

Thanks for your precious time!..

Uproarious answered 29/7, 2013 at 8:19 Comment(2)
are you using Custom adapter for listview??Lucan
Then you should save which item is selected and then change view.enable=false. and when you are loading or refreshing listview check one condition that if view item is equal to clicked item then disable it.Lucan
L
24

try using this code in setOnItemClicklistener()

if(listview.getChildAt(selectedPosition).isEnabled())
{
    listview.getChildAt(selectedPosition).setEnabled(false);
}
Lint answered 29/7, 2013 at 9:2 Comment(0)
C
23

When you pass a list of data elements to BaseAdapter, add a field in this list's element class called isEnabled and set it to true/false as needed, then override isEnabled method of BaseAdapter like this:

@Override
public boolean isEnabled(int position) {

    return list.get(position).isEnabled;
}

where list is your list of data element objects.

Calabresi answered 16/12, 2014 at 4:5 Comment(0)
S
11

Try overriding BaseAdapter.isEnabled() in your adapter, and calling this method from your onItemClick().

Supposing answered 29/7, 2013 at 8:26 Comment(0)
B
0

When you click the item have it set off a boolean.. And what method would use that item have it check to see if the boolean has been triggered, set to false for example. Then in your on click portion use BaseAdapter.isEnabled() = false;

Or with just that boolean alone they can never click it again.

Brockbrocken answered 29/7, 2013 at 8:50 Comment(0)
G
0
 mlistView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{

 public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3)
{
   arg1.setEnabled(false);
 }
}
Guayule answered 29/7, 2013 at 9:8 Comment(0)
S
0

In order to disable list items on list creation you have to subclass from ArrayAdapter. You have to override the following methods: isEnabled(int position)

class MenuAdapter extends ArrayAdapter<String> {

public boolean isEnabled(int position) {
   // return false if position == positionYouWantToDisable
}

}

Or in Activity class

public class MainActivity extends Activity {

ListView listview;
ArrayAdapter<String> arrayadapter;

@Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 listview = (ListView)findViewById(R.id.listView1);
 button = (Button)findViewById(R.id.button1);

 arrayadapter = new ArrayAdapter<String>(MainActivity.this, 
 android.R.layout.simple_list_item_1, subjects);

 listview.setAdapter(arrayadapter);
 listview.getChildAt(1).setEnabled(false);
 }

}

Strategist answered 23/6, 2017 at 9:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.