如何将图像加载到从数据库中加载数据的列表视图中?

时间:2013-07-02 20:12:08

标签: android image listview

我尝试做的是将与数据库中的记录关联的图片文件加载到ListView中。下面的代码会将数据库中的记录加载到listview中,但是当我使用ImageView并尝试将相关文件加载到ImageView中时。我收到以下错误:

"The method decodeResource(Resources, int) in the type BitmapFactory is not applicable for the arguments (Resources, Bitmap)"

您可以给予我任何帮助将不胜感激。谢谢!

private void displayListView() {     
    setContentView(R.layout.activity_photo_review);
    root = Environment.getExternalStorageDirectory().toString();
    //      File myDir = new File(root + "/req_images/");
    //Get Assessment Name from Global Variable
    String CustName = ((GlobalVariables) this.getApplication()).getSomeVariable();

    camHelper = new DBCamera(this);   
    camHelper.open();     
    Cursor cursor = camHelper.fetchDataByAssessment(CustName);     

    String FileName = cursor.getString(cursor.getColumnIndexOrThrow("file_name"));

    String image = root + "/req_images/" + FileName;
    Bitmap myBitmap = BitmapFactory.decodeFile(image);
    //      ImageView iv = (ImageView) findViewById (R.id.imageView1);
    //      iv.setImageBitmap(myBitmap);

    // The desired columns to be bound   
    String[] columns = new String[] {     
            DBCamera.KEY_NAME,     
            DBCamera.KEY_DESCLUBSTORAGE,     
            DBCamera.KEY_DESCLUBDIS,     
            DBCamera.KEY_DESCLUBSCHED,     
            DBCamera.KEY_DESCTRAINING,     
            DBCamera.KEY_DESCOILSAMP,     
            DBCamera.KEY_DESCEQUIPMENT,     
            DBCamera.KEY_DESCMISCEQUIP,     
            DBCamera.KEY_DESCPRODUCTLEAK,     
            DBCamera.KEY_DESCHYDRAULICSYS,     
            DBCamera.KEY_DESCCENTRALLUBSYS,     
            DBCamera.KEY_DESCMOBILEEQUIP,     
            DBCamera.KEY_DESCHOUSEKEEPING,     
            DBCamera.KEY_DESCPOTENTIALENVIR,     
            DBCamera.KEY_DESCCONCERNS,     
            DBCamera.KEY_DESCSAFETY,     
            DBCamera.KEY_DESCCHECKBOX1,     
            DBCamera.KEY_DESCCHECKBOX2,     
            DBCamera.KEY_DESCCHECKBOX3,     
            DBCamera.KEY_DESCCHECKBOX4,     
            DBCamera.KEY_DESCCHECKBOX5,     
            DBCamera.KEY_DESCCHECKBOX6,     
            DBCamera.KEY_DESCCHECKBOX7,
    };     
    // the XML defined views which the data will be bound to   
    int[] to = new int[] {   
            //Load each picture
            //this is where I get the error "The method decodeResource(Resources, int) in the type BitmapFactory is not applicable for the arguments (Resources, Bitmap)"
            R.id.imageView1.setImageBitmap(BitmapFactory.decodeResource(getResources(),  myBitmap)),  


            R.id.code,     
            R.id.name,
            R.id.manufacturer,     
            R.id.visc40,     
            R.id.visc100,     
            R.id.viscindex,     
            R.id.den15c,     
            R.id.name1,     
            R.id.visctext,     
            R.id.baseoiltype,     
            R.id.name2,     
            R.id.baseoil,     
            R.id.additives,     
            R.id.name3,     
            R.id.otheradditives,     
            R.id.thickener,     
            R.id.nlgi,     
            R.id.name4,     
            R.id.comments,     
            R.id.packages,     
            R.id.area,
            R.id.usage,
            R.id.name5,     
    };     
    // create the adapter using the cursor pointing to the desired data    
    //as well as the layout information   
    dataAdapter = new SimpleCursorAdapter(     
            this, R.layout.activity_photo_review_info, cursor, columns, to, 0);     

    ListView listView = (ListView) findViewById(R.id.listView1);   
    // Assign adapter to ListView   
    listView.setAdapter(dataAdapter); 

    //SetOnItemClickListener for the ListView
    listView.setOnItemClickListener(new OnItemClickListener() {  
    @Override   
    public void onItemClick(AdapterView<?> listView, View view,       
            int position, long id) {    

        // Get the cursor, positioned to the corresponding row in the result set    
        Cursor cursor = (Cursor) listView.getItemAtPosition(position);      
        // Get the Customer Name from this row in the database.    
        String ProdName = cursor.getString(cursor.getColumnIndexOrThrow("ProductName")); 
        String Area = cursor.getString(cursor.getColumnIndexOrThrow("Area")); 

        Intent intent = new Intent(PhotoReview.this, ProductInput.class);
        intent.putExtra("ProdName", ProdName);
        intent.putExtra("Area", Area);
        startActivity(intent);

        //          Toast.makeText(getApplicationContext(), Area, Toast.LENGTH_SHORT).show();      

        }   

    });
}

2 个答案:

答案 0 :(得分:0)

decodeResource()期望资源ID被传递参数,而不是Bitmap,这就是你现在要传递的。

答案 1 :(得分:0)

您是否尝试过R.id.imageView1.setImageBitmap(myBitmap)