如何将数据库从数据库显示到listview?

时间:2014-09-17 04:23:46

标签: android listview

我是Android开发的新手。我能够将sqite数据库中的字符串恢复到我的列表视图中。现在我想通过我保存到数据库的图像路径将图像添加到我的列表视图中(我已经能够保存路径,我唯一的问题是如何在我的代码中添加该路径)。这是我的代码:

private void populateFields()  {
    // if the row is not null from the database. 
    if (mRowId != null) {
        cursor = studentsDbAdapter.queueStud(mRowId);
        String[] from = new String[]{StudentsDbAdapter.KEY_StudentName,StudentsDbAdapter.KEY_StudentID,StudentsDbAdapter.KEY_Course,StudentsDbAdapter.KEY_Year,StudentsDbAdapter.KEY_Contact,StudentsDbAdapter.KEY_Email};
        int[] to = new int[]{R.id.textstud,R.id.textstudID};
        cursorAdapter = new SimpleCursorAdapter(this, R.layout.stud_row, cursor, from, to);
        listContent.setAdapter(cursorAdapter);                 
    }                   
}

我不知道这是否会有所帮助,但在我保存图像路径的其他活动中,我可以使用以下代码恢复图像:

public void showpic() {

    //LoginDataBaseAdapter db = studentsDbAdapter.open();
    boolean emptytab = false;
    boolean empty = studentsDbAdapter.checkPic(null, emptytab);

    //Cursor cursor = loginDataBaseAdapter.fetchProfileImageFromDatabase();  

    if(empty==false) {  

        String pathName = studentsDbAdapter.getImapath(studID);
        File image = new  File(pathName); 
        if (image.exists()) {
            ImageView imageView= (ImageView) findViewById(R.id.studpic);
            imageView.setImageBitmap(BitmapFactory.decodeFile(image.getAbsolutePath()));
        }
    }
} 

2 个答案:

答案 0 :(得分:0)

您必须制作自定义适配器。

In adapter you can display image like this,

public static void ShowPicture(String fileName, ImageView pic) { 
    File f = new File(Environment.getExternalStorageDirectory(), fileName); 
    FileInputStream is = null; 
    try { 
        is = new FileInputStream(f); 
    } catch (FileNotFoundException e) {
        Log.d("error: ",String.format( "ShowPicture.java file[%s]Not Found",fileName)); 
        return; 
    } 

    Bitmap = BitmapFactory.decodeStream(is, null, null); 
    pic.setImageBitmap(bm); 
} 

答案 1 :(得分:0)

如何将图像存储在数据库中?

如果存储为blob,请使用BitmapFactory将其转换为Bitmap并将其设置为ImageView。

或者,如果它存储为Base64编码的字符串,则将其解码并将其转换为Bitmap。

相关问题