从List <object> </object>填充ListView

时间:2014-01-02 16:04:58

标签: java android listview android-listview android-arrayadapter

基本上我有一个类成分的对象列表,如下所示:

class Ingredient {
    public int id;
    public String name;

    public Ingredient(String name) {
        this.name = name;
    }
}

所以列表中的每个对象都有一个名称。

现在使用ArrayAdapter我需要一个用名字填充的字符串列表。 有没有办法将ArrayAdapter与我的成分列表一起使用? 这就是它现在的样子

List<Ingredient> ingredientsList= new ArrayList<Ingredient>();
ingredientsList.add(new Ingredient("foo"));
ingredientsList.add(new Ingredient("bar"));

2 个答案:

答案 0 :(得分:9)

使用以下内容。您可以使用for循环并填充ingredientsList。以下只是一个例子

List<Ingredient> ingredientsList= new ArrayList<Ingredient>(); 
Ingredient i= new Ingredient("foo");   
ingredientsList.add(i);
Ingredient i1= new Ingredient("bar");   
ingredientsList.add(i1);

然后

 ListView lv = (ListView) findViewById(R.id.listview);
 // initialize listview
 lv.setAdpater(new CustomAdapterArrayAdapter(ActivityName.this,ingredientsList));
 // set the custom adapter to listview

您可以使用CustomAdapterArrayAdapter为自定义布局充气

public class CustomAarrayAdapter extends ArrayAdapter
{

List<Ingredient> ingredientsList;
public CustomArrayAdapter(Context context, List<Ingredient> list)
{
   super(context,0,list);
   ingredientList = list;
}

@Override 
public View getView(int position, View convertView, ViewGroup parent) {  
ViewHolder holder; 

if (convertView == null) { 
convertView = mInflater.inflate(R.layout.row,parent,false);
// inflate custom layout called row 
holder = new ViewHolder();
holder.tv =(TextView) convertView.findViewById(R.is.textView1);  
// initialize textview
convertView.setTag(holder);
}
else
{
      holder = (ViewHolder)convertView.getTag();
}
      Ingredient in = (Ingredient)ingredientsList.get(position);
      holder.tv.setText(in.name); 
      // set the name to the text;

return convertView;

}

static class ViewHolder
{

   TextView tv;
} 
}

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

ViewHolder用于平滑滚动和性能

row.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp"
        android:text="TextView" />

 </RelativeLayout>

编辑:

不使用自定义适配器

class Ingredient {
    public int id;
    public String name;

    public Ingredient(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return this.name.toString();
    }

}

然后

公共类MainActivity扩展了Activity {

List<Ingredient> ingredientsList= new ArrayList<Ingredient>(); 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    for(int i=0;i<10;i++)
    {
        ingredientsList.add(new Ingredient("foo"+i));
    }
   ArrayAdapter<Ingredient> adapter = new ArrayAdapter<Ingredient>(this,android.R.layout.simple_list_item_1,ingredientsList);
   ListView lv= (ListView) findViewById(R.id.listView1);
   lv.setAdapter(adapter);
  }
  }

然后

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" >
    </ListView>
</RelativeLayout>

对齐

enter image description here

答案 1 :(得分:1)

扩展数组适配器类:

public class MyAdapter extends ArrayAdapter<Ingredient> {

    Activity activity;

    public MyAdapter(Activity context, int resource,
            List<Ingredient> objects) {
        super(context, resource, objects);
        this.activity = context;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        Ingredient currentIngredient = getItem(position);

                //Do Something
              ....
        }