gpt4 book ai didi

android - 在 simpleadapter 中使用 drawable

转载 作者:行者123 更新时间:2023-11-29 00:36:37 24 4
gpt4 key购买 nike

我正在开发 Android 应用程序。在我的应用程序中,我必须使用适配器。所以我使用了简单的适配器。

int[] flags = new int[]{
R.drawable.img1,
R.drawable.img2,

};

List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<number.size();i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", number.get(i));
hm.put("flag", Integer.toString(flags[i]) );
aList.add(hm);
}

String[] from = { "flag","txt"};

// Ids of views in listview_layout
int[] send = { R.id.flag,R.id.txt};

// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.autocomplete_layout, from, send);

现在我想使用我自己的可绘制数组列表而不是从资源中绘制。

Drawable d="some downloaded image from server"

现在我想在hashmap中使用上面的d。

hm.put("flag", d.toString() );

上面的 stamenet 不工作。我知道它是因为我之前发送了图像 ID。现在我正在将图像转换为字符串。

所以我必须将我的图像放入 hashmap hm。但是,如果我使用下载的可绘制图像,我该如何放置呢?

最佳答案

//这可能对你有帮助

[1]首先你需要

HashMap<String, Object> hm= new HashMap<String, Object>();
Bitmap bmImg;

[2] 对于在线图片需要1个函数来获取Bitmap对象

public void downloadFile(final String fileUrl) 
{
URL myFileUrl = null;
try {
myFileUrl = new URL(fileUrl);
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
//int length = conn.getContentLength();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
} catch (MalformedURLException e) {
// imageLoadedHandler.sendEmptyMessage(FAILED);
} catch (IOException e) {
// imageLoadedHandler.sendEmptyMessage(FAILED);
}

}

//用于将在线图片放入带线程的hasmap中,并需要在这里填写数据

for(int i=0;i<number.size();i++){
HashMap<String, Object> hm= new HashMap<String, Object>();
new Thread() {
public void run()
{
downloadFile(smtLink[ii]);
hm.put("image", bmImg);
};
}.start();
hm.put("flag", Integer.toString(flags[i]) );
aList.add(hm);

//并且需要 viewbinder 类

class MyViewBinder implements ViewBinder 
{
@Override
public boolean setViewValue(View view, Object data,String textRepresentation)
{
if((view instanceof ImageView) & (data instanceof Bitmap))
{
ImageView iv = (ImageView) view;
Bitmap bm = (Bitmap) data;
iv.setImageBitmap(bm);
return true;
}
return false;
}

}

//现在使用简单的适配器设置此数据,如下所示,此处根据您的适配器要求进行更改

adapater1 = new SimpleAdapter(News.this, list, R.layout.homrow, new String[] { "im", "Titel", "Sourcetag", "Date1","im1" }, 
new int[] { R.id.homerowmain,R.id.homerowtitle, R.id.homerowsourcetag,R.id.homerowdate, R.id.homerowimgaerrow });
adapater1.setViewBinder(new MyViewBinder());
itemlist.setAdapter(adapater1);

关于android - 在 simpleadapter 中使用 drawable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12294990/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com