在listview中显示员工列表

时间:2013-08-03 12:54:25

标签: android listview

使用arrayList

显示listview中的员工列表
  • 员工名单 :: sam,max,carl
  • 必须为MainActivity.java添加哪些代码(要进行的更改) 使用bean类employee.java
  • 获得功能

row.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" />

Employee.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);



    }

}

感谢

2 个答案:

答案 0 :(得分: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.

答案 1 :(得分: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扩展)。如果您不知道如何开始查看: