如何从类中向ListView添加项?

时间:2014-02-05 13:59:14

标签: android listview optimization android-listview refactoring

所以我有

List <Employers> employers 

雇主类是:

public class Employers{
    private String fullName;
    private int age;

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
            this.fullName = fullName;
    }

    public int getAge() {
            return age;
    }
    public void setAge(int age) {
            this.age = age;
    }
}

我的ListView工作方式如下:

ArrayAdapter<String> adapter = new ArrayAdapter <String>(MainAcivity.this,android.R.layout.simple_list_item_1,receiveArray(employers));

其中receiveCategoryNames为:

private String[] receiveArray(List<Employers> employers){
    String [] employerArray= new String[employers.size()];
    for (int i=0; i<employerArray.length; i++){
        employerArray[i]=employers.get(i).getFullName();
    }
    return employerArray;
}

该计划正在运作。但是我可以以某种方式显示ListView而不将List of Employers转换为String数组吗?

2 个答案:

答案 0 :(得分:0)

我不确定你想要什么。

如果您只想显示名称

然后

public class Employers{
    private String fullName;
    private int age;

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
            this.fullName = fullName;
    }

    public int getAge() {
            return age;
    }
    public void setAge(int age) {
            this.age = age;
    }

    @Override
    public String toString() {      // note the toString method
        // TODO Auto-generated method stub
        return this.fullName;
    }

}

然后

  ArrayAdapter<Employers> adapter = new ArrayAdapter<Employers>(this,android.R.layout.simple_list_item_1,employers);
  lv.setAdapter(adapter);

如果您想显示姓名和年龄

使用CustomAdapter

CustomAdapter adapter= new CustomAdapter(ActivityName.this,employers);
lv.setAdapter(adapter);

然后

class CustomAdapter extends ArrayAdapter<Employers>
{


    List<Employers> data;
    LayoutInflater mInflater;
    public CustomAdapter(Context context,List<Employers> data) {
        super(context, R.layout.row,data);
        mInflater = LayoutInflater.from(context);
        this.data=data;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder;
        if(convertView==null)
        {
            holder = new ViewHolder();
            convertView= mInflater.inflate(R.layout.row, parent,false);
            holder.tv1= (TextView) convertView.findViewById(R.id.textView1);
            holder.tv2= (TextView) convertView.findViewById(R.id.textView2);
            convertView.setTag(holder);
        }else
        {
            holder= (ViewHolder) convertView.getTag();
        }
        Employers e = data.get(position);
        holder.tv1.setText(e.getFullName());
        holder.tv2.setText(String.valueOf(e.getAge()));
        return convertView;
    }
    static class ViewHolder
    {
        TextView tv1,tv2;

    }

}

row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="29dp"
        android:text="TextView" />

    <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="34dp"
        android:text="TextView" />

</RelativeLayout>

答案 1 :(得分:0)

在这里,您将了解如何制作自定义适配器并使用它。 Click here.