Android : 继承BaseAdapter对ListView进行填充
1 import java.util.ArrayList;
2 import java.util.List;
3
4 import android.app.Activity;
5 import android.content.Context;
6 import android.os.Bundle;
7 import android.view.LayoutInflater;
8 import android.view.View;
9 import android.view.ViewGroup;
10 import android.widget.BaseAdapter;
11 import android.widget.ListView;
12 import android.widget.TextView;
13
14
15 class User {
16 private String userid;
17 private String username;
18
19 public String getUserid() {
20 return userid;
21 }
22
23 public void setUserid(String userid) {
24 this.userid = userid;
25 }
26
27 public String getUsername() {
28 return username;
29 }
30
31 public void setUsername(String username) {
32 this.username = username;
33 }
34 }
35
36 class ViewHolder {
37 TextView textviewUserId;
38 TextView textviewUserName;
39 }
40
41 class UserInfoAdapter extends BaseAdapter {
42 private LayoutInflater inflater;
43 private List<User> list;
44
45 public UserInfoAdapter(Context context, List<User> list) {
46 // TODO Auto-generated constructor stub
47 inflater = LayoutInflater.from(context);
48 this.list = list;
49 }
50
51 @Override
52 public int getCount() {
53 // TODO Auto-generated method stub
54 return list.size();
55 }
56
57 @Override
58 public User getItem(int position) {
59 // TODO Auto-generated method stub
60 return list.get(position);
61 }
62
63 @Override
64 public long getItemId(int position) {
65 // TODO Auto-generated method stub
66 return position;
67 }
68
69 @Override
70 public View getView(int position, View view, ViewGroup viewgroup) {
71 ViewHolder viewHolder = null;
72 if (view == null) {
73 view = inflater.inflate(R.layout.user_detail, null);
74 viewHolder = new ViewHolder();
75 viewHolder.textviewUserId = (TextView) view
76 .findViewById(R.id.textviewUserId);
77 viewHolder.textviewUserName = (TextView) view
78 .findViewById(R.id.textviewUserName);
79 view.setTag(viewHolder);
80 } else {
81 viewHolder = (ViewHolder) view.getTag();
82 }
83 viewHolder.textviewUserId.setText(list.get(position).getUserid());
84 viewHolder.textviewUserName.setText(list.get(position).getUsername());
85 }
86
87 }
88
89
90 class NewsActivity extends Activity{
91 private List<User> list = null;
92 private UserInfoAdapter adapter;
93 protected void onCreate(Bundle savedInstanceState) {
94 super.onCreate(savedInstanceState);
95 setContentView(R.layout.news_list);
96 list = new ArrayList<User>();
97 //Maybe here you will call json...
98 User user=new User();
99 user.setUserid("001");
100 user.setUsername("Zhou, Biwen");
101 list.add(user);
102 adapter=new UserInfoAdapter(this,list);
103 ListView listView = (ListView) findViewById(R.id.userlistview);
104 listView.setAdapter(adapter);
105
106 }
107 }
108
109
110 //news_detail.xml
111 //<?xml version="1.0" encoding="utf-8"?>
112 //<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
113 //android:layout_width="wrap_content"
114 //android:layout_height="wrap_content"
115 //android:orientation="horizontal">
116 //<LinearLayout
117 //android:layout_width="fill_parent"
118 //android:layout_height="wrap_content"
119 //android:orientation="vertical"
120 //android:paddingLeft="0px"
121 //android:paddingRight="5px"
122 //android:layout_marginTop="5px"
123 //android:layout_marginBottom="5px">
124 //<TextView
125 //android:id="@+id/textviewUserId"
126 //android:layout_width="wrap_content"
127 //android:layout_height="wrap_content"
128 //android:textSize="20px"
129 //android:textColor="#424952"
130 //android:layout_alignParentLeft="true">
131 //</TextView>
132 //<TextView
133 //android:id="@+id/textviewUserName"
134 //android:layout_width="wrap_content"
135 //android:layout_height="wrap_content"
136 //android:layout_alignParentRight="true"
137 //android:textColor="#f7a200"
138 //android:textSize="12px">
139 //</TextView>
140 //</LinearLayout>
141 //</LinearLayout>
2 import java.util.List;
3
4 import android.app.Activity;
5 import android.content.Context;
6 import android.os.Bundle;
7 import android.view.LayoutInflater;
8 import android.view.View;
9 import android.view.ViewGroup;
10 import android.widget.BaseAdapter;
11 import android.widget.ListView;
12 import android.widget.TextView;
13
14
15 class User {
16 private String userid;
17 private String username;
18
19 public String getUserid() {
20 return userid;
21 }
22
23 public void setUserid(String userid) {
24 this.userid = userid;
25 }
26
27 public String getUsername() {
28 return username;
29 }
30
31 public void setUsername(String username) {
32 this.username = username;
33 }
34 }
35
36 class ViewHolder {
37 TextView textviewUserId;
38 TextView textviewUserName;
39 }
40
41 class UserInfoAdapter extends BaseAdapter {
42 private LayoutInflater inflater;
43 private List<User> list;
44
45 public UserInfoAdapter(Context context, List<User> list) {
46 // TODO Auto-generated constructor stub
47 inflater = LayoutInflater.from(context);
48 this.list = list;
49 }
50
51 @Override
52 public int getCount() {
53 // TODO Auto-generated method stub
54 return list.size();
55 }
56
57 @Override
58 public User getItem(int position) {
59 // TODO Auto-generated method stub
60 return list.get(position);
61 }
62
63 @Override
64 public long getItemId(int position) {
65 // TODO Auto-generated method stub
66 return position;
67 }
68
69 @Override
70 public View getView(int position, View view, ViewGroup viewgroup) {
71 ViewHolder viewHolder = null;
72 if (view == null) {
73 view = inflater.inflate(R.layout.user_detail, null);
74 viewHolder = new ViewHolder();
75 viewHolder.textviewUserId = (TextView) view
76 .findViewById(R.id.textviewUserId);
77 viewHolder.textviewUserName = (TextView) view
78 .findViewById(R.id.textviewUserName);
79 view.setTag(viewHolder);
80 } else {
81 viewHolder = (ViewHolder) view.getTag();
82 }
83 viewHolder.textviewUserId.setText(list.get(position).getUserid());
84 viewHolder.textviewUserName.setText(list.get(position).getUsername());
85 }
86
87 }
88
89
90 class NewsActivity extends Activity{
91 private List<User> list = null;
92 private UserInfoAdapter adapter;
93 protected void onCreate(Bundle savedInstanceState) {
94 super.onCreate(savedInstanceState);
95 setContentView(R.layout.news_list);
96 list = new ArrayList<User>();
97 //Maybe here you will call json...
98 User user=new User();
99 user.setUserid("001");
100 user.setUsername("Zhou, Biwen");
101 list.add(user);
102 adapter=new UserInfoAdapter(this,list);
103 ListView listView = (ListView) findViewById(R.id.userlistview);
104 listView.setAdapter(adapter);
105
106 }
107 }
108
109
110 //news_detail.xml
111 //<?xml version="1.0" encoding="utf-8"?>
112 //<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
113 //android:layout_width="wrap_content"
114 //android:layout_height="wrap_content"
115 //android:orientation="horizontal">
116 //<LinearLayout
117 //android:layout_width="fill_parent"
118 //android:layout_height="wrap_content"
119 //android:orientation="vertical"
120 //android:paddingLeft="0px"
121 //android:paddingRight="5px"
122 //android:layout_marginTop="5px"
123 //android:layout_marginBottom="5px">
124 //<TextView
125 //android:id="@+id/textviewUserId"
126 //android:layout_width="wrap_content"
127 //android:layout_height="wrap_content"
128 //android:textSize="20px"
129 //android:textColor="#424952"
130 //android:layout_alignParentLeft="true">
131 //</TextView>
132 //<TextView
133 //android:id="@+id/textviewUserName"
134 //android:layout_width="wrap_content"
135 //android:layout_height="wrap_content"
136 //android:layout_alignParentRight="true"
137 //android:textColor="#f7a200"
138 //android:textSize="12px">
139 //</TextView>
140 //</LinearLayout>
141 //</LinearLayout>