今天碰到ListView的问题,为了加深记忆,就写到博客中来。
一。新建一个关于ListView的布局文件(list.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
二。新建一个关于ListView中Item的布局文件(list_item.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FF0000"
/>
<TextView android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FF0000"
/>
</LinearLayout>
三。编写一个实体类(User.java)
package com.youchuang.entity;
public class User {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
四。编写Activity(ListDemoActivity.java)
package com.youchuang.activity;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import com.youchuang.adapter.MyAdapter;
import com.youchuang.entity.User;
public class ListDemoActivity extends Activity {
private ListView lv;
private List<User> list;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
lv = (ListView)findViewById(R.id.listView);
list = getData();
//适配器
MyAdapter adapter = new MyAdapter(this, list);
//为ListView设置适配器
lv.setAdapter(adapter);
}
//获得List_item中的数据
private List<User> getData()
{
List<User> ls = new ArrayList<User>();
User user1 = new User();
user1.setName("jerry");
user1.setPassword("jerry");
ls.add(user1);
User user2 = new User();
user2.setName("tom");
user2.setPassword("tom");
ls.add(user2);
User user3 = new User();
user3.setName("jacker");
user3.setPassword("jacker");
ls.add(user3);
return ls;
}
}
五。编写适配器类(MyAdapter.java)
package com.youchuang.adapter;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.youchuang.activity.R;
import com.youchuang.entity.User;
public class MyAdapter extends BaseAdapter {
private LayoutInflater inflater;
private List<User> list;
public MyAdapter(Context context, List<User> list) {
this.inflater = LayoutInflater.from(context);
this.list = list;
}
public int getCount() {
return list.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
static class ViewHolder {
private TextView tv1;
private TextView tv2;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
//加载List_item的布局文件
convertView = inflater.inflate(R.layout.list_item, null);
holder.tv1 = (TextView) convertView.findViewById(R.id.name);
holder.tv2 = (TextView) convertView.findViewById(R.id.password);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv1.setText(list.get(position).getName());
holder.tv2.setText(list.get(position).getPassword());
return convertView;
}
}
六。
![](file:///C:/Users/Administrator/AppData/Roaming/Tencent/Users/1031047666/QQ/WinTemp/RichOle/O[WZ6SZCH¬WMT6]B]5$T1.jpg)