Android - 从 SQLite 中获取数据并显示在 ListView 中
Android - Fetch data from SQLite and display in ListView
我正在尝试从 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
class 看起来像:
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。
如果您不明白我的问题或只是需要更多代码,请告诉我,我会尽快回复您。
我将更改以下内容:
为方便起见,在检索时创建 ListViewItem
:
ArrayList myItems = new ArrayList<ListViewItem>();
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")));
// create and add your items as they are retrieved from the db
myItems.add(new ListViewItem(...,Title, Preu, percent, data_f));
...
} ...
现在您已经从数据库中获取了所有项目,您可以轻松地将它们传递到您的 ListView
/adapter
:
myAdapter = new ListViewDemoAdapter(getActivity(), myItems);
setListAdapter(myAdapter);
我不会在您的 ListViewItem
中存储 Drawable
,而只是将资源 ID 存储为整数:
int icon = R.drawable.example_icon;
然后在适配器的 getView()
方法中处理到实际图像资源的转换:
ivIcon.setImageResource(item.icon);
我正在尝试从 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
class 看起来像:
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。
如果您不明白我的问题或只是需要更多代码,请告诉我,我会尽快回复您。
我将更改以下内容:
为方便起见,在检索时创建
ListViewItem
:ArrayList myItems = new ArrayList<ListViewItem>(); 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"))); // create and add your items as they are retrieved from the db myItems.add(new ListViewItem(...,Title, Preu, percent, data_f)); ... } ...
现在您已经从数据库中获取了所有项目,您可以轻松地将它们传递到您的 ListView
/adapter
:
myAdapter = new ListViewDemoAdapter(getActivity(), myItems);
setListAdapter(myAdapter);
我不会在您的
ListViewItem
中存储Drawable
,而只是将资源 ID 存储为整数:int icon = R.drawable.example_icon;
然后在适配器的 getView()
方法中处理到实际图像资源的转换:
ivIcon.setImageResource(item.icon);