更新時(shí)間:2016年10月31日17時(shí)23分 來(lái)源:傳智播客Android培訓(xùn)學(xué)院 瀏覽次數(shù):
解決方案:重寫B(tài)aseAdapter,然后自行獲取ListView中每行輸入的EditText值.
大概算法:重寫B(tài)aseAdapter.getView函數(shù),用一個(gè)數(shù)組存儲(chǔ)EditText中的值,根據(jù)position即數(shù)組下標(biāo),在getView中動(dòng)態(tài)更新EditText和動(dòng)態(tài)獲取EditText中的值.因?yàn)長(zhǎng)istView中的item是復(fù)用的,如果不動(dòng)態(tài)清空或動(dòng)態(tài)獲取EditText中值,就會(huì)出現(xiàn)數(shù)據(jù)紊亂,或者沒數(shù)據(jù).那么在生成EditText的時(shí)候要監(jiān)控其值的變化.存儲(chǔ)下來(lái).
代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
package com.exmyth.android;
public class ListEditorAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<map<string, object="">> mData;// 存儲(chǔ)的EditText值
public Map<string, string=""> editorValue = new HashMap<string, string="">();//
public ListEditorAdapter(Context context, List<map<string, object="">> data) {
mData = data;
mInflater = LayoutInflater.from(context);
init();
}
// 初始化
private void init() {
editorValue.clear();
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
private Integer index = -1;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
// convertView為null的時(shí)候初始化convertView。
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.listview_item, null);
holder.name = (TextView) convertView
.findViewById(R.id.list_item_name);
holder.title = (TextView) convertView
.findViewById(R.id.list_item_title);
holder.value = (EditText) convertView
.findViewById(R.id.list_item_inputvalue);
holder.value.setTag(position);
holder.userkey = (TextView) convertView.findViewById(R.id.user_key);
holder.value.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
index = (Integer) v.getTag();
}
return false;
}
});
|
北京校區(qū)