Java 从 List<Object> 填充 ListView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20886795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:01:20  来源:igfitidea点击:

Populate ListView from List<Object>

javaandroidlistviewandroid-listviewandroid-arrayadapter

提问by smotru

Basically I have a list of objects of class Ingredient which looks like this:

基本上我有一个类 Ingredient 的对象列表,它看起来像这样:

class Ingredient {
    public int id;
    public String name;

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

so each object from list have a name.

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

Now to use ArrayAdapter I need to have a List of strings filled with names. Is there any way to use ArrayAdapter with my list of Ingredients? This is how it looks right now

现在要使用 ArrayAdapter,我需要有一个用名称填充的字符串列表。有什么方法可以将 ArrayAdapter 与我的成分列表一起使用吗?这就是它现在的样子

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

采纳答案by Raghunandan

Use the below. You can use a for loop and populate your ingredientsList. The below is just a example

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

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

Then

然后

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

You can use a CustomAdapterArrayAdapterinflate a custom layout

您可以使用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

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

ViewHolder is for smooth scrolling and performance

ViewHolder 用于平滑滚动和性能

row.xml

行.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>

Edit:

编辑:

Without using custom adapter

不使用自定义适配器

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

}

Then

然后

public class MainActivity extends Activity {

公共类 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);
  }
  }

Then

然后

activity_main.xml

活动_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>

Snap

折断

enter image description here

在此处输入图片说明

回答by Submersed

Extend the array adapter class:

扩展数组适配器类:

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