Updating list item in array adapter - android

I have been trying to update the last element of my list view by doing the following
mNavTitles.remove(last_position); //mNavTitles is an ArrayList<string>
mNavTitles.add("Notifications Disabled");
((BaseAdapter)mDrawerList.getAdapter()).notifyDataSetChanged();
However this doesn't work. If I choose to add or remove exclusively, this works just fine. Why can't I update my list by removing and then adding? And if this is not possible how would I go about updating this in the simplest way possible? I've seen solutions that involve recreating the whole array list but I really don't understand why that's necessary when adding and removing both work.
EDIT, more context:
This is happening inside of the onitemclick listener from my nav drawer. When the last nav item is clicked it is supposed to update the title text of the item. I am unable to update onclick, merely add xor remove a list item.

mNavTitles.set(index-number, "new value");
For example if you want to update your 5th element in your arraylist. You shoul try follow
mNavTitles.set(5, "new value");

Related

Refresh listview in fragment

In my android application, I have one spinner box and two edit text box for adding item.When I click on add button all the data will store in database and display in below Item list (listview display in my first image) by refreshing listview.
My problem is, when I click on add button at a time only one list is display and listview heading is also gone.
May be this problem is raised due to refreshing of listview.
I searched on google and found notifyDataSetChanged() method is used for refreshing list view but I don't know how notifyDataSetChanged() method is used in fragment?
Does anybody know how to use this method in fragment?
I am new to fragment. In my app all the java code is developed using fragment activity, and xml file is simple layout.(LinearLayout/RelativeLayout)
You can see my screen here:
1)First image show list heading.
2)Second image show List item.
You need to setAdapter again to your listview after adding new data. it will show new data int listview.
if not get, post your code where you setAdapter to listview.
You have to first add item in Arraylist/List which you passed in Adapter then after you can call below code..
adapter.notifyDataSetChanged();
listView.invalidateViews();

How to add list item in android dynamically?

In my android application I want simple list which should be initially blank when I add item it would my root list item. Further, when I add more item in that list it all should be under that root list item and it should keep number of item added under that root list .
How can I achieve this?
Have a look this answer listView dynamic add item
In this code we are add data at run time.
first of all we set the an empty adapter to listview after that at run time we add the new item on button click
If you want nested items then your best bet is to use the ExpandableListView http://developer.android.com/reference/android/widget/ExpandableListView.html
If you want more than one level of nesting then I do not believe there is anything out-of-the-box to achieve this.
Just use an ArrayAdapter to show items on the list.
When you need to add items just add them to the array and call notifyDataSetChanged();

Adding a row to a ListView that displays "Loading" while information is downloaded. Like in the Market

I've tried, but have had no luck trying to find this answer elsewhere.
I want to add a row to the bottom of my listview that displays "Loading..." and maybe a spinning progress indicator.
My program already loads additional information into the listview once the user scrolls to the bottom. But I want the user to be able to see that the program is indeed loading something.
Example: If you go to the android marketplace and scroll to the bottom of one of the lists, the last row will say "Loading...". Then once the data is loaded, that bar is replaced with the first item of the new data.
Sorry, it's a little hard to describe. I am NOT trying to add a footer to the bottom of the list view. I want it to be an actual item in the listview.
Take a look at the following library:
https://github.com/commonsguy/cwac-endless
It does exactly what you're looking for and it's also very easy to use.
I've used it myself on one of my apps: WorldSpeak. Take a look on it to see the result.
Assuming you have a TextView in the list items, you could add a new item to the end of the list that says "Loading..." and then remove that item when you update the list. I.e., if "hashlist" contains the data as declared in the adapter,
ArrayList<HashMap<String,String>> hashlist ;
HashMap<String,String> loadingitem ;
loadingitem.put( "line1",getString(R.string.loading));
hashlist.add(loadingitem) ;
and then...
// remove "loading..." list item if it's there
hashlist.remove(loadingitem) ;
// then add the new items

Android List adapter issue

How to remove item from a list view? how to reload the list after removal?
Here you can find all the informations.
Anyway you can call
mListAdapter.remove(x); // to remove an item
and
mListAdapter.invalidate(); // to refresh the content
or
mListAdapter.notifyDataSetChanged();
Please, there's no need to write with multiple question marks like that. Removing items from your ListView depends on how you put in the items in the beginning. Edit your question to provide some details on how you did that. Updating your list afterwards can be done with notifyDataSetChanged() called by your ListView adapter.
Remove from the adapter, and the list will refresh automatically.

How can I "set View" in right place where I remove an item in ListView (Android)

I choose an item to remove from my ListView. And after the Item was removed, my ListView was scrolled back and display at the first Item.
I want my ListView display in right place where the Item I had removed (It like remove a contact in Android Contact list). How can I do that?
I suppose you want to update your ListView display, after you remove an item, right?
When you remove the item, you have to modify your data adapter, and let the ListView change accordingly (something like the MVC pattern behavior). I've seen somewhat similar questions (and the corresponding answers :) ) here and here.
Edit:
Aha, in that case, try using setSelection(int), after recalculating new indices and new item count.

Categories

Resources