In my application, I have a Spinner which includes list of categories and GridView to show list of
Images according to selected category.
My question is: "How to reload images on GridView after changing category in Spinner?"
From the code that gets the selected category from the Spinner, get your GridView's adapter and then call clear() on it and then addAll(T... items) with the new category items you want to show.
Keep in mind that this assumes you've created the adapter by passing it a mutable List.
If you created the adapter with an immutable List or T[], then you can create a new adapter and set it on the GridView instead.
Related
I just created a filterable recyclerView of an adapter with textview.onTextChangedListener in my project. it worked just fine. unfortunately, this has caused a problem with my adapter click listener. since I implemented set on click listener to my adapter by taking adapter position when we click on an item in adapter recyclerview it will get the adapter position and pass the item values like text and image to textview and image view respectively.
The filtering method I created is to filter items in adapter recyclerview by userName. so when I type to search for a name, the recyclerview will display the matching one but when I click on that matching name it passes the wrong item values to my textview and imageview. it always passes the first item of adapter recyclerview. I guess that maybe the adapter always takes the 1st position of the adapter dataset since after filtering the only matching item will display. I have tried a lot but I still have no idea to solve this problem.
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 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 need to create dashboard using GridView.
One of the items will be an item that, when clicked, will add new item to an adapter array.
I need to create that one item in a way that it will not be added to the adapter array, because i.e all items will be saved to file.
Is there any way that I can add such item without adding it to adapter array of GridView?
Create your adapter: ArrayAdapter<(type)> adapter = new ArrayAdapter<(type)>(params) then create a button (or whatever item you are using to add values to the array) and set an onClick where you can do adapter.add(item). If you want that button (for example) to be in your GridView but not save it to a file, just have your file saver exclude position=0 which should be defined in your Adapter class.
I have multiple Spinners inside listview control at particular row.
I fill all spinners dynamically from WebService and all spinner values can change on selected item.
Exp. I have 3 spinner of country and State and City then all this 3 spinners fill from WS and while user select country then on countryselecteditem change all data of state and city. And on State selected item city data change...
And also I scroll and go on that row at that time getView() call all 3 selected item and fill all adapters by selected item.
How can I handle 3 spinners inside listview?
Design a ListItem Layout containing 3 spinners of your wish and then set it to ListView Adapter.
Then Take An SubClassArrayAdapter with Generic Type of a POJO Class containing 3 IDs (if required take more) like below and set Adapter to ListView.
class SpinnersStateInListItem
{
private int countrySelectedPosition=0;
private int stateSelectedPosition=0;
private int citySelectedPosition=0;
//here you need to generate respective getters and setter methods.
}
//do the below code to set the SubClassArrayAdapter to listview which you derive from ArrayAdapter class in which you customize your views
SubClassArrayAdapter<SpinnersStateInListItem> = new SubClassArrayAdapter<SpinnersStateInListItem>(context,resourceid,listof SpinnersStateInListItem Generic type);
inside the getView() of your SubClassArrayAdapter Class, create 3 spinner objects and set array adapter to the first Spinner and then set OnItemSelectedListeners to 3 spinners.
Then in the first Spinner OnItemSelected method set the ArrayAdapter to 2nd Spinner which will contain the list of State as per Country selected in the first spinner. Then set array Adapter to 3rd Spinner inside 2nd Spinners OnItemSelected method which contain the list of cities as per country selected and state selected. Here you should filter the data as per selected items. When Items are selected immediately save them to SpinnersStateInListItem respective object so that even if you reload the listview all the items state will remain as it is. for example as below.
OnItemSelected()
{
if(v==firstspinner)
{
listofSpinnersStateinListItem.get(listitemposition).setCountrySelectedPosition(spinnerselectposition);
}
}
in the sameway as above for state and cities too.
Initially you fetch the Data of first country and set it to the First Spinner which simultaneously fetch the first country states and then first country, first state related cities.
Hope this will help you.