Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20656

Android - Fetch data from SQLite and display in ListView

I'm trying to fetch data from SQLite and then I want to display it on my ListView, I think I'm confused or misunderstanding how to do it properly... The thing that I've done is :

1.-Create an Insert into a Table from SQLite (This is working).

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.-On my ListFragment I've created : 1 ImageView, 3 Strings and 1 Integer. This is made because on my Adapter I need a Drawable, String and Integer.

 ImageView FOTO;
 String Title, percent, data_f;
 Integer Preu;

3.-I made a cursor that is like this :

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

Now I've to put the data of my variables inside of :

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

My ListViewItem class looks like:

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

And my ListViewDemoAdapter looks like :

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

This is made because I wanted to see if the fetch was doing correctly.

TL;DR

I'm not using any BaseAdapter, neither a BitMapAdapter, and since I've to use an URL to set as ImageView I don't know if it would work, then my question is, can I do it by the way I'm doing this at the moment, or shall I create a BitMapAdater instead of Drawable? If I do by this way, I can not use the string that I get as a "FOTO" --> Image, because it is not a Drawable and it's a String... Can you guide to me to do this properly, that was make because it was just a test, now I want to set the ListView with data of my SQLite.

If you didn't understand my question or just need more code, let me know I'll reply you as soon as possible.

Upvotes: 0

Views: 857

Answers (1)

bwegs
bwegs

Reputation: 3767

I'd change the following:

  1. To make it easier, create ListViewItems as they are retrieved:

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

Now that you've got all the items out of your database you can easily pass them into your ListView/adapter:

myAdapter = new ListViewDemoAdapter(getActivity(), myItems);
setListAdapter(myAdapter);
  1. I wouldn't store a Drawable in your ListViewItem, instead just store the resource id as an integer:

    int icon = R.drawable.example_icon;
    

Then handle the conversion to the actual image resource in your adapter's getView() method:

ivIcon.setImageResource(item.icon);

Upvotes: 1

Related Questions