问题描述
基本上我有类成分的对象的列表,它看起来像这样:
类成分{
公众诠释身份证;
公共字符串名称; 公共成分(字符串名称){
this.name =名称;
}
}
因此,从列表中的每个对象都有一个名字。
现在使用ArrayAdapter我需要填充名称的字符串列表。
有什么办法用我的配料表中使用ArrayAdapter?
这是它的外观现在
列表<性成分GT; ingredientsList =新的ArrayList<性成分GT;();
ingredientsList.add(新成分(富));
ingredientsList.add(新成分(酒吧));
使用以下。您可以使用一个for循环并填充 ingredientsList
。以下仅仅是一个例子。
列表<性成分GT; ingredientsList =新的ArrayList<性成分GT;();
成分I =新的成分(富);
ingredientsList.add(ⅰ);
成分I1 =新的成分(巴);
ingredientsList.add(I1);
然后
的ListView LV =(ListView控件)findViewById(R.id.listview);
//初始化列表视图
lv.setAdpater(新CustomAdapterArrayAdapter(ActivityName.this,ingredientsList));
//设置自定义适配器的ListView
您可以使用 CustomAdapterArrayAdapter
膨胀的自定义布局
公共类CustomAarrayAdapter扩展ArrayAdapter
{清单<性成分GT; ingredientsList;
公共CustomArrayAdapter(上下文的背景下,列表与LT;成分>清单)
{
超级(上下文,0,清单);
ingredientList =清单;
}@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
ViewHolder持有人;如果(convertView == NULL){
convertView = mInflater.inflate(R.layout.row,父母,假);
//夸大所谓的行自定义布局
持有人=新ViewHolder();
holder.tv =(TextView中)convertView.findViewById(R.is.textView1);
//初始化的TextView
convertView.setTag(保持器);
}
其他
{
支架=(ViewHolder)convertView.getTag();
}
成份=(成分)ingredientsList.get(位置);
holder.tv.setText(in.name);
//设置名称的文本;返回convertView;}静态类ViewHolder
{ TextView的电视;
}
}
ViewHolder是平滑滚动和性能。
row.xml
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT> <的TextView
机器人:ID =@ + ID / textView1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentTop =真
机器人:layout_centerHorizontal =真
机器人:layout_marginTop =28dp
机器人:文字=TextView的/> < / RelativeLayout的>
编辑:
如果不使用自定义适配器
类成分{
公众诠释身份证;
公共字符串名称; 公共成分(字符串名称){
this.name =名称;
} @覆盖
公共字符串的toString(){
// TODO自动生成方法存根
返回this.name.toString();
}}
然后
公共类MainActivity延伸活动{
列表<性成分GT; ingredientsList =新的ArrayList<性成分GT;();
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
的for(int i = 0;我小于10;我++)
{
ingredientsList.add(新成分(富+ i)段);
}
ArrayAdapter<性成分GT;适配器=新ArrayAdapter<性成分GT;(这一点,android.R.layout.simple_list_item_1,ingredientsList);
LV的ListView =(ListView控件)findViewById(R.id.listView1);
lv.setAdapter(适配器);
}
}
然后
activity_main.xml中
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>
< ListView控件
机器人:ID =@ + ID / ListView1的
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerHorizontal =真正的>
< /&的ListView GT;
< / RelativeLayout的>
对齐
Basically I have a list of objects of class Ingredient which looks like this:
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
List<Ingredient> ingredientsList= new ArrayList<Ingredient>();
ingredientsList.add(new Ingredient("foo"));
ingredientsList.add(new Ingredient("bar"));
Use the below. You can use a for loop and populate your ingredientsList
. The below is just a example
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 CustomAdapterArrayAdapter
inflate a custom layout
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 is for smooth scrolling and performance
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>
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 {
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
<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
这篇关于从列表与LT填充的ListView;对象&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!