今天碰到ListView的問題,爲了加深記憶,就寫到博客中來。java
一。新建一個關於ListView的佈局文件(list.xml)android
<?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>app
二。新建一個關於ListView中Item的佈局文件(list_item.xml)ide
<?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"佈局
/>this
<TextView android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FF0000"xml
/>utf-8
</LinearLayout>get
三。編寫一個實體類(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;
}
}
六。