android list view image addition
holder.text.setText(DATA[position]); holder.icon.setImageBitmap((position &1) == 1 ? mIcon1 : mIcon2);
return convertView;
}
The following code above lets me display only icons 1 and 2, I have added other images but dont know how to add those icons to the listview.
Any hel开发者_开发知识库p would appreciated as I am just new here.
I'm not sure if this is exactly what you are looking for but you can try something like this.
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
setListAdapter(new CustomAdapter(this));
selection=(TextView)findViewById(R.id.selection);
}
class CustomAdapter extends ArrayAdapter {
Activity context;
CustomAdapter(Activity context) {
super(context, R.layout.row, items);
this.context=context;
}
public View getView(int position, View convertView, ViewGroup parent) {
View row=convertView;
if (row==null) {
LayoutInflater inflater=context.getLayoutInflater();
row=inflater.inflate(R.layout.row, null);
}
TextView label=(TextView)row.findViewById(R.id.label);
label.setText(items[position]);
ImageView icon=(ImageView)row.findViewById(R.id.icon);
//you can put your own logic to add images here
if (items[position].length()>4) {
icon.setImageResource(R.drawable.delete);
}
else {
icon.setImageResource(R.drawable.ok);
}
return(row);
}
}
精彩评论