Android 在simpleadapter中使用drawable

Android 在simpleadapter中使用drawable,android,adapter,simpleadapter,Android,Adapter,Simpleadapter,我正在开发一个Android应用程序。在我的应用程序中,我必须使用适配器。所以我使用了简单的适配器 int[] flags = new int[]{ R.drawable.img1, R.drawable.img2, }; List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>(); for(int i

我正在开发一个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);
现在我想在hashmap中使用上面的d

hm.put("flag", d.toString() );
上面的stamenet不工作。我知道这是因为之前我发送了图像ID。现在我正在将图像转换为字符串

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

试试这个

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

};

确保导入时没有android.R…并添加自己的R文件:)

//这可能会对您有所帮助

[1] 首先你需要

HashMap<String, Object> hm= new HashMap<String, Object>();
Bitmap bmImg;
//用于将联机图像放入带有线程的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);
//现在用下面这样的简单适配器设置这些数据,在这里根据您的需要使用adpter进行更改

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);

我的drawble图像不在Resources文件夹中。我从服务器获取该图像。请参阅此@gtumca MAC。。我下载所有图像并存储在arraylist中。检查我的hashmap hm。在那里我如何使用我的arraylist?@Arnoff。。我想你没有明白我的问题…简单地说。。。hm.put(“flag”,Integer.toString(flags[i]);flags I包含可绘制id。我想用它来代替。drawable d=“一些下载的图像”。那么我如何才能将我的d传递给hm?Hi@ankitmakwana..感谢您的回复..hm.put(“flag”,Integer.toString(flags[I]);。。flags是一个包含可绘制id的int数组。我们的新图像是下载的,因此我们不能使用flags[i]。我的问题是如何将图像添加到列表中。只需在forloop中传递imgaeurl数组,并给出它的位置和调用函数downloadfile(smtLink[ii]);是包含allimagesurlOke的数组..我有所有图像URI..例如:int-imagesuri[];。。Hashmap hm.add(“标志”,图像[uri]);列表。添加(hm)。simpleAdapter(News.this,list,R.layout.homrow,新字符串[]{“flag”},新int[]{R.id.image});。。。现在我也不会在这里获取IAMGE绑定布局SimpleAdapter=new SimpleAdapter(getBaseContext(),aList,R.layout.autocomplete\u layout,from,send)
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);
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);