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.
Related
My problem is how can i update list view within view pager.
i am using the same 4 tabs having a list view with different arraylist set to there adapters.
I am using only one fragment and bases of tabs position I am loading different list data to inner fragment arrayAdapter. Also I have two buttons one is delete and another one is Add.
What I want: If I press the add button then it should add new data to the array list(based on the tab position add the new add to respective arrayList) and refresh the listview data.
Use onPageSelected(int position) from your Pager to figure out at what Array you have to add items.
Then, after you add that new item to Array, you should set ArrayAdapter again for those ListView since list has changed.
Then call notifyDataSetChanged() on your Adapter object once you've modified the data in that adapter to refresh ListView.
Edit: you don't have to use constructor for changing ArrayAdapter, this should work:
ArrayAdapter.clear();
ArrayAdapter.addAll(changedArray);
ArrayAdapter.notifyDataSetChanged();
I have two listviews with custom adapters. The first listview contains names of lists. When I click on the name of a list, the second listview have to be populated with the contents of the clicked list. I need to read the contents of the list from a local SQLite database that's located in assets, but that's not important. Assume that I have array of the contents. I know how to do this if I have to populate them on button click, but I don't know to do it this way. Both listviews are in the same activity.
by using onItemClickListener of listview u can do this.
on onitemclickListener of first listview u set adapter for the second listview
u can do this using one customadapetr and u can write separate customadapter for both
listview.
if u post some sample code i will try to give more specific answer
I am trying to create a list view, in which items can be added dynamically, the dynamic part is working fine, as it can be done using simple cursor adapter and inflating a layout with the list item, each time an item is created. But now the problem I am having is that, I want to swap these items as well, swapping as in replacing item positions. All the examples I have seen use a string array, that is a predefined list. How can I achieve this?
Use an Arrayadapter as your Listadapter. You can now use insert(object, int) to add an item to a specific position in the lists dataset. With remove items can be removed from the lists dataset. Since the Arrayadapter will monitor changes to the dataset itself the list should update once you are done modifying the Adapter.
If you need to use a CursorAdapter this may get harder. You would need to change the underlying database and then requery the Cursor that is used in your list.
How can I recreate listView?
I have listView with items containing few TextViews and I want, to reaload ListView and hide part of those TextViews from every row. I tried setting new adapter, clearing adapter (it makes listView empty), invalidating listView, using notifyDataSetChanged(). Nothing forced ListView to recreate items.
Use a ArrayAdapter backed by an ArrayList. To change the data, just update the data in the list and call adapter.notifyDataSetChanged() - Robby Pond
Also please check Google I/O 2010 - The world of ListView
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));