Following the guide found here http://developer.android.com/guide/topics/ui/menus.html#CAB I went up to a dead end on how to remove all the selected items from the listView's adapter.
In the guide it is shown as a method called deleteSelectedItems(); but since it is never implemented, I got stuck. How can I do this?
I asume you are using a List. Do the following:
private void deleteSelectedItems() {
SparseBooleanArray checked = mListView.getCheckedItemPositions();+
List<YourObject> list = mListOfObjects;
for (int i = 0; i < mListView.getCount(); i++)
if (checked.get(i))
YourObject item = list.get(i);
mListOfObjects.remove(item); //or whatever you want to do with it.
}
Related
I have a ListView which am setting that to my adapter. The ListView item contains two view elements which are chekbox and TextView.
I have given the setChoiceMode(ListView.CHOICE_MODE_MULTIPLE) for the ListView.
When i want to select one or more element, I want to get the count (based on selecting and deselecting).
I can't do this stuff in onItemClickListener of ListView.
So I have written the logic in BaseAdapter.
holder.check.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count += 1;
}
});
But If i deselect the item then the value have to decrease. If iam having only one item in ListView then I can write
SparseBooleanArray checked = listView.getCheckedItemPositions();
and get the value in Fragment. Just I get confused. Could someone help me?
Use this.
SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
int count = 0;
for (int i = 0, ei = checkedItemPositions.size(); i < ei; i++) {
if (checkedItemPositions.valueAt(i)) {
count++;
}
}
// use count as you wish
Make sure count is in a local (or block of the OnClickListener) scope so that every time you click a button or something, count gets reset and it recounts how many items are checked/unchecked.
Let me know if you have more question :)
I can retrieve a list of all items of a spinner by:
List<String> list = new ArrayList<String>();
for (int i =0; i<spinner.getCount(); ++i)
{
String item = String.valueOf(spinner.getItemAtPosition(i));
list.add(item);
}
Or storing the item list globally...
Is there any more elegant way, something like .getItemList()?
My concern is the iteration (linear complexity), I would prefer to directly get the list from the adapter (possibly constant complexity?)
See http://developer.android.com/reference/android/widget/Adapter.html and http://developer.android.com/reference/android/widget/SpinnerAdapter.html (which inherits from Adapter)
You will find that the method you're looking for doesn't exist
Your solution is about as elegant as it gets
I have a listview , i would like to put some position preselected; in fact i have some values that i would like to put them selected. this is my code
for (int i = 0; i < checked.size(); i++) {
// Item position in adapter
int position = checked.keyAt(i);
// Add sport if it is checked i.e.) == TRUE!
if (checked.valueAt(i)) {
selectedItems.add(adapter.getItem(position));
}
}
String[] outputStrArr = new String[selectedItems.size()];
for (int i = 0; i < selectedItems.size(); i++) {
outputStrArr[i] = selectedItems.get(i);
}
My selectedItems firstly is not empty; i would like to put its value as selected.
Pass your selectedItem list to your custom adapter and inside getView() method compare position. If it is available in seloectedItem list change the background of view.
Use a custom adapter and pass the list inside there and render it on getView() by inflating the item.xml layout and use a static class viewHolder to create a viewholder that transfers the data from your list into the appropriate widgets.
Make sure you check if the convertView has been created previously as you do not want to create the view every time android hits getView from your adapter as it will slow your listView UI.
Good luck
I would like to find a specific item in the list and have it selected by default. If I can find out the position, I can call ListView.setItemSelected(position, true)
I'm using a SimpleCursorAdapter to show a list of categories and this can change dynamically so I can't just find the index and hard code.
How do you find the position of a list item by a String without use of the OnClickListener?
Hard to say what might be best for your implementation without seeing your code but maybe an implementation like this one
SimpleCursorAdapter myAdapter;
...
//search here
int count = myAdapter.getCount();
for(int i=0; i < count; i++){
if("desired string".equals(myAdapter.getItem()){
listView.setItemSelected(i, true);
}
}
I am using a ListView with MULTIPLE_CHOICE and to get the selected items back i am using setItemChecked() method.
It works fine as i am able to see the previously checked items.
The issue is that if i uncheck one of the previously checked items, and then get the list of checked items by custList.getCheckItemIds()
the array still has the Item that i unchecked.
Can anyone please tell me if that is supposed to happen or am i missing something?
Here you have to call setOnCheckedChangeListener and you have to manage the code inside this listener block.
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Write and manage your code here.
}
});
If you are simply trying to find out what items are checked at any given time, you can get a SparseBooleanArray from the ListView, and iterate over it with a for loop. For example:
SparseBooleanArray checked = list.getCheckedItemPositions();
for (int i = 0; i < checked.size(); i++){
if (checked.get(i))
//the item at index i is checked, do something
else
//the item is not checked, do something else
}
this:
SparseBooleanArray checked = list.getCheckedItemPositions();
for (int i = 0; i < checked.size(); i++){
if (checked.get(i))
//the item at index i is checked, do something
else
//the item is not checked, do something else
}
doens't work.
follow Multiple Contact Picker List [getCheckedItemPositions()]
should be OK.
SparseBooleanArray selectedPositions = listView.getCheckedItemPositions();
for (int i=0; i<selectedPositions.size(); i++) {
if (selectedPositions.get(selectedPositions.keyAt(i)) == true) {
//do stuff
}
}