how to update fragment using the viewpager in android - android

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();

Related

How to add a item to Listview but always make it the last item?

I am going to manually add one item to the listView but i always want that item to be the last item in the listview because i am already adding items the listView dynamically
here is what i am using to add the items:
tableItems.Add(new TableItem("Test","Test",Resource.Drawable.Icon));
you should use your custom adapter.
In your adapter, create Add method and make the new item as the last item of ArrayObject or another way make sure that method getItem (int position) return the item you had added with position == (arrayObject.size - 1)
You could probably use listView.setFooterView(footerView); like shown here.
You need to add data in your listview adapter and need to notify your adapter.
i.e.
_youAdapter.add("ABC");
_youAdapter.notifyDataSetChanged();
Your data will add in listview at the end of the list always.
Notify your adapter after this line
tableItems.Add(new TableItem("Test","Test",Resource.Drawable.Icon));
mAdapter.notifyDataSetChnaged();
if in any case this not work then directly add item to adapter ie.
mAdapter.Add(new TableItem("Test","Test",Resource.Drawable.Icon));
mAdapter.notifyDataSetChnaged();

How to reload Gridview's Items after changing selected item in Spinner

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.

add more to listview with out calling notifydataset

im trying to create a feed like instagram. When the user reaches the bottom of the listview more pics should be added to the listview. Is there a way to add more to the listview with out calling notifyDataSetChanged() because it causes the listview to flicker.
Try creating a new Adapter or re-initializing the Adapter being used with added data then set that Adapter to the ListView.
Then use setSelection(int position) to move to the bottom of the ListView.

ListView Inflation/Preservation

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.

How canI dynamically add items in a list view and swap them accordingly

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.

Categories

Resources