Listview and multiple selection

Tutorials with advanced 'difficulty' and more Lines of Code.

Listview and multiple selection

Postby reddy506 » Tue Oct 26, 2010 1:11 pm

Hi all,

I have a custom lisview with multiple rows. Each row has one checkbox.

And user selects all the rows which he wants to delete .

and In menu we have a option for delete .

when the user clicks on delete in menu we have delete all the rows which are checked .

I need some idea to implement it.

Thanks in advance.
reddy506
Junior Developer
Junior Developer
 
Posts: 11
Joined: Fri Sep 03, 2010 12:13 pm

Top

Re: Listview and multiple selection

Postby dmurarik » Tue Oct 26, 2010 6:16 pm

ItemsList= new ArrayList<String>();

TheListView = (ListView)findViewById(R.id.ListView01);


TheListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, ItemsList));
TheListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);//this is important

//instead of a save button use your menu option
Button save = (Button)findViewById(R.id.ButtonchannelsSave);
save.setOnClickListener(new View.OnClickListener(){

public void onClick(View v) {
if(TheListView != null)

a = new SparseBooleanArray();
a.clear();
a = TheListView.getCheckedItemPositions();
long[] b;
b = TheListView.getCheckItemIds();
if(a.size() > 0 && a != null)
{
for (int i = 0; i < a.size(); i++)
{

if(a.valueAt(i) == true)
{
ItemsList.get(a.keyAt(i)).toString(); // this will give you the value of the item checked. say"Item1". At this point you could remove them from the current list of items.
}
}
}
}
});
}

In the above example I save the list of items. You could update ItemsList and then call
TheListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, ItemsList)); or TheListView.invalidate(); should do it.
again.
dmurarik
Freshman
Freshman
 
Posts: 8
Joined: Tue Oct 26, 2010 6:02 pm

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests