【发布时间】:2015-03-25 18:16:25
【问题描述】:
我正在尝试从 SQLite 获取数据,然后我想在我的 ListView 上显示它,我想我很困惑或误解了如何正确地做到这一点......我所做的是:
1.-从SQLite 创建一个Insert 到一个Table(这是有效的)。
db.execSQL("INSERT INTO T_OFERTA VALUES (1, 1, 'Offer1', 30, '2x1', '30, 10-1-2013', '1-1-2013', 'Descripció del producte pew', 1, 'http://www.canon.es/Images/Android-logo_tcm86-1232684.png')");
2.-在我的 ListFragment 上,我创建了:1 ImageView、3 Strings 和 1 Integer。这是因为在我的Adapter 上我需要Drawable、String 和Integer。
ImageView FOTO;
String Title, percent, data_f;
Integer Preu;
3.-我做了一个这样的光标:
Cursor c;
c = db.rawQuery("Select NOM_OFER,PREU_OFERTA,DATA_F,FOTO, PERCENTDESCOMPTE from T_OFERTA", null);
c.moveToFirst();
if (c != null) {
do {
for (int i = 0; i < c.getColumnCount(); i++) {
Title = c.getString((c.getColumnIndex("NOM_OFER")));
Preu = c.getColumnIndex("PREU_OFERTA");
percent = c.getString((c.getColumnIndex("PERCENTDESCOMPTE")));
data_f = c.getString((c.getColumnIndex("DATA_F")));
FOTO = c.getString((c.getColumnIndex("FOTO"))); // <--- Error because FOTO it's an ImageView....
Log.e("", "" + c.getString(i));
Toast.makeText(getActivity(), "Title" + Title + "Preu" + Preu + "Percent" + percent + "Cheese is " + data_f, Toast.LENGTH_LONG).show();
}
}while (c.moveToNext());
}
c.close();
现在我必须将变量的数据放入:
mItems.add(new ListViewItem(resources.getDrawable(R.drawable.zumo_oferta), getString(R.string.pew), getString(R.string.pew_precio), getString(R.string.pew_descuento), getString(R.string.pew_data)));
我的ListViewItem 类看起来像:
public class ListViewItem {
public final Drawable icon; // the drawable for the ListView item ImageView
public final String title; // the text for the ListView item title
public final String precio; // the price for the ListView item
public final String descuento; // the price for the discount for the ListView item
public final String date; //the date for the sale for the ListView item
// the text for the ListView item description
public ListViewItem(Drawable icon, String title, String precio, String descuento, String date) {
this.icon = icon;
this.title = title;
this.precio = precio;
this.descuento = descuento;
this.date = date;
}
}
我的ListViewDemoAdapter 看起来像:
public class ListViewDemoAdapter extends ArrayAdapter<ListViewItem> {
public ListViewDemoAdapter(Context context, List<ListViewItem> items) {
super(context, R.layout.listview_item, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null) {
// inflate the GridView item layout
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.listview_item, parent, false);
// initialize the view holder
viewHolder = new ViewHolder();
viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
viewHolder.tvPrice = (TextView) convertView.findViewById(R.id.tvPrice);
viewHolder.tvDiscount = (TextView) convertView.findViewById(R.id.tvDiscount);
viewHolder.tvDate = (TextView) convertView.findViewById(R.id.tvDatas);
convertView.setTag(viewHolder);
} else {
// recycle the already inflated view
viewHolder = (ViewHolder) convertView.getTag();
}
// update the item view
ListViewItem item = getItem(position);
viewHolder.ivIcon.setImageDrawable(item.icon);
viewHolder.tvTitle.setText(item.title);
viewHolder.tvDiscount.setText(item.descuento);
viewHolder.tvPrice.setText(item.precio);
viewHolder.tvDate.setText(item.date);
return convertView;
}
private static class ViewHolder {
ImageView ivIcon;
TextView tvTitle;
TextView tvDiscount;
TextView tvPrice;
TextView tvDate;
}
}
这是因为我想看看获取是否正确。
TL;DR
我没有使用任何BaseAdapter,也没有使用BitMapAdapter,并且由于我必须使用 URL 设置为ImageView,我不知道它是否会起作用,那么我的问题是,我可以按照我现在这样做的方式来做,还是应该创建一个BitMapAdater 而不是Drawable?如果我这样做,我不能使用我得到的字符串作为“FOTO”——>图像,因为它不是Drawable,而是String...
你能指导我正确地做到这一点吗,那是因为它只是一个测试,现在我想用我的 SQLite 的数据设置 ListView。
如果您不理解我的问题或只是需要更多代码,请告诉我,我会尽快回复您。
【问题讨论】:
-
那么,到底是什么问题?
-
如果有超过 1 个产品,如何将 SQLite 中的数据放在 ListView 上?目前我已经插入 1,但我会创建更多,我必须把 mItems.add 放在哪里?由于我的适配器和 SQLite 上有一个 ImageView,所以我有一个字符串(URL),我无法传递数据......知道吗?
标签: java android sqlite listview android-listview