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);
}
}
Related
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
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.
}
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;
}
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);
}
}
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