基本上我有一个类成分的对象列表,如下所示:
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"));
解决方法:
使用以下.您可以使用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 textvIEwconvertVIEw.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>(); @OverrIDeprotected 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>
快照
总结以上是内存溢出为你收集整理的java – 从List填充ListView全部内容,希望文章能够帮你解决java – 从List填充ListView所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)