为配合
RefreshListView,还需要对应的Adapter,测试代码如下:
CursorRefreshAdapter
1 package jie.java.android.test_listview;
2
3 import java.util.ArrayList;
4
5 import android.content.Context;
6 import android.database.Cursor;
7 import android.view.LayoutInflater;
8 import android.view.View;
9 import android.view.ViewGroup;
10 import android.widget.BaseAdapter;
11 import android.widget.TextView;
12
13 public class CursorRefreshAdapter extends BaseAdapter {
14
15 private ArrayList<DBAccess.Data> array = new ArrayList<DBAccess.Data>();
16 private Context context = null;
17
18 private DBAccess db = null;
19 private Cursor cursor = null;
20
21 private int maxRows = 0;
22 private int offset = 0;
23
24 private String condition = null;
25
26 public CursorRefreshAdapter(Context context, DBAccess db) {
27 this.context = context;
28 this.db = db;
29 }
30
31 @Override
32 protected void finalize() throws Throwable {
33 if(cursor != null) {
34 cursor.close();
35 }
36 super.finalize();
37 }
38
39 public int getCount() {
40 // TODO Auto-generated method stub
41 return array.size();
42 }
43
44 public Object getItem(int index) {
45 // TODO Auto-generated method stub
46 return array.get(index);
47 }
48
49 public long getItemId(int index) {
50 // TODO Auto-generated method stub
51 return 0;
52 }
53
54 public View getView(int position, View convertView, ViewGroup parent) {
55 if(convertView == null) {
56 convertView = LayoutInflater.from(this.context).inflate(android.R.layout.simple_list_item_2, parent, false);
57 }
58 TextView v1 = (TextView) convertView.findViewById(android.R.id.text1);
59 v1.setText(Integer.toString(array.get(position).id));
60 v1 = (TextView) convertView.findViewById(android.R.id.text2);
61 v1.setText(array.get(position).value);
62
63 return convertView;
64 }
65
66 public void setMaxRows(int max) {
67 maxRows = max;
68 }
69
70 public int load(final String condition) {
71 if(cursor != null) {
72 cursor.close();
73 }
74
75 this.condition = condition;
76
77 offset = 0;
78
79 return refresh();
80 }
81
82 public int refresh() {
83
84 cursor = db.getValue(this.condition, offset, maxRows);
85
86 if(cursor == null)
87 return -1;
88 if(!cursor.moveToFirst())
89 return -1;
90
91 while(cursor.moveToNext()) {
92 array.add(new DBAccess.Data(cursor.getInt(0), cursor.getString(1)));
93 }
94
95 offset += cursor.getCount();
96
97 this.notifyDataSetChanged();
98
99 return offset;
100 }
101 }
102
再一次被如下错误撂倒...
java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
解决方法还是如下:
v = vi.inflate(R.layout.items_row, p);
Add a false third parameter to that call, and I think your problem will go away. The call should become:
v = vi.inflate(R.layout.items_row, p, false);
原始链接如下:
http://stackoverflow.com/questions/2496698/android-custom-listadapter-extending-baseadapter-crashes-on-application-launch
唉,记忆力严重衰退啊...
另,近来比较忙,现在看来--LAC新版年底能出来就不错了...唉...困死了,睡觉去了...