Android ListView with Multiselect not deselecting items - android

I have a multi-select listview where I want to keep track of what the user has selected and what they have deselected. When a user clicks on a list item, I add the clicked items into an ArrayList and when they click it again I remove the item from the ArrayList. The issue is that the deselect operation(when a user clicks on an item already clicked on earlier) is adding into the ArrayList again instead of removing.
Below is the code for onListItemClick for that activity:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckBox checkBox = (CheckBox) view.findViewById(R.id.chk_forEasyPayListItem);
int stringPos = 0;
selectedBiller = billerNamesArray[position];
selectedBillerAccountNumber = billerAccountsArray[position];
selectedBillerBalance = billerToPayBalances[position];
SparseBooleanArray sparseBooleanArray = lstPayMyBills.getCheckedItemPositions();
if (sparseBooleanArray != null && sparseBooleanArray.size() > 0) {
for (int index = 0; index < sparseBooleanArray.size(); index++) {
if (sparseBooleanArray.valueAt(index)) {
lstPayMyBills.setItemChecked(sparseBooleanArray.keyAt(index), true);
checkBox.setChecked(true);
selectedItems.add(new PayMyBillsListItemModel(position, selectedBiller, selectedBillerAccountNumber, selectedBillerBalance));
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Array size is " + selectedItems.size(), Toast.LENGTH_LONG).show();
} else {
lstPayMyBills.setItemChecked(sparseBooleanArray.keyAt(index), false);
checkBox.setChecked(false);
//selectedItems.remove(new PayMyBillsListItemModel(position, selectedBiller, selectedBillerAccountNumber, selectedBillerBalance));
}
}
} else {
for (int index = 0; index < lstPayMyBills.getCount(); index++) {
lstPayMyBills.setItemChecked(index, true);
checkBox.setChecked(false);
selectedItems.remove(new PayMyBillsListItemModel(position, selectedBiller, selectedBillerAccountNumber, selectedBillerBalance));
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Array size is " + selectedItems.size(), Toast.LENGTH_LONG).show();
}
}
}

I think your problem may be that you are creating a new object to remove. if you had a list of "A", "B", "C", "D", "B" and you called remove("B") your list would be:
"A", "C", "D", "B" (removes first occurrence). I think you would have to look for the object in your selected list which matches the one deselected then remove that.
I am not sure on the behavior removing a new object?

Related

Update checkbox in CheckedTextView which is inside a ListView

I am a noob android studio and this is my first app I am developing.
Context: I have a ListView lv which is populated with CheckedTextViews using a ListAdapter.
ListAdapter adapter = new SimpleAdapter(
c, manageTrackers.trackers,
R.layout.list_item, new String[]{"id", "mobile", "status"},
new int[]{R.id.trackerID, R.id.trackerMobile, R.id.trackerStatus});
lv.setAdapter(adapter);
I have set up the OnItemClickListener for lv as shown below which checks and unchecks the check boxes as expected. I want the checks to remain persistent when I navigate between activities, so I am storing a key in the selectedTrackers array list.
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
{
CheckedTextView ctv = (CheckedTextView) view.findViewById(R.id.trackerID);
HashMap s = (HashMap)lv.getItemAtPosition(i);
String mob = (String)s.get("mobile");
//checked and pressed
if (ctv.isChecked())
{
ctv.setChecked(false);
for (int j = 0; j < selectedTrackers.size(); j++)
{
if (selectedTrackers.get(j) == mob)
{
selectedTrackers.remove(j);
break;
}
}
}
//not checked
else
{
ctv.setChecked(true);
selectedTrackers.add(mob);
}
}
});
When I navigate back to the activity with the list view, I call a function getSelectedTrackers which I want to select the saved checkboxes based on the key in selectedTrackers
public static void getSelectedTrackers()
{
if (basicSettings.selectedTrackers.size() == 0) return;
for (int i = 0; i < trackers.size(); i++)
{
HashMap s = trackers.get(i);
String mob = (String)s.get("mobile");
for (int j = 0; j < basicSettings.selectedTrackers.size(); j++)
{
if (basicSettings.selectedTrackers.get(j).equals(mob))
{
View v = getViewByPosition(i, lv);
CheckedTextView ctv = (CheckedTextView) v.findViewById(R.id.trackerID);
ctv.setChecked(true);
//******************************
//some call to update the view HERE
//******************************
break;
}
}
}
}
Question: I have confirmed that the function finds the correct checkbox, but none of the check boxes are displayed as being selected after calling setChecked(). I have scoured SO and tried invalidating, refreshing drawable state, notifyDataSetChanged, and I can't seem to figure it out how to get it to work. What's the best way to do this? Any help is appreciated!

Filtered listview highlighting incorrect item

I have a list view where I am filtering items through a SearchView. On activating the state for an item, it is not getting the correct item instead getting it from the position. To make it more clear, please refer to the below screenshots:
Search for keyword com and selected the filtered item (i.e activated_state)
On clearing the filter, when the position of the items changes it does not keep track of the selected item i.e com.android.gesture.builder:
I want the selection to be correct regardless of the position change.
My code in MainActivity for this section:
apps.setChoiceMode(apps.CHOICE_MODE_MULTIPLE_MODAL);
apps.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
if (apps.isItemChecked(position)) {
Adapter.getItem(position);
Toast.makeText(getApplicationContext(), "CHECKED", Toast.LENGTH_LONG).show();
count = count + 1;
mode.setTitle(count + "items selected");
list_apps.add(Adapter.filteredData.get(position).packageName);
list_apps.trimToSize();
}
else {
Toast.makeText(getApplicationContext(), "UNCHECKED" , Toast.LENGTH_LONG).show();
count--;
mode.setTitle(count + "items selected");
list_apps.remove(Adapter.filteredData.get(position).packageName);
list_apps.trimToSize();
}
I am using an extended baseAdapter, please let me know if you need to see that code as well.
Update:
I am having OnItemClick listener in the code:
apps.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
/*
for (int i = 0; i < packageList.size(); i++) {
TextView txtview = ((TextView) arg1.findViewById(R.id.textView1));
product = txtview.getText().toString();
list_apps.add(Adapter.filteredData.get(arg2).packageName);
//Toast.makeText(MainActivity.this, product,
// Toast.LENGTH_SHORT).show();
//for
}
*/
String selection;
selection = Adapter.filteredData.get(arg2).packageName;
string = (String) Adapter.getItem(arg2);
//list_apps.trimToSize();
Toast.makeText(MainActivity.this, selection,
Toast.LENGTH_SHORT).show();
I am using activated_state for the item selected on filtering and maintaining that selection.
If i am not getting wrong, then you need to use OnItemClick lisenter, and get item like this.
Search View Change position of list item, but when we fetch our item from adapter, it return currect item.
lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
SString itemName = (String) adapter.getAdapter().getItem(position);
// assuming string and if you want to get the value on click of list item
// do what you intend to do on click of listview row
}
});
You can make a function getVisibleArray() in your adapter, and call it from your onItemClickListener.
in setOnClickListener:
People personInFocus = (People) adapter.getVisibleArray().get(position);
And in adapter:
public ArrayList<People> getVisibleArray() {
return mDisplayedValues;
}
Which is your filtered array.
Ive tested it and it works.

listView count numbers of selected rows

I am trying to find out the total number of selected rows in a customized list-view. If the number of items (rows) more than 2 then we cannot click the list-view again.Here I am using customized checklist(Multiple Choice)
What's wrong with listView.getCheckedItemCount()?
lvMain.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
{
int len = lvMain.getCount();
SparseBooleanArray checked = lvMain.getCheckedItemPositions();
for (int i = 1; i < len; i++){
if (checked.get(i)) {
count++;
/* do whatever you want with the checked item */
}
}
if(count>2)
{
/* do whatever you want with the checked item count more than one x value*/
lvMain.setSelected(false);
count=1;
}
}
});
Else, you could try to store your checkboxes and the other element displayed in a row (I've used TextView in my example) in a HashMap when overridden getView method get called and then count how many elements are checked iterating over the Map :
Iterator<Entry<TextView, CheckBox>> it = listCheck.entrySet().iterator();
int i = 0;
while (it.hasNext()) {
Entry<TextView, CheckBox> entry = it.next();
if (entry.getValue().isChecked())
i++;
}
return i;
I think you are trying to count the total number of selected rows in multiple listView.
for(i=0; listCount; i++) {
if(mListView.isItemChecked(i)){
}
else {
}
}

How to get checked items from android listview?

I have an android application which uses listview.Each row consist of ImageView,a TextView and a CheckBox.
I want to get selected items from this listview.I used
private void getSelectedItems() {
List<String>list = new ArrayList<String>();
try {
SparseBooleanArray checkedItems = new SparseBooleanArray();
checkedItems = listView.getCheckedItemPositions();
if (checkedItems == null) {
return;
}
final int checkedItemsCount = checkedItems.size();
for (int i = 0; i < checkedItemsCount; ++i) {
int position = checkedItems.keyAt(i);
boolean bool = checkedItems.valueAt(position);
if (bool) {
list.add(mainList.get(position));
}
}
} catch (Exception e) {
}
}
But i want to set some items as checked with respect to a condition at start up.The checked item obtain only when if the user check/Uncheck an item.No checked item obtain even if the item is set as checked at the start up programmatically.What is the problem here?
Thanks in Advance
Do something like this,
ArrayList<Integer> checkedPositions = new ArrayList<Integer>();
myListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long arg3) {
CheckBox cb = (CheckBox) view.findViewById(R.id.yourCheckBox);
Toast.makeText(getApplicationContext(), "Row " + position + " is checked", Toast.LENGTH_SHORT).show();
if (cb.isChecked()) {
checkedPositions.add(position); // add position of the row
// when checkbox is checked
} else {
checkedPositions.remove(position); // remove the position when the
// checkbox is unchecked
Toast.makeText(getApplicationContext(), "Row " + position + " is unchecked", Toast.LENGTH_SHORT).show();
}
}
});

get all checked item of listview on check of checkbox without using POJO class [duplicate]

I've a couple of question I haven't been able to figure out.
I'm trying to get all the checked elements from a ListView but:
If I check and then uncheck an element, it's returned as "checked" by the getCheckedItemPositions() function
I don't know how can I iterate through this:
SparseBooleanArray checked = list.getCheckedItemPositions();
The other answers using SparseBooleanArray are nearly correct, but they are missing one important thing: SparseBooleanArray.size() will sometimes only return the count of true values. A correct implementation that iterates over all the items of the list is:
SparseBooleanArray checked = list.getCheckedItemPositions();
for (int i = 0; i < list.getAdapter().getCount(); i++) {
if (checked.get(i)) {
// Do something
}
}
I solved my case with this:
public class MyAdapter extends BaseAdapter{
public HashMap<String,String> checked = new HashMap<String,String>();
....
public void setCheckedItem(int item) {
if (checked.containsKey(String.valueOf(item))){
checked.remove(String.valueOf(item));
}
else {
checked.put(String.valueOf(item), String.valueOf(item));
}
}
public HashMap<String, String> getCheckedItems(){
return checked;
}
}
To set an element is checked:
public class FileBrowser extends Activity implements OnClickListener{
private ListView list;
...
list.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int item,
long id) {
BrowserAdapter bla = (BrowserAdapter) parent.getAdapter();
bla.setCheckedItem(item);
}
});
Then to get the checked items from outside the class..
MyAdapter bAdapter;
Iterator<String> it = bAdapter.getCheckedItems().values().iterator();
for (int i=0;i<bAdapter.getCheckedItems().size();i++){
//Do whatever
bAdapter.getItem(Integer.parseInt(it.next());
}
Hope it can help someone.
Jarett's answer is great, but this should be a bit faster, since it's only checking the elements in the sparse array that are present in the underlying array (only those can be true):
SparseBooleanArray checkedItemPositions = listView.getCheckedItemPositions();
final int checkedItemCount = checkedItemPositions.size();
for (int i = 0; i < checkedItemCount; i++) {
int key = checkedItemPositions.keyAt(i);
if (checkedItemPositions.get(key)) {
doSomething(key);
} else {
// item was in the sparse array, but not checked.
}
}
Pro tip: look at the source of SparseBooleanArray, it's a pretty simple class:
http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/4.0.3_r1/android/util/SparseBooleanArray.java/?v=source
My brain didn't like looping through the SparseBooleanArray and I didn't need a custom adapter, so the following was a little more intuitive for me:
Don't forget to use CHOICE_MODE_MULTIPLE in onCreate():
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Use the following method to get an ArrayList of them:
// This example is to get an ArrayList of names (Strings)
protected ArrayList<String> getNames() {
ArrayList<String> names = new ArrayList<String>();
for (int i = 0; i < getListView().getCount(); i++) {
if (getListView().isItemChecked(i)) {
// Do whatever you need to in here to get data from
// the item at index i in the ListView
names.add(mUsers.get(i).getName());
}
}
return names;
}
You can iterate through the SparseBooleanArray using a for loop and the SparseBooleanArray's size() and get(index) methods.
EDIT 3/2/2014: People have pointed out that SparseBooleanArray's size() method returns the number of checked values, rather than the true length of the array, so I have mended my answer to account for that. Essentially it is the same, except that rather than iterating for the length of the array, we iterate until we have found all checked items. Since we only care about the number of checked items, it's irrelevant that with this method, we may not actually get to the end of the array (we won't see anything past the last checked item).
SparseBooleanArray checked = list.getCheckedItemPositions();
int numChecked = checked.size();
for (int i = 0; numChecked > 0; i++){
if (checked.get(i)){
//the item at index i is checked, do something
numChecked--; //We found a checked item, so decrement the number of checked items remaining
}
else
//the item is not checked, do something else
}
final long[] checkedIds = lv.getCheckItemIds();
for (int i = 0; i < checkedIds.length; i++) {
Log.d("checkedIds", "id checked: " + checkedIds[i]);
}
Just saw the question and I was facing the same problem.
There is a simpler solution using SparseBooleanArray to exactly count how many items are checked.
This is my onClick procedure:
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.button:
SparseBooleanArray a = listView.getCheckedItemPositions();
if(checked(vArray)>0) {
String vCheckedList = "";
for (int i = 0; i < nLength; i++) {
if (a.valueAt(i) && i < nLength-1 && a.size()>1)
vCheckedList += listView.getAdapter().getItem(vArray.keyAt(i))+"\n";
else if (a.valueAt(i))
vCheckedList += listView.getAdapter().getItem(vArray.keyAt(i));
}
Toast.makeText(getApplicationContext(), vCheckedList+ " is checked", Toast.LENGTH_SHORT).show();
a.clear();
} else
Toast.makeText(getApplicationContext(), "No Item is Selected", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
The checked method:
private int checked(SparseBooleanArray vArray) {
int vCounter = 0;
for(int i=0;i<vArray.size(); i++)
if(vArray.valueAt(i))
vCounter++;
return vCounter;
}
It will solve both problem of the checked items.
I had the same problem and here is my solution with SparseBooleanArray :
SparseBooleanArray checkedPositions = lv.getCheckedItemPositions ();
int size = checkedPositions.size ();
for (int i=0 ; i<size ; i++) {
// We get the key stored at the index 'i'
int key = checkedPositions.keyAt (i);
// We get the boolean value with the key
Log.i (Tag, "checkedPositions(" + key + ")=" + checkedPositions.get (key));
}
In it's entirety this is the solution I used.
Where pos is the Recyclerview item position
SparseBooleanArray selected = new SparseBooleanArray();
//OnClick listener here or whatever you use to check or uncheck an item
if (selected.get(pos, false)) {
selected.delete(pos);
} else {
selected.put(pos, true);
}
selected.size() will reveal how many items are selected
selected.get(pos) will return true if it's selected
The following as has been stated, iterates over all those items which were selected:
for (int i = 0; i < list.getAdapter().getCount(); i++) {
if (selected.get(pos)) {
// Do stuff
}
}
Here is how I get checked items from recyclerview adapter
fun getAllCheckedItems(outList){
for (position in spareBooleanArray.keyIterator()) {
outList.add(mList.get(position)
}
}

Categories

Resources