游标适配器只拉入第一行

时间:2016-03-08 18:17:50

标签: android listview cursor android-cursoradapter

我目前正在尝试使用游标适配器来显示sqlite数据库中的项目,但它只显示第一行。

  

查询

Cursor res = db.rawQuery("SELECT  * FROM " +TABLE_NAME, null);
  

光标功能

public class entryCursorAdapter extends CursorAdapter {
    public   entryCursorAdapter(Context context, Cursor res, int flags) {
        super(context, res, flags);
    }

    // The newView method is used to inflate a new view and return it,
    // you don't bind any data to the view at this point.
    @Override
    public View newView(Context context, Cursor res, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.display_data_layout, parent, false);
    }

    // The bindView method is used to bind all data to a given view
    // such as setting the text on a TextView.
    @Override
    public void bindView(View view, Context context, Cursor res) {
        // Find fields to populate in inflated template
        TextView entryveg = (TextView) view.findViewById(R.id.id);
        TextView entrypeg = (TextView) view.findViewById(R.id.name);
        TextView entryfoodname = (TextView) view.findViewById(R.id.type);


        // Extract properties from cursor
        int id = res.getInt(0);
        String veg = res.getString(1);
        String peg = res.getString(2);

        // Populate fields with extracted properties

        entryid.setText(String.valueOf(id));
        entryveg.setText(veg);
        entrypeg.setText(peg);

    }

}
  

调用ListView

    entryItems = (ListView) findViewById(R.id.displayview);
    entryCursorAdapter entryAdapter = new    entryCursorAdapter(getActivity(), res, 0);
    entryItems.setAdapter(entryAdapter);

0 个答案:

没有答案