mercredi 28 octobre 2015

CursorAdapter doesn't fetch all data from db using LoaderManager.LoaderCallbacks

I don't know what is wrong with my code, actually there are 20 rows present in SQLite database table, but when I fetch data with LoaderManager.LoaderCallbacks adding data to custom List using CursorAdapter I get 19 rows back first row is never fetched but rest of 19 rows are fetched.
Mycode is

public class RSSAdapter extends CursorAdapter {

    /**
     * Cache of the children views for a list item.
     */
    public static class ViewHolder {
        public final ImageView iconView;
        public final TextView dateView;
        public final TextView descriptionView;            

        public ViewHolder(View view) {
            iconView = (ImageView) view.findViewById(R.id.list_item_icon);
            dateView = (TextView) view.findViewById(R.id.list_item_date_textView);
            descriptionView = (TextView) view.findViewById(R.id.list_item_desc_textView);                
        }
    }

    public RSSAdapter (Context context, Cursor c, int flags) {
        super(context, c, flags);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {            

        View view = LayoutInflater.from(context).inflate(layoutId, parent, false);

        ViewHolder viewHolder = new ViewHolder(view);
        view.setTag(viewHolder);

        return view;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        ViewHolder viewHolder = (ViewHolder) view.getTag();

        // Use placeholder image for now
        viewHolder.iconView.setImageResource(R.mipmap.ic_launcher);    

        viewHolder.dateView.setText(cursor.getString(1);    

        viewHolder.descriptionView.setText(2);    

    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }    
}

RSSFragment

public class RSSFragment extends Fragment implements LoaderManager.LoaderCallbacks{

private ListView listView;
private RSSAdapter rssAdapter;
private String mLocation;
private static final int RSS_LOADER = 404;


public RSSFragment() {
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    getLoaderManager().initLoader(RSS_LOADER, null, this);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}   

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    rssAdapter = new RSSAdapter(getActivity(), null, 0);

    Log.e("onCreateView()", "List Adapter is of " + rssAdapter.getCount() + " size");

    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    listView = (ListView) rootView.findViewById(R.id.listView_rss);
    listView.setAdapter(rssAdapter);

    return rootView ;
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {        

    Uri rssUri = Util.getRSSUri();

    Log.e("RSS_FRAGMENT", "Uri : " + rssUri.toString() );

    CursorLoader cursorLoader = new CursorLoader(getActivity(), rssUri, columnc, null, null, sortOrder);

        Log.d("ONCREATE_LOADER()", "Called with null");
        return cursorLoader;

}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    Log.e("Loader<Cursor>", "Size of Cursor is " + data.getCount());
    rssAdapter.swapCursor(data);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {        
    rssAdapter.swapCursor(null);
}

}

the Log from method above

 @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        Log.e("Loader<Cursor>", "Size of Cursor is " + data.getCount());
        rssAdapter.swapCursor(data);
    }

prints

E/Loader<Cursor>  Size of Cursor is 19

means skipping first row/record and fetching/showing rest 19 rows/records. I've tried a ton of times but can not resolve the issue. Please any help is like great favor.

Aucun commentaire:

Enregistrer un commentaire