如何检索图像数据并将其显示为ListView

时间:2015-03-21 15:14:11

标签: java android listview bitmap

我正在尝试检索已经从字节转换为位图的图像数据。但是没有显示任何图片,而是显示图片。在logcat中,我可以清楚地看到我的图像数据变成了位图。

这是我保存mydata的java代码

        items Item= new items();
        db.getWritableDatabase();
        Item.setName(name.getText().toString());
        Item.setCategory(SpCate.getSelectedItem().toString());
        Item.setDetails(details.getText().toString());
        Item.setImage(imageInByte);
        db.addItemSpinner(new items());
            // Save the Data in Database
          MyDb entry= new MyDb(SellingItem.this);
          entry.getWritableDatabase();
          entry.addItemSpinner(Item);
          entry.close();
          Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
            Log.d("name", Item.getName());
            Log.d("category", Item.getCategory().toString());
            Log.d("details", Item.getDetails());
            Log.d("image", Item.getImage().toString());
            Intent i=new Intent(SellingItem.this,SearchItem.class);
            startActivity(i);

这是我的数据库,用于检索数据。

 public List<items> getAllContacts() {
            List<items> itemlist = new ArrayList<items>();
            // Select All Query
            String selectQuery = "SELECT  * FROM PRODUCT ORDER BY name";
            SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery(selectQuery, null);
            // looping through all rows and adding to list
            if (cursor.moveToFirst()) {
                do {

                    items item = new items();
                    item.setId(Integer.parseInt(cursor.getString(0)));
                    item.setName(cursor.getString(1));
                    item.setImage(cursor.getBlob(2));
                    // Adding contact to list
                    itemlist.add(item);

                } while (cursor.moveToNext());
            }
            // close inserting data from database
            db.close();
            // return contact list
            return itemlist;

        }

这是我在listview中显示数据的java代码

 dataList = (ListView) findViewById(R.id.list);
        db = new MyDb(this);
        List<items> Item = db.getAllContacts();
        for (items cn : Item) {
            String log = "Id:" + cn.getId() 
                    + " ,Image: " + cn.getImage();

            // Writing Contacts to log
            Log.d("Result: ", log);
            // add contacts data in arrayList
            imageArray.add(cn);

        } 
           adapter = new CustomListAdapter(this, R.layout.screen_list,
              imageArray);
            dataList.setAdapter(adapter);
            dataList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                 imageName = imageArray.get(position).getImage();
                 imageid = imageArray.get(position).getId();
                 //convert byte to bitmap
                 ByteArrayInputStream imageStream = new ByteArrayInputStream(
                 imageName);
                    theImage = BitmapFactory.decodeStream(imageStream);
                    Intent intent = new Intent(SearchItem.this,
                           DisplayDetails.class);
                    intent.putExtra("imageid", imageid);
                    intent.putExtra("imagename", theImage);
                    startActivity(intent);

            }
    }); 

这是logcat,它显示了可以读取的图像位图和id。

Id:2 ,Image: [B@428e21d8

我不知道我的代码有什么问题,但我需要在listview中查看该图像。任何帮助将不胜感激。

这是我的CustomListAdapter

package com.example.projectonline;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomListAdapter extends ArrayAdapter<items>{
    ArrayList<items> data= new ArrayList<items>();
    Context context;
    int layoutResourceId;

     public CustomListAdapter(Context context, int layoutResourceId, ArrayList<items> data) {
            super(context, layoutResourceId, data);
            this.layoutResourceId = layoutResourceId;
            this.context = context;
            this.data = data;
        }

     public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            ImageHolder holder = null;

            if(row == null)
            {
                LayoutInflater inflater = ((Activity)context).getLayoutInflater();
                row = inflater.inflate(layoutResourceId, parent, false);

                holder = new ImageHolder();
                holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
                holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
                row.setTag(holder);
            }
            else
            {
                holder = (ImageHolder)row.getTag();
            }

            items picture = data.get(position);
            holder.txtTitle.setText(picture.name);
            //convert byte to bitmap take from contact class

            byte[] outImage=picture.image;
            ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
            Bitmap theImage = BitmapFactory.decodeStream(imageStream);
            holder.imgIcon.setImageBitmap(theImage);
            return row;

        }

        static class ImageHolder
        {
            ImageView imgIcon;
            TextView txtTitle;
        }

}

0 个答案:

没有答案