gpt4 book ai didi

java - 使用从游标获取的数据使用 arrayList 填充 ListView

转载 作者:太空宇宙 更新时间:2023-11-04 12:48:45 25 4
gpt4 key购买 nike

Helo 我正在尝试使用 sqLite 中存储的数据填充 listView。 选择产品后,我希望该产品的所有引用信息 沿着我在图中画的同一条线走。 enter image description here

我可以让ArrayAdapter把所有的 在同一个 xml 中记录?

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

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 添加数据:

    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 中我写了这个:

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 传递给适配器,因此无需维护任何集合。你必须遵循一些事情

适配器

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

}
}

从数据库中检索记录

公共(public)光标 getAllRows() { 字符串 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

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

关于java - 使用从游标获取的数据使用 arrayList<String> 填充 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36056134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com