Update ListView when updated list has got from server - android

How dynamically update the content of ListView?
App gets list from server and shows it. Also app caches this list for showing it to the user without delay. Somewhere in the future app will get updated list (update is small: two items was added, one was deleted).
How to detect changes and update only these items in ListView? Is there any complete solution?

If you are using ArrayAdapter, you can use .add(), .addAll(), .remove() to change data of the Adapter. And then call .notifyDataSetChanged() to refresh the view in the GUI.

Here are codes to refresh ListView when its data source is changed.
List<Something> lists;
And your Adapter should like this:
public MyAdapter extends BaseAdapter{
private List<Something> mLists;
public MyAdapter(List<Something> lists){
mLists = lists;
}
...
}
And initialize Adapter:
MyAdapter adapter = new MyAdapter(lists);
listView.setAdapter(adapter);
And when lists changes and you want to refresh ListView, call like this:
adapter.notifyDataSetChanged();
or:
listview.notify();
Then you will see your ListView is changed.

Related

Don't showing new record on ListView

i have 3 class. MainActivity,OneFragment,TwoFragment, I have listview which i fill using by Sqlite on OneFragment.And I add record from TwoFragment (with CreateData function using by my SqlHelper class) but new record dont showing in the listview.
How can i solve this?
If you want to see codes i can upload.
I highly recommend you to use RecyclerView instead of ListView (as Google says:
The RecyclerView widget is a more advanced and flexible version of ListView.
), but Both of them you need some refresh data may be like this on your OneFragment:
#Override
public void onResume() {
super.onResume();
items.clear();
items = dbHelper.getItems(); // reload the items from database
adapter.notifyDataSetChanged();
}
this question may help you with your problem Android ListView not refreshing after notifyDataSetChanged
if you do not understand send code

Endless Scrolling List View in android

I am displaying list of records using List View. I am loading records when user scroll to end.
Here my problem is records is loading but scroll goes to first record every time and how to end when records all are loaded.
You mean when the user reaches the bottom of the ListView you load additional data, but then the ListView jumps back up to the top of the ListView?
That is probably because you reset the adapter. Try modifying the data source of the adapter and then calling notifyDataSetChanged() on the adapter like this:
In your Adapter you have some kind of datasource like a list of all items, create a public getter for these items:
public List<ListViewItem> getItems() {
return this.items;
}
Then when you load additional items you get the list from the adapter and add your items:
List<ListViewItem> items = adapter.getItems();
items.addAll(newItems);
And after that you call notifyDataSetChanged() to tell the adapter that you changed the datasource:
adapter.notifyDataSetChanged();

How to get the ListAdapter and update the shown list on an Android Activity Screen

I am setting up a view of a list. I got the list from a remote server call in a AsynchTask call and now in the onPostExecute method I am trying to update the list view with the items I got from the db.
I have some code like this:
ListAdapter l = getListAdapter();
But the ListAdapter does not have the notifyDataSetChanged(); that I need to call to change the original list that was set.
How do I update my list that is shown to the screen?
Thanks!
Depending on which adapter you actually used and which class it extends you may coerce your adapter like this:
((ArrayAdapter<MyClass>) yourListView
.getAdapter()).notifyDataSetChanged();
This is required since only certain adapters have this method.
While I have defined my own adapter like this:
public class MyAdapter extends
ArrayAdapter<MyClass> {...

Android ListAdapter or Possibly ListView Updating Text at Runtime

Hello folkes I have this little problem for which I cannot find a suitable answer looking around the web and on these forums. Please don't direct me to articles in which people have requested list view text color changes at run time, as I read lots of them and not found one to help me out.
I have a simple ListView that displays an array of String objects via the use of a ListAdapter.
I need to update some of ListView Strings at run time, based on their contents. Using a global reference to the list adapter used in the lists views creation I can get the contents of each list view String using following code below.
However, in addition to retrieval I'd like to be able to modify each string in turn, then put it back in the same index position and have the list view reflect the changes. How?
for (int x = 0; x <= listAdapter.getCount();x++)
{
Object o = this.listAdapter.getItem(x);
if (o.getClass().getSimpleName().equals("String"))
{
String s = (String) o;
s = modifyString(s);
//s is the string I want to modify then put back in the same place.
}//end if
}//end for
As far as I know you cannot change the items in an Adapter - unless you are using a custom Adapter (by extending a BaseAdapter etc...)
So, I think you will have to:
make sure you Adapter's constructor takes in the data structure that holds your strings
make sure your data structure is global
make the changes in that data structure whenever you need to
call myAdapter.notifyDataSetChanged();
This will tell adapter that there were changes done in the list and listview should be recreated.
And after your listview is renewed you can even take the user back to the index by:
list.setSelection(positionWhereTheUserClicked);
I hope this helps, let me know if you need more code references.
Here is some code
private ArrayList<String> results = new ArrayList<String>(); //global
private BaseAdapter searchAdapter = new BaseAdapter (results, this); //global
private void updateResults(final ArrayList<String> updatedList){
results = updatedList;
final ListView list = (ListView)findViewById(R.id.search_results);
list.setAdapter(searchAdapter);
list.setOnItemClickListener(new ListView.OnItemClickListener(){
// implementation of what happens when you click on an item //
});
searchAdapter.notifyDataSetChanged();
}
This code works just fine on my end, I hope it helps.
Just stumbled on this problem and found a solution.
I'm using a
m_ListAdapter = new SimpleAdapter(this, m_List, R.layout.option_list_row, columns, renderTo);
Each item in my listView is a manu option causing a dialog to show, once data is received through the dialog, all I have to do is just create a new SimpleAdapter with an updated ArrayList that includes the new data, then just setAdapter to the new adapter.
The ListView will update instantly.

Android: Possible solution for updating the custom list view at random times

I am having a situation where I want to update my Custom List View using BaseAdapter whenever my Database is updated. I have tried calling invalidate() on this Custom List but it didn't work, similarly I even tried having a timer to update my list after sometime, that didn't work either. Please let me know of possible solution.
Update:
This is how I am making my custom list view
li= (ListView)findViewById(R.id.id_lv_row);
ColorDrawable divcolor = new ColorDrawable(Color.DKGRAY);
registerForContextMenu(li);
li.setDivider(divcolor);
li.setDividerHeight(2);
li.setAdapter(new FriendsPositionAdapter(this));
BaseAdapter.notifyDataSetChanged() should do the trick as long as the data behind the adapter actually changed. That's all you need to do to refresh the list.
Invalidate is for repainting views only, you have to tell to the List adapter (BaseAdapter) that dataset has changed.
When the data changes, asign the new dataset to the adapter, and later call notifyDataSetChanged()...
in order to make functional notifyDataSetChanged() the adapter data must be changed. Remember that the original data that change is not reflected automatically to the adapter.
//here i retrieve the new list, named "beans"
lista = (BeanList) result.getDataObject();
Vector<Bean>beans = list.getBeanList();
((BeanListAdapter)listAdapter).syncData(beans);
((BeanListAdapter)listAdapter).notifyDataSetChanged();
//now the syncData method
public void syncData( List<PINPropiedad> newData ){
for(Object o : newData){
add(o);
}
}

Categories

Resources