一千萬個為什麽

搜索

如何在android中將drawable設置為ListView

我正在為Android 1.5編寫一個應用程序。  我想使用復雜的列表視圖來顯示我的數據。  我想在List項目中顯示可繪制對象的ImageView。

我從一個演示中學到了:

------> listData.put("Img", listData.put("Img", R.drawable.XXX));
listData.put("Time", "100");
listItems.add(listData);

它可以正確顯示,但是,我想在運行時更改Img,圖像可能是在運行時生成的,所以我將代碼更改為如下,但它會下降。 誰能幫我 ?非常感謝!

protected void onCreate(Bundle savedInstanceState) {
   //TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.item_list);
    itemListView = (ListView) findViewById(R.id.listview);
    ArrayList> listItems = new ArrayList>();
    for(int i = 0;i  listData = new HashMap();
--------->/*1)*/  listData.put("Img", new Drawable(XXX));
        /*2)*/  listData.put("Time", "100");
        /*3)*/  listItems.add(listData);
    }
    SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItems, R.layout.listitem, new String[] { "Img", "Time"}, new int[] { R.id.listitem_img, R.id.listitem_time });
    itemListView.setAdapter(listItemAdapter);
}

listitem.xml

<?xml version="1.0" encoding="utf-8"?>


    

    


最佳答案

嘗試這個,它幫助了我。

public class AndroidList extends ListActivity {

public class MyCustomAdapter extends ArrayAdapter {

 public MyCustomAdapter(Context context, int textViewResourceId,
   String[] objects) {
  super(context, textViewResourceId, objects);
 //TODO Auto-generated constructor stub
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
 //TODO Auto-generated method stub
  //return super.getView(position, convertView, parent);

  View row = convertView;

  if(row==null){
   LayoutInflater inflater=getLayoutInflater();
   row=inflater.inflate(R.layout.row, parent, false);
  }

  TextView label=(TextView)row.findViewById(R.id.weekofday);
  label.setText(month[position]);
  ImageView icon=(ImageView)row.findViewById(R.id.icon);

  if (month[position]=="December"){
   icon.setImageResource(R.drawable.icon);
  }
  else{
   icon.setImageResource(R.drawable.icongray);
  }

  return row;
 }
}

String[] month = {
     "January", "February", "March", "April",
     "May", "June", "July", "August",
     "September", "October", "November", "December"
   };

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      //setContentView(R.layout.main);
      /*setListAdapter(new ArrayAdapter(this,
        R.layout.row, R.id.weekofday, DayOfWeek));*/
      setListAdapter(new MyCustomAdapter(AndroidList.this, R.layout.row, month));
  }

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
//TODO Auto-generated method stub
 //super.onListItemClick(l, v, position, id);
 String selection = l.getItemAtPosition(position).toString();
 Toast.makeText(this, selection, Toast.LENGTH_LONG).show();
}

}

found here: http://android-er.blogspot.com/2010/06/using-convertview-in-getview-to-make.html

轉載註明原文: 如何在android中將drawable設置為ListView