目前,我有一個默認的列表視圖顯示名稱從Arraylist
JavaClassName。我想實現一個ImageView的到ListView中,所以我已閱讀並試圖從這些2個鏈接方法:
Android Custom Listview with Image and Text using ArrayAdapter
Android Custom ListView with Images and Text – ExampleAndroid自定義列表視圖和ArrayList
我從他們在各自CustomList
類String[]
和Integer[]
轉換成我自己的ArrayList
問題圖像和名稱存儲在該Arraylist
。我的ArrayList
也包含其他數據,但我只需要獲取數據以顯示在定製的ListView
中。
編輯/添加上:
private final Activity context;
private final String[] itemname;
private final Integer[] imgid;
public CustomListAdapter(Activity context, String[] itemname, Integer[] imgid) {
super(context, R.layout.mylist, itemname);
// TODO Auto-generated constructor stub
this.context=context;
this.itemname=itemname;
this.imgid=imgid;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.mylist, null,true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);
txtTitle.setText(itemname[position]);
imageView.setImageResource(imgid[position]);
extratxt.setText("Description "+itemname[position]);
return rowView;
};
我按照密切合作,以提供創建customAdapter的環節之一。這是我在MainActivity
final ArrayList<JavaClassName> arrayListName = new ArrayList<JavaClassName>();
arrayListName.add(new JavaClassName(R.drawable.icon1, "name", "info", "info", "info"));
arrayListName.add(new JavaClassName(R.drawable.icon2, "name", "info", "info", "info"));
final ArrayList<String> itemName = new ArrayList<String>();
for(int i=0; i<arrayListName.size(); i++){
String name = arrayListName.get(i).getName().toString();
itemName.add(name);
};
CustomAdapter adapter = new CustomAdapter>(this, android.R.layout.simple_list_item_1, itemName);
listView.setAdapter(adapter);
我怎麼能適應的String []和Integer []從CustomAdapter類到CustomAdapter適配器=新CustomAdapter>(這一點,android.R.layout.simple_list_item_1 , 項目名);?
小心分享您的代碼並正確解釋發生了什麼? – aribeiro
'adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,arrayListName);' 這是我的當前適配器。給定的2個鏈接使用這個傳遞2個數組的adapater:'CustomListAdapter adapter = new CustomListAdapter(this,itemname,imgid);' 所以我如何進行更改以從列表中獲取圖像和名稱到customListAdapter中,數組? –
Alvin
您將不得不創建自定義適配器,以接受您爲創建列表中的數據而創建的集合,以便創建自定義列表視圖。有關這方面的更多信息,請提供問題中的代碼,以便人們可以幫助您。 –