2016-03-28 124 views
-1

目前,我有一个默认的列表视图显示名称从Arraylist JavaClassName。我想实现一个ImageView的到ListView中,所以我已阅读并试图从这些2个链接方法:
Android Custom Listview with Image and Text using ArrayAdapter
Android Custom ListView with Images and Text – ExampleAndroid自定义列表视图和ArrayList

我从他们在各自CustomListString[]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 , 项目名);?

+0

小心分享您的代码并正确解释发生了什么? – aribeiro

+0

'adapter = new ArrayAdapter (this,android.R.layout.simple_list_item_1,arrayListName);' 这是我的当前适配器。给定的2个链接使用这个传递2个数组的adapater:'CustomListAdapter adapter = new CustomListAdapter(this,itemname,imgid);' 所以我如何进行更改以从列表中获取图像和名称到customListAdapter中,数组? – Alvin

+0

您将不得不创建自定义适配器,以接受您为创建列表中的数据而创建的集合,以便创建自定义列表视图。有关这方面的更多信息,请提供问题中的代码,以便人们可以帮助您。 –

回答

0

https://www.youtube.com/watch?v=cyk_ht8z6IA

从我可以告诉,我建议你应该创建自己的适配器。由于大部分时间我只使用数组,因此我不确定ArrayList。

+0

是的,我确实创建了自己的适配器,但我不确定如何更改以适应数组列表。如果我要使用数组,我有另外4-5个数据,这意味着我必须创建多个数组。我做arraylist的目的是因为我的ListView一旦被点击,将会被显示到另一个页面,该页面显示来自arrayList的多个数据 – Alvin

0

转换ArrayList to Array

List<String> names = new ArrayList<String>(); 
// add stuff here 
String[] namesArr = names.toArray(new String[names.size()]); 

希望这有助于!

0

您的自定义适配器类继承自ArrayAdapter,我推测。根据该文档,资源,你提供给构造(在你的情况下第二个参数,R.layout.simple_list_item_1)应包含布局的TextView -

enter image description here

既然你已经不仅仅是一个TextView更复杂的布局,你应该扩展BaseAdapter类。然后,

  1. 创建您自己的ArrayList(S)来管理你的数据(在你的情况的ArrayList<String>ArrayList<Integer>)。
  2. 提供添加,删除和其他类似的方法来管理您的列表。
  3. 覆盖getView方法来扩充自定义布局以根据您的要求设置文本和图像。