Android学习第三天--listview方式五
xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <ListView android:id="@+id/listview1" android:layout_width="match_parent" android:layout_height="wrap_content"/> </RelativeLayout>
java文件
import java.util.ArrayList; import java.util.List; import cn.core.entity.Person; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView listView=(ListView)findViewById(R.id.listview1); List <Person> list= new ArrayList<Person>(); Person person1=new Person(1, "是东方闪电", "男"); Person person2=new Person(3, "燕方法", "男"); list.add(person1); list.add(person2); ArrayAdapter<Person> arrayAdapter =new ArrayAdapter<Person>(this, android.R.layout.simple_list_item_1, list); listView.setAdapter(arrayAdapter); } }
person类
public class Person {
private Integer userId;
private String userName;
private String userSex;
public Person() {
super();
}
public Person(Integer userId, String userName, String userSex) {
super();
this.userId = userId;
this.userName = userName;
this.userSex = userSex;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserSex() {
return userSex;
}
public void setUserSex(String userSex) {
this.userSex = userSex;
}
@Override
public String toString() {
return "Person [userId=" + userId + ", userName=" + userName
+ ", userSex=" + userSex + "]";
}
}