Android 将“资源”文件夹中的图像插入列表视图 ArrayList list=new ArrayList(); JSONObject json=jParser.getJSONFromUrl(“http://domain.com/directory/database/retrieveComments.php?placeId=“+stringPlaceId); 尝试 { commentsRatingsArray=json.getJSONArray(“commentsRatings”); 对于(int i=0;i
这是我的代码,我从数据库中获取这些JSON数组值。我只是想知道如何在列表视图中更改图像的src。因为我将只使用5个图像,所以我决定将这些图像包含在我的资产文件夹中,而不是将它们上载到web 有人能给我一个想法让这成为可能吗 以下是我的XML代码:Android 将“资源”文件夹中的图像插入列表视图 ArrayList list=new ArrayList(); JSONObject json=jParser.getJSONFromUrl(“http://domain.com/directory/database/retrieveComments.php?placeId=“+stringPlaceId); 尝试 { commentsRatingsArray=json.getJSONArray(“commentsRatings”); 对于(int i=0;i,android,image,listview,Android,Image,Listview,这是我的代码,我从数据库中获取这些JSON数组值。我只是想知道如何在列表视图中更改图像的src。因为我将只使用5个图像,所以我决定将这些图像包含在我的资产文件夹中,而不是将它们上载到web 有人能给我一个想法让这成为可能吗 以下是我的XML代码: //这是imageView,我将在其中显示资产文件夹中的图像 检查位置并使用 <LinearLayout android:orientation="horizontal" android:layout_width="fil
//这是imageView,我将在其中显示资产文件夹中的图像
检查位置并使用
<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"/>
方法::
Bitmap bmp=null;
if(position==0){
bitmap=getBitmap("img0.png");
}else if (position==1){
bitmap=getBitmap("img1.png");
}
.
.
.
正常使用SimpleAdapter,但请确保覆盖“适配器”的setViewBinder方法,如:
private Bitmap getBitmap(String name) throws IOException
{
AssetManager asset = getAssets();
InputStream is = asset.open(name);
Bitmap bitmap = BitmapFactory.decodeStream(is);
return bitmap;
}
然后使用getBitmap()获取断言图像
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;
}
});
最后,在简单适配器列表参数中,将
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;
}
您传递的getBitmap(…)将显示。字符串“name”是我图像的文件路径吗?对不起,先生。我还是不明白。所以“img0”和“img1”是图像的文件路径?谢谢。@JetPro如果您的图像在资源中(而不是在资源中的内部文件夹中),则使用诸如、、
bitmap=getBitmap(“img0.png”)代码>好的,先生。我试着去理解它。对不起,我是Android开发的新手。谢谢
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 ));