如何在android中的listview中显示db浏览器中存储的blob图像

时间:2018-05-17 22:21:39

标签: android sqlite listview blob

我目前正在android中的列表视图中显示来自我的数据库(db浏览器中的sqllite数据库)的信息。
我将数据库复制到assets文件夹中,从数据库中检索文本信息并在列表视图中显示它可以正常工作,但我不知道如何显示图像。

这是我的数据库问题:如何在列表视图项目的图像视图中设置图像(sqllite数据库中的blob)?

这是databasehelper的代码:

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DBNAME = "fairytales.sqlite";
    public static final String DBLOCATION = "/data/data/login.dell.com.affichage/databases/";
    private Context mContext;
    private SQLiteDatabase mDatabase;

    public DatabaseHelper(Context context) {
        super(context, DBNAME, null, 1);
        this.mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void openDatabase() {
        String dbPath = mContext.getDatabasePath(DBNAME).getPath();
        if(mDatabase != null && mDatabase.isOpen()) {
            return;
        }
        mDatabase = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
    }

    public void closeDatabase() {
        if(mDatabase!=null) {
            mDatabase.close();
        }
    }

    public List<Histoire> getListHistoire() {
        Histoire histoire = null;
        List<Histoire> histoirelist = new ArrayList<>();
        openDatabase();



        Cursor cursor = mDatabase.rawQuery("SELECT * FROM histoire", null);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            histoire = new Histoire(cursor.getInt(0), cursor.getInt(1),
                    cursor.getString(2), cursor.getString(3), cursor.getBlob(4));
            byte [] photo=cursor.getBlob(4);
            ByteArrayInputStream imageStream = new ByteArrayInputStream(photo);
            Bitmap image= BitmapFactory.decodeStream(imageStream);
            histoirelist.add(histoire);

            cursor.moveToNext();
        }
        cursor.close();
        closeDatabase();
        return histoirelist;
    }



}

这是我的列表适配器:

public class ListHistoireAdapter extends BaseAdapter {
    private Context mContext;
    private List<Histoire> mHistoireList;

    public ListHistoireAdapter(Context mContext, List<Histoire> mHistoireList) {
        this.mContext = mContext;
        this.mHistoireList = mHistoireList;
    }

    @Override
    public int getCount() {
        return mHistoireList.size();
    }

    @Override
    public Object getItem(int position) {
        return mHistoireList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return mHistoireList.get(position).getIdHistoire();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = View.inflate(mContext, R.layout.activity_item_histoire, null);
        TextView tvtitre = (TextView)v.findViewById(R.id.txtTitle);
        TextView tvauteur = (TextView)v.findViewById(R.id.txtAuteur);
        TextView tvnb = (TextView)v.findViewById(R.id.txtnb);
        ImageView tvimg=(ImageView)v.findViewById(R.id.imgIcon);

        tvtitre.setText(mHistoireList.get(position).getTitre());
        tvauteur.setText(mHistoireList.get(position).getAuteur());
        tvnb.setText(String.valueOf(mHistoireList.get(position).getNombredepages()));

//如何在列表视图中设置图像!!

        return v;
    }
}

我的主要活动:

public class MainActivity extends Activity {
    private ListView lvHisoire;
    private ListHistoireAdapter adapter;
    private List<Histoire> mHistoireList;
    private DatabaseHelper mDBHelper;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lvHisoire = (ListView)findViewById(R.id.listview_histoire);
        mDBHelper = new DatabaseHelper(this);


        lvHisoire.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent i=new Intent( MainActivity.this,Splash.class);
                Histoire histoire = (Histoire)mHistoireList.get(position);
                i.putExtra("ID",histoire.getIdHistoire());
                startActivity(i);
            }
        });


        //Check exists database
        File database = getApplicationContext().getDatabasePath(DatabaseHelper.DBNAME);
        if(false == database.exists()) {
            mDBHelper.getReadableDatabase();
            //Copy db
            if(copyDatabase(this)) {
                Toast.makeText(this, "Copy database succes", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "Copy data error", Toast.LENGTH_SHORT).show();
                return;
            }
        }
        //Get lignes list in db when db exists
        mHistoireList = mDBHelper.getListHistoire();
        //Init adapter
        adapter = new ListHistoireAdapter(this, mHistoireList);
        //Set adapter for listview
        lvHisoire.setAdapter(adapter);

    }

0 个答案:

没有答案