目前,我有一个显示Arraylist
JavaClassName名称的默认列表视图。我希望在listview中实现一个imageview,所以我从这两个链接中读取并尝试了方法:
Android Custom Listview with Image and Text using ArrayAdapter
Android Custom ListView with Images and Text – Example
我在将各自String[]
类中的Integer[]
和CustomList
转换为我自己的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);
如何将CustomAdapter类中的String []和Integer []放入CustomAdapter适配器= new CustomAdapter&gt;(这个,android.R.layout.simple_list_item_1,itemName);?
答案 0 :(得分:0)
https://www.youtube.com/watch?v=cyk_ht8z6IA
据我所知,我建议你应该创建自己的适配器。我不确定arraylist,因为大多数时候我只使用数组。
答案 1 :(得分:0)
转换
ArrayList to Array
List<String> names = new ArrayList<String>();
// add stuff here
String[] namesArr = names.toArray(new String[names.size()]);
希望这有帮助!!!
答案 2 :(得分:0)