久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Android - ListView 中的 EditTexts 綁定到自定義 ArrayA

Android - EditTexts within ListView bound to Custom ArrayAdapter - keeping track of changes(Android - ListView 中的 EditTexts 綁定到自定義 ArrayAdapter - 跟蹤更改)
本文介紹了Android - ListView 中的 EditTexts 綁定到自定義 ArrayAdapter - 跟蹤更改的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時(shí)送ChatGPT賬號(hào)..

我有一個(gè) Android 活動(dòng),其中我有一個(gè)綁定到自定義 ArrayAdapter 的 ListView.ListView 的每一行都有兩個(gè) EditText(數(shù)字)字段.

I have an Android activity in which I have a ListView bound to a custom ArrayAdapter. Each row of the ListView has two EditText (numeric) fields.

ArrayAdapter 最初是從 SQLite DB 填充的.但是,用戶可以在 ListView 的末尾添加一行,或者(通過長(zhǎng)按一行)刪除 ListView 中的任何行.當(dāng)他們點(diǎn)擊保存"按鈕時(shí),他們的更改會(huì)被保留.

The ArrayAdapter is initially populated from an SQLite DB. However the user can add a row at the end of the ListView, or (by long-pressing on a row) delete any row in the ListView. When they hit the 'Save' button, their changes are persisted.

我正在跟蹤所做的更改,方法是將 AfterTextChanged() 事件的 CustomTextWatcher 附加到 ArrayAdapter 的 getView() 方法中的每個(gè) EditText(傳入 EditText 和 ArrayAdapter 的項(xiàng)目列表中的相應(yīng)對(duì)象),然后將該對(duì)象的匹配屬性設(shè)置為 EditText 的內(nèi)容.這樣在保存時(shí),我可以簡(jiǎn)單地遍歷底層對(duì)象列表并進(jìn)行適當(dāng)?shù)臄?shù)據(jù)庫更改,知道對(duì)象列表是最新的.

I am keeping track of changes as they are made by attaching a CustomTextWatcher for the AfterTextChanged() event to each EditText in the ArrayAdapter's getView() method (passing in the EditText and the corresponding object in the ArrayAdapter's item list) and then setting the matching property of that object to the content of the EditText. This is so that on saving I can simply iterate through the underlying object list and make the appropriate DB changes, knowing that the object list is up-to-date.

適配器類和 CustomTextWatcher 的代碼:

Code for the adapter class and the CustomTextWatcher:

private class CustomAdapter extends ArrayAdapter<DataItem> {
    private ArrayList<DataItem> items;

    public CustomAdapter(Context context, int textViewResourceId, ArrayList<DataItem> items) {
        super(context, textViewResourceId, items);
        this.items = items;
}

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;

            DataItem wed = items.get(position);

            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.log_exercise_row, null);
            }

            if(wed != null)
            {
                     EditText text1 = (EditText) v.findViewById(R.id.text1);
                     EditText text2 = (EditText) v.findViewById(R.id.text2);

                text1.addTextChangedListener(new CustomTextWatcher(text1,wed));
                text2.addTextChangedListener(new CustomTextWatcher(text2,wed));


                int weight = wed.getWeight();

                if(weight != -1)
                {
                   text1.setText(weight+"");
                }


                int reps = wed.getReps();

                if(reps != -1)
                {
                    text2.setText(reps+"");
                }


            }

            return v;
    }

private class CustomTextWatcher implements TextWatcher {

    private EditText EditText; 
    private DataItem item;

    public CustomTextWatcher(EditText e, DataItem item)
    {
        this.EditText = e;
        this.item = item;
    }

    @Override
    public void afterTextChanged(Editable arg0) {

        String text = arg0.toString();

        if(text != null && text.length() > 0)
        {
            int val;

            try
            {
                val =Integer.parseInt(text);
            }

            catch(NumberFormatException e)
            {
                val=-1;
            }

            if(EditText.getId()==R.id.weightEditText)
            {
                item.setWeight(val);
            }

            else if(EditText.getId()==R.id.repsEditText)
            {
                item.setRepetitions(val);
            }
        }
    }

這一切都很好,除非在發(fā)生不需要的 EditText 內(nèi)容重復(fù)時(shí)添加或刪除項(xiàng)目.

This all works fine except when items are added or deleted when unwanted duplication of EditText content happens.

舉例說明:

從 3 行開始,EditText 全部為空
在第 2 行中,輸入5"、6"
單擊更多"-> 在末尾添加一個(gè)(空)行.現(xiàn)在有 4 行.
刪除最后一行->顯示的 3 行,但第 2 行和第 3 行現(xiàn)在顯示 '5'、'6'->???????

Start with 3 rows, EditText's all empty
In row 2, enter '5', '6'
Click 'More'->Adds an (empty) row on the end. Now 4 rows.
Delete last row->3 rows displayed, BUT 2nd AND 3rd rows now display '5', '6'->???????

看起來 EditText 對(duì)象在 ListView 中的相對(duì)位置在重新綁定后不穩(wěn)定,這是導(dǎo)致問題的原因.例如.EditText 對(duì)象X"從位置 3 開始,然后在重新綁定后位于位置 1 - 但它仍然附有一個(gè) CustomTextWatcher,引用位置 3 中的數(shù)據(jù)對(duì)象.另一個(gè)問題是 addTextChangedListener() 不影響TextWatchers 已附加到 EditText.

It looks like the relative position of EditText objects within the ListView is not stable after rebinding which is causing the problem. E.g. EditText object 'X' starts in position 3, then after a rebind it is in position 1 - but it still has a CustomTextWatcher attached to it referencing the data object in position 3. The other issue is the fact that addTextChangedListener() does not affect the TextWatchers already attached to the EditText.

我對(duì) Android 很陌生,所以我不確定是否有更好的方法來解決我的問題.任何幫助表示贊賞.

I'm pretty new to Android, so I'm not sure if there is a better approach to solve my problem. Any help is appreciated.

推薦答案

看起來我需要一個(gè)自定義 ('ViewHolder') 類來保持 EditText 及其關(guān)聯(lián)數(shù)據(jù)對(duì)象之間的引用在每個(gè)對(duì)象上正確同步綁定"數(shù)據(jù).此外,當(dāng)在 getView() 方法中的 convertView 對(duì)象為 null 時(shí)放置事件偵聽器調(diào)用意味著每個(gè) EditText 對(duì)象僅添加一個(gè)偵聽器.

It looks like a custom ('ViewHolder') class was what I needed in order to keep the references between a EditText and its associated data object properly synchronised on each 'bind' of the data. Also, placing the event listener calls when the convertView object was null in the getView() method meant only one listener was added per EditText object.

感謝 http://www.vogella.de/articles/AndroidListView/article.html#listsactivity 為我指明了正確的方向.

Thanks to http://www.vogella.de/articles/AndroidListView/article.html#listsactivity for pointing me in the right direction.

public class CustomAdapter extends ArrayAdapter<DataItem> {
private ArrayList<DataItem> items;
private Activity Context;

public CustomAdapter(Activity context, int textViewResourceId, ArrayList<DataItem> items) {
    super(context, textViewResourceId, items);
    this.items = items;
    this.Context = context; 
}

static class ViewHolder {
    protected EditText weight;
    protected EditText reps;

}

 public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        DataItem wed = items.get(position);


        if (v == null) {
            LayoutInflater inflator = Context.getLayoutInflater();
            v = inflator.inflate(R.layout.log_exercise_row, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.text1 = (EditText) v.findViewById(R.id.text1);
            viewHolder.text2 = (EditText) v.findViewById(R.id.text2);


            viewHolder.text1.addTextChangedListener(new CustomTextWatcher(viewHolder, viewHolder.text1));
            viewHolder.text2.addTextChangedListener(new CustomTextWatcher(viewHolder, viewHolder.text2));

            v.setTag(viewHolder);
            viewHolder.text1.setTag(wed);
            viewHolder.text2.setTag(wed);

        }

        else
        {
            ViewHolder holder = (ViewHolder) v.getTag();
            holder.text1.setTag(wed);
            holder.text2.setTag(wed);   
        }

        ViewHolder holder = (ViewHolder) v.getTag();

        // set values

        if(wed.getWeight() != -1)
        {
            holder.text1.setText(wed.getWeight()+"");
        }

        else
        {
            holder.weight.setText("");
        }

        if(wed.getRepetitions() != -1)
        {
            holder.text2.setText(wed.getRepetitions()+"");
        }

        else
        {
            holder.reps.setText("");
        }

        return v;
}

這篇關(guān)于Android - ListView 中的 EditTexts 綁定到自定義 ArrayAdapter - 跟蹤更改的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Cut, copy, paste in android(在android中剪切、復(fù)制、粘貼)
android EditText blends into background(android EditText 融入背景)
Change Line Color of EditText - Android(更改 EditText 的線條顏色 - Android)
EditText showing numbers with 2 decimals at all times(EditText 始終顯示帶 2 位小數(shù)的數(shù)字)
Changing where cursor starts in an expanded EditText(更改光標(biāo)在展開的 EditText 中的開始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問題)
主站蜘蛛池模板: 亚洲午夜精品一区二区三区 | 日本不卡视频在线播放 | 免费欧美 | аⅴ资源新版在线天堂 | 91不卡 | 天堂av中文 | 国产欧美日韩综合精品一区二区 | 久久久av中文字幕 | 国产成人精品区一区二区不卡 | 亚洲综合视频 | 欧美精品日韩精品国产精品 | 久在线视频 | 九一视频在线播放 | 欧美福利网站 | 一本一道久久a久久精品蜜桃 | a级在线| 欧美在线a | 精品伊人 | 午夜天堂精品久久久久 | 操操日 | 亚洲成人av| 日日夜夜免费精品视频 | 亚洲精品美女视频 | 成人激情视频在线播放 | 亚洲顶级毛片 | 日韩午夜 | 91麻豆精品国产91久久久久久久久 | 91成人午夜性a一级毛片 | 日韩1区 | 免费网站国产 | 亚洲精品影院 | 久草在线中文888 | 中文字幕 国产精品 | 秋霞影院一区二区 | 91se在线| 91精品国产一区二区三区 | 亚洲成人精品在线 | 欧美一级久久精品 | 99久久99久久精品国产片果冰 | 欧美综合一区二区三区 | 日韩中文在线视频 |