gpt4 book ai didi

android - 在 ListView 中显示员工列表

转载 作者:行者123 更新时间:2023-11-29 21:37:34 25 4
gpt4 key购买 nike

使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com