pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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

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

問題描述

限時送ChatGPT賬號..

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

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 的末尾添加一行,或者(通過長按一行)刪除 ListView 中的任何行.當他們點擊保存"按鈕時,他們的更改會被保留.

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() 方法中的每個 EditText(傳入 EditText 和 ArrayAdapter 的項目列表中的相應對象),然后將該對象的匹配屬性設置為 EditText 的內容.這樣在保存時,我可以簡單地遍歷底層對象列表并進行適當的數據庫更改,知道對象列表是最新的.

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);
            }
        }
    }

這一切都很好,除非在發生不需要的 EditText 內容重復時添加或刪除項目.

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

舉例說明:

從 3 行開始,EditText 全部為空
在第 2 行中,輸入5"、6"
單擊更多"-> 在末尾添加一個(空)行.現在有 4 行.
刪除最后一行->顯示的 3 行,但第 2 行和第 3 行現在顯示 '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 對象在 ListView 中的相對位置在重新綁定后不穩定,這是導致問題的原因.例如.EditText 對象X"從位置 3 開始,然后在重新綁定后位于位置 1 - 但它仍然附有一個 CustomTextWatcher,引用位置 3 中的數據對象.另一個問題是 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.

我對 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.

推薦答案

看起來我需要一個自定義 ('ViewHolder') 類來保持 EditText 及其關聯數據對象之間的引用在每個對象上正確同步綁定"數據.此外,當在 getView() 方法中的 convertView 對象為 null 時放置事件偵聽器調用意味著每個 EditText 對象僅添加一個偵聽器.

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;
}

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

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

相關文檔推薦

Cut, copy, paste in android(在android中剪切、復制、粘貼)
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 位小數的數字)
Changing where cursor starts in an expanded EditText(更改光標在展開的 EditText 中的開始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問題)
主站蜘蛛池模板: 齿轮减速机电机一体机_齿轮减速箱加电机一体化-德国BOSERL蜗轮蜗杆减速机电机生产厂家 | 西安展台设计搭建_西安活动策划公司_西安会议会场布置_西安展厅设计西安旭阳展览展示 | 注塑模具_塑料模具_塑胶模具_范仕达【官网】_东莞模具设计与制造加工厂家 | 南京技嘉环保科技有限公司-杀菌除臭剂|污水|垃圾|厕所|橡胶厂|化工厂|铸造厂除臭剂 | 变色龙PPT-国内原创PPT模板交易平台 - PPT贰零 - 西安聚讯网络科技有限公司 | 磁力去毛刺机_去毛刺磁力抛光机_磁力光饰机_磁力滚抛机_精密金属零件去毛刺机厂家-冠古科技 | 冷却塔风机厂家_静音冷却塔风机_冷却塔电机维修更换维修-广东特菱节能空调设备有限公司 | 低合金板|安阳低合金板|河南低合金板|高强度板|桥梁板_安阳润兴 北京租车牌|京牌指标租赁|小客车指标出租 | 瓶盖扭矩测试仪-瓶盖扭力仪-全自动扭矩仪-济南三泉中石单品站 | 低合金板|安阳低合金板|河南低合金板|高强度板|桥梁板_安阳润兴 北京租车牌|京牌指标租赁|小客车指标出租 | 微信小程序定制,广州app公众号商城网站开发公司-广东锋火 | 一点车讯-汽车网站,每天一点最新车讯! | 小小作文网_中小学优秀作文范文大全 | 三氯异氰尿酸-二氯-三氯-二氯异氰尿酸钠-优氯净-强氯精-消毒片-济南中北_优氯净厂家 | 隧道窑炉,隧道窑炉厂家-山东艾瑶国际贸易 | 微波消解仪器_智能微波消解仪报价_高压微波消解仪厂家_那艾 | 炉门刀边腹板,焦化设备配件,焦化焦炉设备_沧州瑞创机械制造有限公司 | 苏州柯瑞德货架-仓库自动化改造解决方案 | 清管器,管道清管器,聚氨酯发泡球,清管球 - 承德嘉拓设备 | 南京展台搭建-南京展会设计-南京展览设计公司-南京展厅展示设计-南京汇雅展览工程有限公司 | 吸污车_吸粪车_抽粪车_电动三轮吸粪车_真空吸污车_高压清洗吸污车-远大汽车制造有限公司 | LNG鹤管_内浮盘价格,上装鹤管,装车撬厂家-连云港赛威特机械 | 口信网(kousing.com) - 行业资讯_行业展会_行业培训_行业资料 | 热熔胶网膜|pes热熔网膜价格|eva热熔胶膜|热熔胶膜|tpu热熔胶膜厂家-苏州惠洋胶粘制品有限公司 | 上海深蓝_缠绕机_缠膜机-上海深蓝机械装备有限公司 | 工业插头-工业插头插座【厂家】-温州罗曼电气 | 防水试验机_防水测试设备_防水试验装置_淋雨试验箱-广州岳信试验设备有限公司 | 烟气在线监测系统_烟气在线监测仪_扬尘检测仪_空气质量监测站「山东风途物联网」 | 禹城彩钢厂_钢结构板房_彩钢复合板-禹城泰瑞彩钢复合板加工厂 | 天坛家具官网| 广州二手电缆线回收,旧电缆回收,广州铜线回收-广东益福电缆线回收公司 | 高考志愿规划师_高考规划师_高考培训师_高报师_升学规划师_高考志愿规划师培训认证机构「向阳生涯」 | 健康管理师报名入口,2025年健康管理师考试时间信息网-网站首页 塑料造粒机「厂家直销」-莱州鑫瑞迪机械有限公司 | HV全空气系统_杭州暖通公司—杭州斯培尔冷暖设备有限公司 | 威客电竞(vk·game)·电子竞技赛事官网 | 宁夏档案密集柜,智能密集柜,电动手摇密集柜-盛隆柜业宁夏档案密集柜厂家 | 小青瓦丨古建筑瓦丨青瓦厂家-宜兴市徽派古典建筑材料有限公司 | 盘装氧量分析仪-防爆壁挂氧化锆分析仪-安徽吉帆仪表有限公司 | 厌氧工作站-通用型厌氧工作站-上海胜秋科学仪器有限公司 | 二手Sciex液质联用仪-岛津气质联用仪-二手安捷伦气质联用仪-上海隐智科学仪器有限公司 | 国标白水泥,高标号白水泥,白水泥厂家-淄博华雪建材有限公司 |