Android - ListView 中的 EditTexts 绑定到自定义 ArrayAdapter - 跟踪更改

发布于 2024-12-09 06:21:08 字数 3252 浏览 0 评论 0原文

我有一个 Android 活动,其中有一个绑定到自定义 ArrayAdapter 的 ListView。 ListView 的每一行都有两个 EditText(数字)字段。

ArrayAdapter 最初是从 SQLite DB 填充的。但是,用户可以在 ListView 的末尾添加一行,或者(通过长按一行)删除 ListView 中的任何行。当他们点击“保存”按钮时,他们的更改将被保留。

我通过将 AfterTextChanged() 事件的 CustomTextWatcher 附加到 ArrayAdapter 的 getView() 方法中的每个 EditText(传入 EditText 和 ArrayAdapter 的项目列表中的相应对象)然后设置匹配来跟踪所做的更改该对象的属性到 EditText 的内容。这样,在保存时,我可以简单地迭代底层对象列表并进行适当的数据库更改,知道对象列表是最新的。

适配器类和 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 内容重复时添加或删除项目。

举个例子来说明:

从 3 行开始,EditText 都是空的
在第 2 行中输入“5”、“6”
单击“更多”->在末尾添加一个(空)行。现在有 4 行。
删除最后一行 -> 显示的 3 行,但第二行和第三行现在显示“5”,“6”->??????

重新绑定后,ListView 中 EditText 对象的相对位置似乎不稳定,从而导致了问题。例如,EditText 对象“X”从位置 3 开始,然后在重新绑定后位于位置 1 - 但它仍然附加了一个 CustomTextWatcher,引用位置 3 中的数据对象。另一个问题是 addTextChangedListener() 不影响已附加到 EditText 的 TextWatchers。

我对 Android 还很陌生,所以我不确定是否有更好的方法来解决我的问题。任何帮助表示赞赏。

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.

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.

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.

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

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

To Illustrate with an example:

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'->???????

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久隐师 2024-12-16 06:21:08

看起来我需要一个自定义(“ViewHolder”)类,以便在数据的每个“绑定”上保持 EditText 及其关联数据对象之间的引用正确同步。此外,当 getView() 方法中的 ConvertView 对象为 null 时放置事件侦听器调用意味着每个 EditText 对象仅添加一个侦听器。

感谢 http://www.vogella.de/articles/AndroidListView/article.html#listsactivity 为我指明了正确的方向。

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

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.

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文