public void UpdateData(ArrayList<HashMap<String,String>> array_list){
GridView glist = (GridView) findViewById(R.id.tipss_grid)
adapter2 =new CurrentAdapter(CurrentChanels.this,array_list);
glist.setAdapter(adapter2);
}
I call this method to populate data in gridview. I m displaying currently running programs. After every 1 min I call this method to refresh the data. The problem is that when user is on the last element of gridview and mean while I refresh it then control move to top of the screen. I do not want the screen to move,it must stay where it is before refresh. Any suggestions?
This is because every one minute you are creating a new Adapter and assigning it to the GridView.
Implement a new method resetData() in CurrentAdapter:
public void resetData(List<HashMap<String,String>> list) {
_list.clear();
_list.addAll(list);
notifyDataSetChanged();
}
Call resetData() whenever you want to refresh the grid:
GridView glist = (GridView) findViewById(R.id.tipss_grid);
if (glist.getAdapter() == null) {
CurrentAdapter adapter2 = new CurrentAdapter(CurrentChanels.this,
array_list);
glist.setAdapter(adapter2);
} else {
CurrentAdapter adapter2 = ((CurrentAdapter)glist.getAdapter());
adapter2.resetData(array_list);
}
use this line after setadapter line
glist.setSelection(adapter2.getCount() - 1) ;
You can call adapter.notifyDataSetChanged() but if you creating new ArrayList you have to set new adapter. You should change data in old ArrayList if you want notifyDataSetChanged() work.
Related
I am trying to set up a gridview that will refresh when a new search is executed.
this is my call to set up the gridview
final GridElement_Results adapter = new GridElement_Results(this, savedInstanceState, track_name, album_name, track_num, album_id, album_link);
grid = (GridView)findViewById(R.id.grid_results);
grid.setAdapter(adapter);
As you can see, I am passing it a bunch of arrays.
On a button click, the function getTracks() is called which gets new data from the server. The arrays are updated with the new data, which I have verified is working.
icon_activesearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getTracks("newsearchterm");
adapter.notifyDataSetChanged();
grid.invalidateViews();
}
}
);
And then I call adapter.notifyDataSetChanged() and grid.invalidateViews() to update and redraw the grid. But obviously I'm doing something wrong here.
Is it because I'm setting the adapter as final? To access it within the onClick method I need to set it to final.
What should I change to get it to work.
you should wait until new data fetch then notify adapter but you call notify just after getTracks() without any delay
I'm attempting to get method to handle updates to the ListView when SetWeatherData is called. Nothing ever shows up in my listview below. Any ideas? _rootView points to the right root and ListView comes back not null. m_weatherdata has a couple string elements in it.
Note the initial set of data does not show up either. Just blank.
I'm thinking it should be easier to setup a generic method to update a ListView when the data changes using straight up code.
private ArrayList<String> m_weatherdata;
private void SetWeatherData ( ArrayList<String> _weather)
{
m_weatherdata = _weather;
UpdateWeatherUI();
return;
}
ArrayAdapter<String> m_adapter = null;
private void UpdateWeatherUI()
{
if ( m_adapter == null ) {
m_adapter = new ArrayAdapter<String>(
this.getContext(),
R.layout.list_item_forecast,
R.id.list_item_forecast_textview,
m_weatherdata);
View _rootview = this.getLayoutInflater(null).inflate(R.layout.fragment_main, null, false);
ListView _listview = (ListView) _rootview.findViewById(R.id.listview_forecast);
_listview.setAdapter(m_adapter);
}
else
{
m_adapter.notifyDataSetChanged();
}
}
You are assigning a new ArrayList to your dataset.
m_weatherdata = _weather;
Instead add items to the dataset. Like this
m_weatherdata.addAll(_weather);
private void SetWeatherData ( ArrayList<String> _weather)
{
m_weatherdata.addAll(_weather);//change here
UpdateWeatherUI();
return;
}
When you set an adapter there is an observer attached to the
underlying data. So notifyDatasetChanged() only works if you only
modify the data in it.
If you want to clear all data from your dataset before adding new items to it, use the clear() method of ArrayList
private void SetWeatherData ( ArrayList<String> _weather)
{
m_weatherdata.clear();//change here
m_weatherdata.addAll(_weather);//change here
UpdateWeatherUI();
return;
}
m_weatherdata = _weather; // updates the local variable with new set of data. but adapter doesn't know about the changes made as you have created the instance of the adapter with array list by the following line of code.
m_adapter = new ArrayAdapter<String>(this.getContext(), R.layout.list_item_forecast,R.id.list_item_forecast_textview,m_weatherdata);
In you want to updated the data either call
m_adapter.addAll(newsetofstringtobeadded);
or
Create new adapter
This will update your list.
adapter = new ImageAdapter(this, imagelist, imageBool, resourceName, docBool);
adapter.NotifyDataSetChanged();
//gridview.Adapter = null;
gridview.InvalidateViews();
gridview.Adapter = adapter;
I send the list with the new values to the imageadapter and it sorts the values out and returnes the items as images and alls is good. But when i want to refresh the items the old ones just stays the same and the new ones never arrives on the screen. Ive tried a lot of different ways to reload the grid but it doesn't work.
Add following methods to your customized adapter ie. ImageAdapter.
// Call it once , first time when you want to pass the data to adapter
public void setImageList(List<YOUR DATA TYPE> imagelist) {
this.imagelist = imagelist;
}
// Call this method whenever new data is to be added to existing list.
public void updateImageList(List<YOUR DATA TYPE> newImagelist) {
if(this.imagelist != null){
this.imagelist.addAll(newImagelist);
}else{
this.imagelist = imagelist;
}
notifyDataSetChanged();
}
And call it once the new data is downloaded.
Just create a new ImageAdapter with all the Images (including the old ones and the new ones) and assign it to the gridview. This way it works without problems.
After that call
adapter.notifyDataSetChanged();
and it will work fine.
List view is refreshed every N minutes of interval using a web service call. After refresh my current position goes back to the first item in list view.
Here is my code.
private void updateListUI(String response){
arrList = new ArrayList<Object>(); // to store array list
Object obj;
obj = some data; // some parsing of data and store in obj instance
arrList.add(obj);
if(listview.getAdapter()==null){
adapter = new customAdapter(myActivity,layout,arrList);
listview.setAdapter(adapter);
}else{
HeaderViewListAdapter adapterwrap =
(HeaderViewListAdapter) listview.getAdapter();
BaseAdapter basadapter = (BaseAdapter) adapterwrap.getWrappedAdapter();
customAdapter ad = (customAdapter)basadapter;
ad.setData(arrList); // it is a func in custom adapter that add items
ad.notifyDataSetChanged();
// here i want to set the position of list view,as it goes back to first item
}
}
BroadcastReceiver serviceReceiver = new BroadcastReceiver() {
public void onReceive( final Context context, final Intent intent ) {
updateListUI(intent.getStringExtra(response_from_service));
}
}
// Intent service calls webservice and broadcast response to receiver as finishes call. My question is on every updateListUI call list view goes back to first position. Is any way to solve this..?
use listLead.setSelectionFromTop(index, top); to set the position in list view
You can restore the posistion of your ListView using:
int position = 12; // your position in listview
ListView listView = (ListView) findViewById(R.id.listView);
listView.setSelectionFromTop(position, 0);
Instead of loading setAdapter during refresh
use the below code ... it will work
yourAdapterName.notifyDataSetChanged();
Just use the method notifyDataSetChanged() at the point where you want to refresh your list view.
I have a listview in which i am adding some data after fixed interval.but I don't want to set the adapter again as it will refresh the complete list.Is there any method to add item without refreshing the complete list.
Thanks in advance.
You probably want to use the following (in RecycleView not ListView):
notifyItemInserted(0);//NOT notifyDataChanged()
recyclerViewSource.scrollToPosition(0);
//Scroll up, to use this you'll need an instance of the adapter's RecycleView
You can call adapter.notifyDataSetChanged() to just update the list.
Adapter's getView() is called at different times and there is no particular pattern. So your views are updated whenever ListView wants it to be updated.
But as far as I see it, you are looking for adapter.notifyDataSetChanged. The workflow should be something like this.
Set adapter to ListView
Add data to adapter`
Call notifyDataSetChanged() on adapter.
It will at least prevent your list to bounce back to first item on the list.
Hope that helps.
you can use this .notifyDataSetChanged()
However notifyDataSetChanged() only works For an ArrayAdapter,if you use the add, insert, remove, and clear functions on the Adapter.
You can use
adapter.add(<new data item>); // to add data to your adapter
adapter.notifyDatasetChanged(); // to refresh
For eg.
ArrayAdapter<String> adapter;
public void onCreate() {
....
....
ArryList<String> data = new ArrayList<String>();
for(int i=0; i<10; i++) {
data.add("Item " + (i+1));
}
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
setListAdapter(adapter);
}
Now whenever you have new data to be added to list you can do following
private void appendToList(ArrayList<String> newData) {
for(String data : newData)
adapter.add(data);
adapter.notifyDatasetChanged();
}