1

使用arrayList在listview中显示员工列表

  • 员工名单:: sam, max, carl
  • 必须添加哪些代码(进行更改)才能MainActivity.java使用 bean 类实现功能employee.java

行.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="TextView" />

雇员.java

import java.io.Serializable;

public class Employee implements Serializable{
    private String Name;
    public void SetName(String name){
        this.Name=name;
    }
    public String GetName(){
        return Name;
    }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <ListView
        android:id="@+id/listView-ID"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" >
    </ListView>
</LinearLayout>

MainActivity.java

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

    ArrayList<Employee> AL=new ArrayList<Employee>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView TV1=(TextView) findViewById(R.id.textView1);



    }

}

谢谢

4

2 回答 2

2
**You need to set Custom Adapter to your Listview. And in getView() method inflate your custom layout, as in below example.******** 


import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

    ArrayList<Employee> emp_arrayList=new ArrayList<Employee>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


      // Add Data to your ArrayList<Employee>

        Employee e=new Employee();
        e.setName("AAA");
        e.setSalary("23000");
        e.setName("Infi");
        emp_arrayList.add(e);
        Employee e1=new Employee();
        e1.setName("BBB");
        e1.setSalary("27000");
        e1.setName("Wipro");
        emp_arrayList.add(e1);

        **ListView l=findViewById(R.id.listView-id);
        MyAdapter adapter=new Adapter();
        l.setAdapter(l);
        l.notifyDataSetChanged();**


    }

}




class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return emp_arrayList.size();
        }
   @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return emp_arrayList.get(arg0);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup  rootLayout) {
            // TODO Auto-generated method stub

             View v = (LinearLayout) View.inflate(rootLayout.getContext(), R.layout.yourcustomlayout,null); 


          TextView employeename=(TextView) v.findViewById(R.id.empname);
          TextView employeesalary=(TextView) v.findViewById(R.id.empsalary);
          TextView employeecompany=(TextView) v.findViewById(R.id.empcompany);

                  employeename.setText(emp_arrayList.get(position).getName());
                  employeesalary.setText(emp_arrayList.get(position).getSalary());
                  employeecompany.setText(emp_arrayList.get(position).getCompany());
        return v;
    }``
}



I hope this will help you.
于 2013-08-03T15:34:23.873 回答
0

必须为 MainActivity.java 添加什么代码(进行更改)才能使用 bean 类 employee.java 实现功能

这不是太棘手。如果您不想要一些特殊/自定义的 ListView(列表中的自定义项),您只需要:

  • 正确初始化 ListView
  • 创建 ListAdapter (在您的情况下将足够系统内置 ArrayAdapter
  • 为了能够显示员工的姓名,您需要覆盖toString() Employee 类中的方法(否则将打印字符串表示 ob 对象,这不是人类可读的)

例子:

更新类:

public class Employee implements Serializable{

    private String name;

    public void setName(String name){
        this.name=name;
    }

    public String getName(){
        return name;
    }

    public String toString() {
       return this.name;
    }
}

ArrayAdapter 的初始化和分配示例:

ListView list = (ListView) findVieById(R.id.listid);
ArrayAdapter<Employee> adapter = new ArrayAdapter<Employee>(<context>, 
                                 android.R.layout.simple_list_item_1, <list>);
list.setAdapter(adapter);

笔记:

如果要自定义列表项,则需要为列表行创建自己的 XML 布局,并实现自己的 ListAdapter(例如从 BaseAdapter 扩展)。如果你不知道如何开始看:

于 2013-08-03T15:43:59.827 回答