CursorAdapter不使用LoaderManager.LoaderCallbacks

时间:2015-10-29 03:45:46

标签: java android sqlite

我不知道我的代码有什么问题,实际上SQLite数据库表中有20行,但是当我用LoaderManager.LoaderCallbacks获取数据时,使用CursorAdapter将数据添加到自定义List我得到第19行的第19行是永远不会获取但是其余的19行都被取出。
我的代码是

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.java类是

public class RSSFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>{

    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);
    }
}

上面的Log from方法

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

打印

E/Loader<Cursor>  Size of Cursor is 19

表示跳过第一行/记录并获取/显示其余19行/记录。 我尝试了很多次但无法解决问题。 请大家帮帮忙。

0 个答案:

没有答案