教育行業(yè)A股IPO第一股(股票代碼 003032)

全國(guó)咨詢/投訴熱線:400-618-4000

Android培訓(xùn)之ListView獲取item中EditText值(1)

更新時(shí)間:2016年10月31日17時(shí)23分 來(lái)源:傳智播客Android培訓(xùn)學(xué)院 瀏覽次數(shù):

 可以明確,現(xiàn)在沒有直接方法可以獲得ListView中每一行EditText的值.

解決方案:重寫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;
                }
            });
本文版權(quán)歸傳智播客Android培訓(xùn)學(xué)院所有,歡迎轉(zhuǎn)載,轉(zhuǎn)載請(qǐng)注明作者出處。謝謝!
作者:傳智播客Android培訓(xùn)學(xué)院
首發(fā):http://xamj520.com/Android
0 分享到:
和我們?cè)诰€交談!