arrayAdapter list 图片

1. ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)
2.ArrayAdapter (Context context, int textViewResourceId, T[] objects)
3. ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)
几种常用的解释:
1.resource 一般为 layout ,T[] objects为数组,textViewResourceId 一般为textview即要显示 objects中的每一项(比如如下)
  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/icon"
android:layout_width="22px"
android:paddingLeft="2px"
android:paddingRight="2px"
android:paddingTop="2px"
android:layout_height="wrap_content"
android:src="@drawable/ok"
/>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="44sp"
/>
</LinearLayout>

setListAdapter(new ArrayAdapter<String>(this,
R.layout.row, R.id.label,items));
这样就可以实现图片和文字在list 中。
当然你也可以用
tv_show.setCompoundDrawablesWithIntrinsicBounds();实现该例子
如果你要动态的创建list图案的话,那么最后的做法就是自己写一个类继承baseAdapter

public class DynamicDemo extends ListActivity {
TextView selection;
String[] items={"lorem", "ipsum", "dolor", "sit", "amet",
"consectetuer", "adipiscing", "elit", "morbi", "vel",
"ligula", "vitae", "arcu", "aliquet", "mollis",
"etiam", "vel", "erat", "placerat", "ante",
"porttitor", "sodales", "pellentesque", "augue",
"purus"};
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
setListAdapter(new IconicAdapter(this));
selection=(TextView)findViewById(R.id.selection);
}
public void onListItemClick(ListView parent, View v,
int position, long id) {
selection.setText(items[position]);
}
class IconicAdapter extends ArrayAdapter {
Activity context;
IconicAdapter(Activity context) {
super(context, R.layout.row, items);
this.context=context;
}
public View getView(int position, View convertView,
ViewGroup parent) {
ViewInflate inflater=context.getViewInflate();
View row=inflater.inflate(R.layout.row, null, null);
TextView label=(TextView)row.findViewById(R.id.label);
label.setText(items[position]);
if (items[position].length()>4) {
ImageView icon=(ImageView)row.findViewById(R.id.icon);
icon.setImageResource(R.drawable.delete);
}
return(row);
}
}
}

你可能感兴趣的:(xml)