I'am loading data into listview from cursors by initiating multiple loaders. I want my listview to show the data from all cursors but data in listview is changing immediately with a 2 seconds interval and showing data of only last cursor. Please help me. Where iam going wrong? Do I have to merge the cursors?
Inside onCreateView() of my fragment :
for (int i = 0; i < idsArray.length; i++) {
Bundle b = new Bundle();
b.putInt("id", idsArray[i]);
getLoaderManager().initLoader(idsArray[i], b, this); //initiating multiple loaders
}
adapter = new CustomAdapter(getActivity(), R.layout.list_row, null, column_names_array, ui_ids_array, 0);
listView.setAdapter(adapter);
And loader callback methods :
@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle b) {
Builder uriBuilder = MY_PROVIDER_URI.buildUpon();
uriBuilder.appendPath("categories");
uriBuilder.appendPath(String.valueOf(b.getInt("id")));
uriBuilder.appendPath("data");
return new CursorLoader(getActivity(), uriBuilder.build(), null, null, null, null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
if(adapter != null && data != null){
adapter.swapCursor(data);
adapter.notifyDataSetChanged();
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
adapter.swapCursor(null);
}
And here is my adapter class :
public class CustomAdapter extends SimpleCursorAdapter {
Cursor cursor;
Context context;
Activity activity;
int layout;
public CustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags ) {
super(context, layout, c, from, to, flags);
this.cursor = c;
this.context = context;
this.activity = (Activity) context;
this.layout = layout;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(this.layout, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
ViewHolder holder = (ViewHolder) view.getTag();
if (holder == null) {
holder = new ViewHolder();
//finding UI elements in list row here
view.setTag(holder);
}
//binding data to views of viewholder here
}
static class ViewHolder {
//variables of all my UI elements
}
}
Your loop :
for (int i = 0; i < idsArray.length; i++) {
Bundle b = new Bundle();
b.putInt("id", idsArray[i]);
getLoaderManager().initLoader(idsArray[i], b, this); //initiating multiple loaders
}
will result in creating multiple Loaders, once a Loader has finished loading its data, the method
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
if(adapter != null && data != null){
// This line result in replacing the last Cursor by the new one
adapter.swapCursor(data);
adapter.notifyDataSetChanged();
}
}
is invoked. When the last Loader has finished loading the Cursor, its data will replace the last Cursor because of swapCursor.
If you look at the CursorAdapter documentation, you will notice that this CursorAdapter can only manage one Cursor at a time.
To resolve your problem, you should try to query all the wanted data into a single query, resulting in a single Cursor.