使用arrayList< String>填充ListView.从游标获得的数据 [英] Populate ListView with an arrayList<String> with data obtained from a cursor

查看:66
本文介绍了使用arrayList< String>填充ListView.从游标获得的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我正在尝试使用存储在sqLite中的数据填充listView. 选择产品后,我想要该产品的所有参考 就像我在图片中画的一样.

Helo i am trying to populate a listView with data stored in a sqLite. After i select a product i want all the reference of that product to go on the same line like i drew in the picture.

我可以让ArrayAdapter放置所有 记录在相同的xml中?

Can i make the ArrayAdapter put all records in the same xml?

我的代码如下:返回所有记录的游标:

My code looks like this: The cursor that returns all records:

public Cursor getAllRows() {
    String where = null;

    // query the DBAdapter
    Cursor cursor = db.query(true, TABLE_NAME, ALL_KEYS, where, null, null, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
    }
    return cursor;
}

将数据添加到arrayList:

Adding data to arrayList:

    public ArrayList<String> fromCursorToArrayListString(Cursor c){
    ArrayList<String> result = new ArrayList<String>();
    c.moveToFirst();
    for(int i = 0; i < c.getCount(); i++){

        String row_PRODUCT = c.getString(c.getColumnIndex(KEY_PRODUCT));
        String row_PRICE = c.getString(c.getColumnIndex(KEY_PRICE));
        String row_TYPE = c.getString(c.getColumnIndex(KEY_TYPE));

        result.add(row_PRODUCT);
        result.add(row_PRICE);
        result.add(row_TYPE);
        c.moveToNext();
    }
    return result;
}

在mainActivity中,我这样写:

In the mainActivity i wrote this:

ListView listView = (ListView) findViewById(R.id.new_list_listView);
Cursor cursor = newListDBAdapter.getAllRows();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,R.layout.custom_product_layout_new_list,R.id.custom_product_layout_new_list_productName,newListDBAdapter.fromCursorToArrayListString(cursor));
listView.setAdapter(arrayAdapter);

推荐答案

CursorAdapter适配器对此更可靠.您只需将Cursor传递给适配器,就无需维护任何集合.您必须遵循几件事

CursorAdapter adapter more reliable for this . you just pass your Cursor to adapter so you no need to maintain any collection . you must follow few things

适配器

 public class YourCursorAdapter extends CursorAdapter {
      public YourCursorAdapter(Context context, Cursor cursor, int flags) {
          super(context, cursor, 0);
      }

      // 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 cursor, ViewGroup parent) {
          return LayoutInflater.from(context).inflate(R.layout.your_item_view, 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 c) {
          // Find fields to populate in inflated template
          TextView tvproduct = (TextView) view.findViewById(R.id.tcproduct);
          TextView tvPrice = (TextView) view.findViewById(R.id.tvPrice);
          TextView tvType = (TextView) view.findViewById(R.id.tvType);
          // Extract properties from cursor
          String row_PRODUCT = c.getString(c.getColumnIndex(KEY_PRODUCT));
        String row_PRICE = c.getString(c.getColumnIndex(KEY_PRICE));
        String row_TYPE = c.getString(c.getColumnIndex(KEY_TYPE));

          // Populate fields with extracted properties
          tvproduct.setText(row_PRODUCT);
           tvPrice.setText(row_PRICE);
           tvType.setText(row_TYPE);

      }
    }

从数据库中检索记录

公共游标getAllRows(){ 字符串,其中= null;

public Cursor getAllRows() { String where = null;

// query the DBAdapter
Cursor cursor = db.query(true, TABLE_NAME, ALL_KEYS, where, null, null, null, null, null);
if (cursor != null) {
    cursor.moveToFirst();
}
return cursor;

}

现在将光标设置为适配器,将适配器设置为listview

Now set cursor to your adapter ,adapter set to listview

ListView listView = (ListView) findViewById(R.id.new_list_listView);
Cursor cursor = newListDBAdapter.getAllRows();
YourCursorAdapter  arrayAdapter = new YourCursorAdapter(this, cursor); 
listView.setAdapter(arrayAdapter );

这篇关于使用arrayList&lt; String&gt;填充ListView.从游标获得的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆