2

我正在开发一个 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 中。但是,如果我使用我下载的可绘制图像,我该如何放置?

4

3 回答 3

2

// 这可能对你有帮助

[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);
于 2012-09-06T10:55:41.910 回答
0

确保在您的导入中没有 android.R ...并添加您自己的 R 文件 :)

于 2012-09-06T09:26:34.437 回答
0

尝试这个

  Integer[] flags = {
        R.drawable.Yourimg1, R.drawable.Yourimg2,
        R.drawable.Yourimg3, R.drawable.Yourimg4,          

};
于 2012-09-06T07:32:59.353 回答