Reputation: 11109
I have a Fragment, with GridView loaded using CustomAdapter(extending BaseAdapter)
. The data is retrieved from Server, and hence, I am trying to call notifyDataSetChanged on the adapter, once the download is complete. But, the GridView doesn't load the content after download.
Here are few of my code snippets:
GridView gridview;
CustomAdapter mCustomAdapter;
List<Item> listItems = new ArrayList<Item>();
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
....
....
mCustomAdapter = new CustomAdapter(activity, listItems);
gridview = (GridView) view.findViewById(R.id.gridview);
gridview.setAdapter(mCustomAdapter);
// This is the call for retrieving data from Server
checkRefreshTimeAndGetData();
return view;
}
After getting data, i am calling notifyDataSetChanged() in the below method:
public void checkRefreshTimeAndGetData(){
....
....
listItems.clear();
if (!mMainTableDbAdapter.isTableEmpty(
prefs.getInt(Const.Prefs.MAIN_INDEX, DEFAULT_VALUE),
prefs.getInt(Const.Prefs.SUB_INDEX, DEFAULT_VALUE))){
listItems = mMainTableDbAdapter.getTitles(
prefs.getInt(Const.Prefs.MAIN_INDEX, DEFAULT_VALUE),
prefs.getInt(Const.Prefs.SUB_INDEX, DEFAULT_VALUE));
}
// At this point, i checked if listItems has values,
// and it has 100 items in it. So, its not empty.
mCustomAdapter.notifyDataSetChanged();
}
CustomAdapter:
public class CustomAdapter extends BaseAdapter {
private List<Item> items;
private LayoutInflater vi;
private List<Boolean> selectedState = new ArrayList<Boolean>();
String imagepath = "";
int curPos = 0;
public CustomAdapter(Context context, List<Item> items) {
this.items = items;
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView,
final ViewGroup parent) {
....
....
}
}
What is it that I am doing wrong? if any other data is needed, please let me know.
Upvotes: 1
Views: 4573
Reputation: 66
you need to implement a listener to know when data downloaded. like implement an interface which will get notification when you successfully get data from server and then you call notifyDatasetChanged()
in current scenario your method will be called only when you start your fragment. not when data downloaded.
Upvotes: 0
Reputation: 83028
The reason your code is not working:
You are not updating adapter with latest data. In your checkRefreshTimeAndGetData
, you take new data but you did not given that data to adapter. So mCustomAdapter.notifyDataSetChanged()
is not doing any thing.
For this you can add one more method into adapter which will update items into adapter.
public void updateItems(List<Item> newItems) {
// Either you can add or replace all data
// Here I am replacing all data with new
this.items = newItems;
notifyDataSetChanged();
}
and call this method into checkRefreshTimeAndGetData
at place of mCustomAdapter.notifyDataSetChanged()
mCustomAdapter.updateItems(listItems);
Upvotes: 2
Reputation: 47817
Here is one way out
make refresh(..)
in your Adapter
public void refresh(List<Item> items)
{
this.items = items;
notifyDataSetChanged();
}
and used it like
listItems.clear();
mCustomAdapter.refresh(listItems); //pass update list
Upvotes: 6