Update Listview Dynamically with Adapter

update listview dynamically with adapter

Use a ArrayAdapter backed by an ArrayList. To change the data, just update the data in the list and call adapter.notifyDataSetChanged().

Android - Dynamically Updating a custom ListView after items in an ArrayList are added

Your adapter does not get the new data, because you are initializing it with its own set of data.

One possibility would be to instantiate a new adapter and assign it to the ListView.

Add a field for your ListView in your activity:

public TextView tv;
private int variantPosition;
CustomListAdapter customListAdapter;
CurrentOrderClass currentOrder = new CurrentOrderClass();
ListView myListView; //Listview here

In onCreate, set myListView to point to your ListView:

final ListView lv1 = (ListView) findViewById(R.id.listViewProductOrder)
myListView = lv1;

Finally, when you change your data, create a new Adapter for the new data:

myListView.setAdapter(new CustomListAdapter(this, getListData());

Alternatively, modify your Custom adapter to contain a setListData method:

public class CustomListAdapter extends BaseAdapter {

private ArrayList<CurrentOrderClass> listData;

private LayoutInflater layoutInflater;

public CustomListAdapter(Context context, ArrayList<CurrentOrderClass> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
}

public void setListData(ArrayList<CurrentOrderClass> data){
listData = data;
}

@Override
public int getCount() {
return listData.size();
}

@Override
public Object getItem(int position) {
return listData.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
holder = new ViewHolder();
holder.variantView = (TextView) convertView.findViewById(R.id.variant);
holder.unitView = (TextView) convertView.findViewById(R.id.unit);
holder.quantityView = (TextView) convertView.findViewById(R.id.quantity);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

holder.variantView.setText(listData.get(position).getVariantArray().get(position).toString());
holder.unitView.setText(listData.get(position).getUnitArray().get(position).toString());
holder.quantityView.setText(String.valueOf(listData.get(position).getQuantityRow()));

return convertView;
}

static class ViewHolder {
TextView variantView;
TextView unitView;
TextView quantityView;
}

}

Then, after modifying your data, just call:

customListAdapter.setListData(getListData());
customListAdapter.notifyDataSetChanged();

Android How to dynamically update a listview?

Take a look at the answer to this question for an explanation / example of how to maintain scroll position after adding items to a list: Retaining position in ListView after calling notifyDataSetChanged

Basically, you make a note of the position and scroll offset of the first visible item in the list before adding new data. Then afterwards, you restore the previous position and scroll offset.

However, because you're adding new items at the top of the list, the position returned by getFirstVisiblePosition() may refer to a different item after refreshing. The item that was at position 0 before refreshing may now be position 10. To fix that, you need to determine the number of new items returned by timeline.php and add it to index, like this:

mList.setSelectionFromTop(index + newItemCount, top);

You could probably determine which items are new by comparing the date field.

(By the way, if timeline.php only returns the most recent set of items, you could run into trouble if the user has scrolled to an older item which is no longer returned by timeline.php. After setting the new data, the old item will no longer be present, so you won't be able to scroll to it.

To fix that, you could keep the old ArrayList around and only add new items to it in doInBackground(). And call notifyDataSetChanged() in doInBackground(). That way, the adapter will still have access to older items.)

How to update listView data of an Adapter on deletion of item dynamically?

The way adapters work is that it takes an arraylist as a parameter, and converts it into a "representable" list to be viewed in a ListView.
The way "notifyDataSetChanged" works, is that it "is" called on an adapter (that is true), however since you are calling it inside your adapter class then the compiler is reading it this way [ this.notifyDataSetChanged ] , where (this here resembles the adapter).

Now for your question. Every time you accept or deny an item, you apply the appropriate changes to the Firebase database, why this issue is arising is that as I said earlier all that notifyDataSetChanged does is that it checks the changes in the ArrayList and "Refreshes" itself and the listView basically to apply these changes. Try to access the ArrayList that is passed in the constructor of your custom array adapter, and in the denyButton click listener, when you remove the request from the request looper, also remove it from the arrayList.

How to update Android ListView with dynamic data in real time?

I experimented with ListView, and you essentially have to update the ListView cells manually without calling notifyDataSetChanged() if you have realtime data and you want the ListView to update with better performance.

notifyDataSetChanged() causes the ListView to rebuild its entire View hierarchy is very slow if you are calling it frequently (i.e. once every second).

Note (2014). This DOES NOT APPLY if you are using normal modern ListView with a ViewHolder pattern. You simply call 'notifyDataSetChanged' and that's all there is to it. It is incredibly efficient as Android knows to only update the cells on the screen.

I implemented a scheme where if my data set size changed from one update to the next, I call notifyDataSetChanged(). If the data set size remained constant from one update to the next (such that the number of cells in the ListView is the same as before when a redraw of the data is needed), then I iterate over the ListView.getFirstVisiblePosition() : getLastVisiblePosition(), and update the visible cells only.

Updating Recycler Listview dynamically using Database

You're not updating the myNotesList that is in adapter class but in activity class. But the adapter uses it's local myNotesList.

So on button click, update myNotesList of adapter with latest data available and notify the adapter.

EDIT
Pass the latest data to adapter. Have this method in adapter class and call this before notifyDataSetChanged();

public void updateNotes(ArrayList<String> notesList) {
myNotesList = notesList;
}

Update adapter items with add dynamic drawable on getview() lead to show wrong data

You have to select data from db before adapter initialization. So that

getItem(position) 

will return already a "ready" item-object.

You shouldn't set the values to Views inside

if (convertView == null) {
...
}

This code is only for a viewHolder initialization. You create a new one, if convertView is null or read it as tag.
Setting of values you have to do after viewHolder initialization, actually where you set the title.
But in order to increase a performance, you shouldn't select the values from db on each step of getView. You have to have everything prepared (already selected).



Related Topics



Leave a reply



Submit