Android listview, would like to have a diffrent picture on each row of my listview
I’m tryin to make a list box that has a different image and text for each listed item. I tried to creat two setListAdapter, one for the text string to be displayed and another for the image name. It did not work, I’m assuming there is another way to do this.
My CTICTY CLASS FOR THE LIST
public class cFashions extends ListActivity {
TextView selection;
// list to fill list box wi*/
/*
String[] items={"CASUAL","DR ESSES","CAREER","OUTWEAR","FOOTWEAR",
"JEWELRY","ACCESSORIES"};
*/
String[] items={"CASUAL"};
String[] icons={"icon"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fashions);
setListAdapter(new ArrayAdapter<String>(this,
R.layout.row, R.id.label,
items));
setListAdapter(new ArrayAdapter<String>(this,
R.layout.row, R.id.icon,
icons));
selection=(TextView)findViewById(R.id.selection);
}
}
THE XML FILE FOR THE LAYOUT OF EACH CELL IN THE LIST
<?xml version="1.0" encoding="utf-8"?>
<Line开发者_JAVA技巧arLayout 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"
/>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
/>
</LinearLayout>
Ted
You need to use one adapter, not two, where the adapter knows how to handle both your TextView
and your ImageView
. Here is a free excerpt from one of my books that covers this topic in detail.
精彩评论