Android - 从SQLite获取数据并在ListView中显示

时间:2015-03-25 18:16:25

标签: java android sqlite listview android-listview

我正在尝试从SQLite中获取数据然后我想在我的ListView上显示它,我想我很困惑或误解了如何正确地做到这一点......我做过的事情是:

1. - 从Insert Table创建一个SQLite(这是有效的)。

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我需要DrawableStringInteger

 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,因为我要使用网址设置为ImageView我不知道是否会有用,那么我的问题是,我可以按照我现在这样做的方式来做,或者我应该创建一个BitMapAdater而不是Drawable?如果我通过这种方式,我不能使用我得到的字符串作为“FOTO” - &gt;图片,因为它不是Drawable而且是String ... 您是否可以指导我正确执行此操作,因为它只是一个测试,现在我想用我的SQLite数据设置ListView。

如果您不理解我的问题或只是需要更多代码,请告诉我,我会尽快回复您。

1 个答案:

答案 0 :(得分:1)

我改变了以下内容:

  1. 为方便起见,请在检索时创建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));
    
        ...
    } ...
    
  2. 现在您已经从数据库中获取了所有项目,您可以轻松地将它们传递到ListView / adapter

    myAdapter = new ListViewDemoAdapter(getActivity(), myItems);
    setListAdapter(myAdapter);
    
    1. 我不会在Drawable中存储ListViewItem,而只是将资源ID存储为整数:

      int icon = R.drawable.example_icon;
      
    2. 然后在适配器getView()方法中处理转换为实际图像资源:

      ivIcon.setImageResource(item.icon);