android - 在 ListView 中显示员工列表

标签 android listview

使用 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);



    }

}

谢谢

最佳答案

**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.

关于android - 在 ListView 中显示员工列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18033086/

相关文章:

安卓开发 : Passing information

android - 单击后 onListItemClick 停止工作

android - ListView 自定义过滤器在过滤时给出错误的项目选择 [Android]

java - 如何在 listView 中同时左右对齐文本

java - Android 抽屉布局显示在 fragment 内容下

c# - ListView 未在 asp.net 中显示

Android:如何禁用 ListView?

java - 由于“确定”按钮而无法创建 AVD

android - 使用 SimpleCursorAdapter.ViewBinder 改变 TextView 的颜色

java - Android如何在继续之前等待代码完成