Programatically access specific rows in List of CheckedTextView's - Android - android

is it possible to programatically access specific rows in a list of CheckedTextViews to change the state of their textboxes?
my program has a listview which has several CheckedTextViews which the user can press to toggle state.
I want to save the state of the checkboxes when the user leaves the activity, so I have in my onPause method:
public void onPause(){
super.onPause();
SparseBooleanArray positions;
positions = listView.getCheckedItemPositions();
ListAdapter items = listView.getAdapter();
int j = items.getCount();
ArrayList<Long> ids = new ArrayList<Long>();
for (int k =0; k < j;k++){
if(positions.get(k)==true){
ids.add(items.getItemId(k));
}
}
this.application.getServicesHelper().open();
this.application.getServicesHelper().storeServices(ids,visit_id);
this.application.getServicesHelper().close();
}
which very simply iterates the list view, adds the checked items to an ArrayList and then saves that list of ids to the database.
My problem lise in trying to reset the list once a user goes back to that activity.
so far in my onStart method, I recall the checked items from the database, but I do not know how to march the ids returned to the listview elements. can I do something like:
listView.getElementById(id_from_database).setChecked?
I know I cant use getElementById but I have it here to show what I mean
Thanks in advance
Kevin

You can call
listView.setItemChecked(int position, boolean value)

This is what Ive ended up doing.. but it seems like a complete hack.
Basically I have to set up a double for loop.. one to iterate through my list elements, and one to iterate through the cursor that I have retreived my check list state (a simply array of ids of elements that were checked when state was last saved)
my outer for iterates through the list elements checking each id against a loop through the list of ids to be set as checked. if they equal each other then set that item as checked.
// mAdapter is contains the list of elements I want to display in my list.
ServiceList.this.setListAdapter(mAdapter);
// Getting a list of element Ids that had been previously checked by the user. getState is a function I have defined in my ServicesAdapter file.
Cursor state = ServiceList.this.application.getServicesHelper().getState(visit_id);
int checks = state.getCount();
int check_service;
int c = mAdapter.getCount();
if(checks>0){
state.moveToFirst();
for (int i=0; i<checks; i++) {
// set check_service = the next id to be checked
check_service = state.getInt(0);
for(int p=0;p<c;p++){
if(mAdapter.getItemId(p)==check_service){
// we have found an id that needs to be checked. 'p' corresponds to its position in my listView
listView.setItemChecked(p,true);
break;
}
}
state.moveToNext();
}
}
ServiceList.this.application.getServicesHelper().close();
Please tell me there is a more efficient way of achieving this!!
Thanks
Kevin

Related

Getting a spinner's item list

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

Remove items from listview on its ItemClickListener

I am using a collection of ArrayList to fill my Listview. My ListView contains two separate rows types.
Header and Footer.
I am trying to achieve the ExpandableListView Functionality on my Listview from which I am trying to remove some items on click of header till next header.
I am using this function to loop through items and removing items
private void removeItems(int value)
{ Log.e(Constant.LOG, items.size()+"");
for (int i = value;i < items.size(); i++) {
if(!items.get(i).isSection())
items.remove(i);
}
Log.e(Constant.LOG, items.size()+"");
adapter = new EntryAdapter(this, items, this);
mListView.setAdapter(adapter);
}
QUESTION IS : I am not able to remove all items from the list in one shot, some stays there !
I have tried looping through adapter.count(); but no luck
My List :
SECTION 1
ITEM 1
ITEM 2
Item N
Section 2
But when I click on Section 1 not all ITEMS get deleted in one shot WHY!
I am not able to use Expandable Listview at this stage because activity contains many more complex functionality on List. Please help me where I am going wrong!
Create a new ArrayList<Collection> , Then add your item in it and then use removeAll(collection).
TRY THIS:
private void removeItems(int value)
{ Log.e(Constant.LOG, items.size()+"");
ArrayList<Collection> deleteItems= new ArrayList<Collection>();
for (int i = value;i < items.size(); i++) {
if(!items.get(i).isSection())
deleteItems.add(items.get(i));
}
items.removeAll(deleteItems);
Log.e(Constant.LOG, items.size()+"");
adapter = new EntryAdapter(this, items, this);
mListView.setAdapter(adapter);
}
EDIT
Every time you are deleting an item, you are changing the index of the elements inside .
e.g : let suppose you are deleting list1 , then list[2] becomes list1 and hence your code will skip list1 next time because now your counter would be moved to 2.
Here are other ways by which you can achieve this also,
Removing item while iterating it
So what exactly I did now. Instead of looping through items again I did like this :
I created another list and parallely populate it with the main array.
items.add(user);
// after populating items did this
newItems.addAll(items); // same collection ArrayList
and finally I can play with the main array by using removeAll and addAll methods.
items.removeAll(newItems); // remove items
items.addAll(afterPosition,newItems); // add items after position

Remove multiple checked items from list view using iterator

I have a ListView which uses CHOICE_MODE_MULTIPLE.
Once the user has made their selections, I need to remove the selected items from the list.
I have used getCheckedItemPositions() to return the SparseBooleanArray of checked items.
All the documentation and related questions I've read indicate that to remove items from a list efficiently, you should use an Iterator, but I can't figure out how to do this.
I have achieved the same operation by looping though the items in the ListView by index and using a temporary clone of the items list to look up which items should be removed (see code below). But supposedly looping through indexes like this to remove items from a List = Bad Coding Monkey. I'm told I should do it with an Iterator instead. But how?
protected void removeItems() {
SparseBooleanArray itemsSelectionState = mItemsListView.getCheckedItemPositions();
ArrayList<String> itemsClone = (ArrayList<String>) mItems.clone();
for (int i = 0; i < mItemsListView.getCount(); i++) {
if (itemsSelectionState.get(i) == true) {
mItems.remove(itemsClone.get(i));
}
}
mItemsListView.clearChoices();
mItemsListAdapter.notifyDataSetChanged();
}
You need the position anyway to lookup the boolean value in the Sparse array. there's not much point in using an iterator if you have to maintain an index value to determine if you should remove that item.
Yes, I think your approach is valid. The iterator does have a remove() method, but you would still need have an index handy. If the id's are stable (with ArrayList based adapters, the id usually equals the position) then it might be more efficient to use getItemIds () and iterate through that list, using the id's as positions.
// could also store ids[] and pre-allocate mItemsClone to ids.length
for (long id : ..getItemIds() ){
itemsClone.put( mItems.get( (int) id) );
}
and that might be a bit more efficient since it's only one pass

How to add a select all button to a custom Listview

I have a custom listView in my app I would like to implement my select all button I have created.
My ListView looks like.
[(Image)(Text)(CheckBox)]
I have looked at some similar questions, the most common answer was with the user of the notifyDataSetChanged () method, iv'e tried researching and implementing without any luck, I was wondering can you think of a way round it or give me an example of how I can implement the method
A simple way of doing this would be to iterate through the ListView to get each item View, then check it off. I have provided some sample code below given that you are able to acquire the ListView as sampleListView and have an id of the checkbox of checkBoxId:
// Loop through all the items in the list view
for(int viewIndex = 0; viewIndex < sampleListView.getCount(); viewIndex++)
{
// Get the current list item
View listItem = sampleListView.getChildAt(sampleListView.getFirstVisiblePosition() + viewIndex);
// Get the checkbox within the list item
CheckBox currentBox = (CheckBox)listItem.findViewById(R.id.checkBoxId);
// Check the checkbox
currentBox.setChecked(true);
}
You could place this code within the OnClickListener() of the button and it should do the trick.
I couldn't get the above answer to work, so I used parts of another answer and found the answer.
I added this this globally.
ListView list;
//\\ 0 = None set \\// 1 = Select all \\// 2 = Un-Select all//\\
int selState = 0;
Then in the onClick method I used
selState = 2;
list.invalidateViews();
selState being equal to what function you want to do
In the Adapter, this is final part of code
switch(selState)
{
...
case 2:
CheckBox.setChecked(false);
break;
}

Android listview updating all items

I have a listview with a big list of items.
Now I have a common button "select all" on click of which should do some operation on each item in the list.
I saw the answers about overriding onscrollchanged. But I don't want user to do scrolling. Without that itself all items should be selected.
Currently I am iterating using listview.getcount, but this update for only current view group.
I suppose you have your custom BaseAdapter and overridden getView(..) method refreshes each view according to its state (checked/unchecked). If so, then you just need to make changes to the underlying data of this ListAdapter (let's say, iterate over the array which stores objects which are represented by this list and change some property of each object) and then call notifyDataSetChanged() method.
What is working for me:
private void enableAllListItems() {
for(int i=0;i<mCursorAdapter.getCount();i++)
mCheckedPositions.set(i, true);
mListView.invalidateViews();
}
The import part is the "invalidateViews()", because you have to say the ListView that it's underlying data changed!
But I'm actually using my own Adapter so I overrode the getView()-Method. I'm not sure if it will work with the standard Adapters.
You iterate it using the generic list that you use to populate the list. that would help you.
try this.
private void SelectAll() {
// TODO Auto-generated method stub
int count = this.mainListView.getAdapter().getCount();
for (int i = 0; i < count; i++) {
this.mainListView.setItemChecked(i, true);
}
}

Categories

Resources