0
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
    JSONObject json = jParser.getJSONFromUrl("http://domain.com/directory/database/retrieveComments.php?placeId=" + stringPlaceId);
    try
    {
        commentsRatingsArray = json.getJSONArray("commentsRatings");
        for(int i = 0; i < commentsRatingsArray.length(); i++)
        {
        JSONObject jsonObject = commentsRatingsArray.getJSONObject(i);
        String dbUserFullName = jsonObject.getString(TAG_FULLNAME);
        String dbUserEmail = jsonObject.getString(TAG_EMAIL);
        String dbComment = jsonObject.getString(TAG_COMMENT);
        String dbRating = jsonObject.getString(TAG_RATING);
        String dbDate = jsonObject.getString(TAG_DATE);
        String dbTime = jsonObject.getString(TAG_TIME);

        HashMap<String, String> map = new HashMap<String, String>();
        map.put(TAG_FULLNAME, dbUserFullName);
        map.put(TAG_EMAIL, dbUserEmail);
        map.put(TAG_COMMENT, dbComment);
        map.put(TAG_RATING, dbRating);
        map.put(TAG_DATE, dbDate);
        map.put(TAG_TIME, dbTime);

        list.add(map);
    }   
}
catch (Exception e)
{
     e.printStackTrace();
     Toast.makeText(getBaseContext(), "Connection to the server is lost. Please check your internet connection.", Toast.LENGTH_SHORT).show();
}

ListAdapter adapter = new SimpleAdapter
(DisplayCommentsRatings.this, list, R.layout.commentrating,

     new String[] { TAG_FULLNAME, TAG_EMAIL, TAG_COMMENT, TAG_DATE,  TAG_TIME },
     new int[] {R.id.tvUserFullName, R.id.tvUserEmail, R.id.tvUserComment, R.id.tvDate, R.id.tvTime });

     setListAdapter(adapter);

这是我的代码,我正在从我的数据库中获取这些 JSON 数组值。我只想知道如何在列表视图中更改图像的 src。因为我只会使用 5 张图片,所以我决定将这些图片包含在我的资产文件夹中,而不是将它们上传到网络上。

有人可以给我一个想法,使这成为可能吗?

这是我的 XML 代码:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tvUserFullName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="12dip"
        android:textStyle="bold"/>
            //This is the imageView where I will display the image from the assets folder
    <ImageView
        android:id="@+id/ivUserRating"
        android:layout_width="100dip"
        android:layout_height="fill_parent"
        android:src="@drawable/zerostar"/>

</LinearLayout>

<TextView
    android:id="@+id/tvUserEmail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="EmailAddress@domain.com"
    android:textSize="9dip"/>

<TextView
    android:id="@+id/tvUserComment"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text='"This is a comment. This is a comment. This is a comment. This is a comment. This is a comment."'
    android:textSize="10dip"
    android:layout_margin="3dip"
    android:textColor="#000000"
    android:maxLength="300"/>

<TextView
    android:id="@+id/tvDate"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="August 1, 2010"
    android:textColor="#000000"
    android:textSize="8dip"
    android:layout_gravity="right"/>

<TextView
    android:id="@+id/tvTime"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="08:20 PM"
    android:textColor="#000000"
    android:textSize="8dip"
    android:layout_gravity="right"/>

4

2 回答 2

1

像往常一样使用 SimpleAdapter,但一定要覆盖你的“适配器”的 setViewBinder 方法,如:

adapter.setViewBinder(new ViewBinder() {   
    public boolean setViewValue(View view, Object data,   
    String textRepresentation) {   
    // Check wether it's ImageView and the data   
    if(view instanceof ImageView && data instanceof Bitmap){   
        ImageView iv = (ImageView) view;   
        iv.setImageBitmap((Bitmap) data);   
        return true;   
    }else   
        return false;   
    }   
});  

然后使用 getBitmap() 获取断言图像

public Bitmap getBitmap( String path, int i ){   
        Bitmap mBitmap = null;   
        try {  
            AssetManager assetManager = getAssets(); 
            String[] files = null; 

            files = assetManager.list( "smartmodel/" + path ); 
            Log.i( "Assert List", files[1].toString() );
            // Pass ur file path, here is one in assert/smartmodel/ filer
            mBitmap = BitmapFactory.decodeStream( this.getAssets().open( "smartmodel/" + path + "/"+ files[i]) );   
        } catch (Exception e) {   
            e.printStackTrace();   
        }
        return mBitmap;   
    }

最后,在您的简单适配器列表参数中,放入

map.put( "ItemImage", getBitmap( gridItemName, i ));

您传递的 getBitmap(...) 将被显示。

于 2013-10-19T09:26:28.387 回答
0

检查位置和使用,

Bitmap bmp=null;

if(position==0){

   bitmap=getBitmap("img0.png");

}else if  (position==1){

   bitmap=getBitmap("img1.png");
}
.
.
.

方法::

private Bitmap getBitmap(String name) throws IOException
        {
            AssetManager asset = getAssets();

            InputStream is = asset.open(name);
            Bitmap bitmap = BitmapFactory.decodeStream(is);

            return bitmap;
        }
于 2012-08-08T09:19:30.547 回答