Java 从列表填充ListView<;对象>;

Java 从列表填充ListView<;对象>;,java,android,listview,android-listview,android-arrayadapter,Java,Android,Listview,Android Listview,Android Arrayadapter,基本上,我有一个类成分的对象列表,如下所示: class Ingredient { public int id; public String name; public Ingredient(String name) { this.name = name; } } 所以列表中的每个对象都有一个名称 现在要使用ArrayAdapter,我需要一个用名称填充的字符串列表。 有什么方法可以将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"));
List ingredientsList=new ArrayList();
添加(新成分(“foo”);
添加(新成分(“条”);

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

List<Ingredient> ingredientsList= new ArrayList<Ingredient>(); 
Ingredient i= new Ingredient("foo");   
ingredientsList.add(i);
Ingredient i1= new Ingredient("bar");   
ingredientsList.add(i1);
您可以使用
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;
} 
}
然后

公共类MainActivity扩展了活动{

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);
  }
  }
List ingredientsList=new ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

对于(int i=0;i扩展数组适配器类:

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
              ....
        }
公共类MyAdapter扩展了ArrayAdapter{
活动;
公共MyAdapter(活动上下文、int资源、,
列出对象){
超级(上下文、资源、对象);
这个活动=上下文;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
成分CurrentComponent=getItem(位置);
//做点什么
....
}

我避免创建CustomAdapter:)但我会创建它,因为我看不到任何其他解决方案了..注:我一开始忘了将成分添加到列表中…错误:)@smotru带有自定义适配器您可以自定义您的布局并按照您想要的方式设置文本视图的样式。CustomAdapter有什么问题吗?我对CustomAdapter没有任何问题。实际上我只是在研究。尝试制作此教程。他们使用了“String[]sports”。我试图找到一种方法来实现我的配料列表,而无需创建新的布局或适配器。@smotru您也可以这样做对我的列表进行迭代,并使用配料名称创建String[]数组。或者您还有其他想法吗?
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();
    }

}
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);
  }
  }
<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>
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
              ....
        }