2010-04-18 7 views
5

Ich schreibe eine App für Android 1.5. Ich möchte eine komplexe Listenansicht verwenden, um meine Daten anzuzeigen. Ich möchte eine ImageView eines Zeichenobjekts in meinem Listenelement anzeigen.Wie kann ich auf eine ListView in Android

ich aus einem Demo gelernt: vielleicht zur Laufzeit generiert ändern

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

Es kann richtig angezeigt, jedoch möchte ich Img zur Laufzeit, wird das Bild, so dass ich ändern Sie den Code wie folgt, aber es fällt. Kann mir jemand helfen? Danke vielmals!

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<HashMap<String, Object>> listItems = new ArrayList<HashMap<String, Object>>(); 
    for(int i = 0;i <XXX.size(); ++i) { 
     HashMap<String, Object> listData = new HashMap<String, Object>(); 
--------->/*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"?> 
<LinearLayout 
    android:layout_width="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
    android:paddingBottom="4dip" 
    android:paddingLeft="12dip" 
    android:paddingRight="12dip"> 

    <ImageView 
     android:id="@+id/listitem_img" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingTop="12dip"/> 

    <TextView 
     android:id="@+id/listitem_time" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:textSize="20dip" /> 

</LinearLayout> 

Antwort

3

versuchen diese, hat es mir geholfen.

public class AndroidList extends ListActivity { 

public class MyCustomAdapter extends ArrayAdapter<String> { 

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<String>(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(); 
} 

} 

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