In my apps preferences screen, i want to pop up a dialog that shows a list of checkbox items that are dynamically generated.
How does one do that and also, how does one get the checked values? I have made custom dialogs in the past, but for some reason my brain wont function today ...
Thanks.
The way I've done this is to create a ListView that contains rows of CheckBoxes.
private class CheckBoxListAdapter extends ArrayAdapter<CheckBoxListRowItem> {
}
To get the checked values, I call setOnCheckedChangeListener for each CheckBox. Each time it's checked, it updates the my model data (CheckBoxListRowItem). When you need to figure out which CheckBoxes are checked, you can get it from the model data, not directly from the CheckBox object (which is how I thought it should work originally).
I ended up creating an activity that extended ListActivity. Since I wanted a list of checkboxes (where 0 or more could be selected), in my
onCreate():
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
and I listen for clicks by overriding onListItemClick().
The list adapter that I used was ArrayAdapter:
setListAdapter(
new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice,
some_string_array));
Related
I have a listview with a custom adapter (extends BaseAdapter).
It recieves a list of objects I have to populate a ListView.
One of my object's atribute is a boolean called "checked".
on my method getView, this atribute is responsible for checking or not checking a CheckBox on my View.
Everything is working just fine and when my Activity loads, the ListView itens apear as they are on my list of objects (which was received from my database), some checked and some not checked.
But when I check one of my ListView's checkbox, I need to update my object and therefore it's value on my database. The problem is:
"How do I know which item (object) I have to update just by checking my CheckBox?"
"Don't they have the same name?"
I have a listView.setOnItemClickListener(...) where I can get my object by it's position, but it works when I click on the "row" of my list view itself, not on my checkbox... I thought about using it to check/uncheck my CheckBox... But how would I do that? Can I use the position to get a specific CheckBox from my listView?
In the end, I also thought that the best method would use the "listView.setOnItemClickListener(...)" to check my CheckBox, once it would be easier for my user to check one Item by it's row than by a tiny CheckBox, so can someone help me with the best way to solve my problem?
I'm sorry I didn't post my code, but right now I can't access it.
Take a look at this tutorial
You need to add an method in the onClick of the checkbox that will be implemented by the activity. One way to do that is to add an abstract method to the adpater and make the activity implement it. lets call onCheckBoxClicked(int position)
I have a GridView of ImageButtons where the user can select an element to add to the ListView which is the following View. The user will need to make multiple selections from the GridView. This means they will have to navigate back and forth between the two Views, adding their selections to the ListView. I need to know how to re-inflate the ListView with the elements that have already been chosen along with the new choice. Basically, I am struggling with how to preserve the list contents and then inflate the contents when another selection is made. I have been trying to use an ArrayAdapter, but I have been unsuccessful.
It's common to overlook the need to use notifyDataSetChanged() when getting to grips with ListView. Below is a basic rundown of how to populate and refresh a ListView.
Create an ArrayList for your list of elements, a ListView to display them in, and an ArrayAdapter to connect them:
private ArrayList<String> mMyElements;
private ListView mMyListView;
private ArrayAdapter<String> mMyArrayAdapter;
Setup your ListView:
mMyListView = (ListView)findViewById(R.id.myListView);
mMyListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); // There are other ChoiceModes available,
// but I'm guessing this is the most likely one you want for your situation.
Setup your ArrayAddapter and assign it to the ListView:
mMyArrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_activated_1, mMyElements);
mMyListView.setAdapter(mMyArrayAdapter);
Now you can change what is displayed in the ListView, by changing what is contained in your ArrayList. Use notifyDataSetChanged() to signal to the ArrayAdapter that it needs to update the display of the ListView:
...
// Code which changes the elements contained in the ArrayList
// For example..
myElements.add(x);
myElements.remove(y);
...
// Notify the ArrayAdapter that it's ArrayList has changed.
mMyArrayAdapter.notifyDataSetChanged(); // This line is vital to get the altered ArrayList to display.
mMyListView.clearChoices(); // You may want to clear any old selections from the ListView when you refresh the display.
<Additional>
If by "re-inflate" you mean that your GridView and ListView are in different Activites or Fragments, all you need to do is maintain the ArrayList myElements when navigating between them. You can pass the ArrayList between them in an intent.
i have an app that has a listview. each view inside of the listview has custom xml and has buttons checkboxes and textviews. i have to get the previous saved settings for the checkboxes from an sqlite database and set them. and if the user changes the checkboxes or radio buttons i have to save those settings in the database.
tried several different ways to do this and most resulted in error and would not display the correct setting for checkboxes when coming back to the page.
what finally worked is the design shown below. where i put any database calls as close as possible to the actual view. however it looks like if there is many rows in the listview it would make many calls to the database and it could get slow.
is my design correct or is there a better way
psudocode
onCreate
instantiate arraylist adapter and set adapter to view
end onCreate
arrayadapter class
getView(){
call databsase and get previous settings for checkboxes in this view and
set the checkboxes to show checked or not depending on database saved settings
onClicklistener or oncheckedchanged listener for getting checkbox positions if changed
call database and set method to store updated positions of checkbox, radiobutton or spinner
}
end arrayadapter
Your approach is correct and your concern is warranted. I can suggest you the following method, but its performance will have to be tested:
onCreate
instantiate arraylist adapter and set adapter to view
end onCreate
arrayadapter class
getView(){
call databsase and get previous settings for checkboxes in this view and
set the checkboxes to show checked or not depending on database saved settings
----> Create two ArrayLists: ArrayList<Boolean> for CheckBoxes
----> and ArrayList<Integer> for RadioButtons.
----> Fill these lists using the values from the databse
onClicklistener or oncheckedchanged listener for getting checkbox positions if changed
call database and set method to store updated positions of checkbox, radiobutton or spinner
----> Here, update the lists, not the database.
----> Execute a AsyncTask to update the database whenever the lists change.
}
end arrayadapter
Another think you can do is to load data from database in Activity.onStart() and save it back in Activity onPause(). All other time use ArrayList to track changes in UI.
In that case you will have this heavy db operations much rarely during the Activity life cycle.
I'm working with the newest Android SDK and I want to filter my ListView which represents a list of different plants, with CheckBoxes.
In my list I have 800 Items with different attributes (e.g. color, size, edibility), and the CheckBoxes should filter the list in a subtractive way. Only the list items/views which match all attributes should be visible - all other rows should be invisible(the program should work like the filtering system on www.pilzsuchmaschine.de).
I tried to modify the getView() of my custom ArrayAdapter but I didn't get the right idea how to do that properly. Does anyone have a solution?
My ArrayAdapter is pretty the same as this one.
I tried to modify the getView() of my custom ArrayAdapter but I didn't
get the right idea how to do that properly. Does anyone have a
solution?
You don't do the filtering in the getView() method. First set a OnCheckedChangeListener on all the filter CheckBoxes to monitor their status(each of those CheckBoxes should have a boolean variable to hold its status). When a filter CheckBox is checked/unchecked update the status variable and then filter the ListView. Filtering the ListView can be done in two ways, manually or by using the dedicated mechanism(the Filter class).
Manually, when the user checks a CheckBox you'll take all the statuses of the filter CheckBoxes and match them against each element of the list of plants. Which element matches all the CheckBoxes statuses is valid and should be added to a new list. After this is done, make the adapter point to the newly created list and call notifyDataSetChanged(). I wouldn't go with this approach as you have a lot of items.
The proper way would be to make your own adapter along with its Filter method(in this case the adapter will hold the statuses of the filter CheckBoxes). When the user checks a filter CheckBox call a method on the adapter to update the corresponding boolean status. Also call the getFilter() method on the adapter and do the filtering: ((Filterable) adapter).getFilter().filter(null). There are a lot of tutorials there about implementing a Filter for an adapter.
I'm using the cool feature of the ListView to show a checkbox next to the item in the ListView.
I bind my list to an array of strings.
The onClick and onSelectedItem listeners get called fine, in this way I know the index of the "string" checked (or unchecked).
I'm storing all the checked strings into preferences (as a comma-concatenated-string), and everytime the activity becomes visible I would like to set the checked items back in the listview.
Is there a way of doing it? or the CHOICE_MODE_MULTIPLE doesn't allow to set the checked items?
note: I'm not using a custom view, since what I want to display is just a string and a checkbox. I've tried setSelection(index) but it should set the onlyone selected (highlighted) row.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,names);
m_playlists_list.setAdapter(adapter);
m_playlists_list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Use the setItemChecked method of ListView
Sets the checked state of the specified position. The result is only valid if the choice mode has been set to CHOICE_MODE_SINGLE or CHOICE_MODE_MULTIPLE.