【问题标题】:Populate ListView from List<Object>从 List<Object> 填充 ListView
【发布时间】:2014-01-20 03:04:22
【问题描述】:

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

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"));

【问题讨论】:

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


    【解决方案1】:

    使用下面的。您可以使用 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 用于平滑滚动和性能

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

    捕捉

    【讨论】:

    • 我避免创建 CustomAdapter :) 但我会创建它,因为我看不到任何其他解决方案.. ps。我最初忘记将成分添加到列表中...错误:)
    • @smotru 使用自定义适配器,您可以自定义布局并按照您想要的方式设置 textview 的样式。 CustomAdapter 有什么问题?
    • 我对 CustomAdapter 没有任何问题。其实我只是在研究。试图制作本教程link。他们使用“String[] sports”。我试图找到一种方法来实现我的成分列表,而无需创建新的布局或适配器。
    • @smotru 你也可以这样做
    • 迭代我的列表并使用成分名称创建 String[] 数组?或者你有什么其他想法?
    【解决方案2】:

    扩展数组适配器类:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-14
      • 2013-06-01
      • 1970-01-01
      • 2016-08-16
      • 1970-01-01
      • 1970-01-01
      • 2013-08-25
      相关资源
      最近更新 更多