Edittext 值被复制

Babd_Catha

我有一个对象列表视图,每个对象都包含一个 EditText,我使用了此页面中的代码我的问题是,当我在 edittext 中键入某些内容时,或者当我向 listView 添加一个内容时,它们中的每一个都会获得相同的值。

这是我的对象 xml:`

<Button
    android:layout_margin="16dp"
    android:layout_width="56dp"
    android:layout_height="56dp"
    android:background="@drawable/button_normal"
    android:id="@+id/row_button"/>
<EditText
    android:layout_marginTop="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginBottom="16dp"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:layout_toRightOf="@id/row_button"
    android:id="@+id/row_edittext"
    android:saveEnabled="false"
    android:hint="@string/NomDuJoueur"/>

`

我的对象类与网页中的相同。

...以及来自适配器的我的 GetView:

    public View getView(final int position, View convertView, ViewGroup     parent) {
    final ViewHolder holder;

    if (convertView == null) {
        holder = new ViewHolder();
        //Log.wtf("New", "Holder");
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.row_player, null, true);

        holder.editText = convertView.findViewById(R.id.row_edittext);
        holder.button = convertView.findViewById(R.id.row_button);

        convertView.setTag(holder);
    }else {
        // the getTag returns the viewHolder object set as a tag to the view
        holder = (ViewHolder)convertView.getTag();
    }

    holder.button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            editModelArrayList.remove(position);
            notifyDataSetChanged();
        }
    });

    holder.editText.setText(editModelArrayList.get(position).getEditTextValue());

    holder.editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            try {
                editModelArrayList.get(position).setEditTextValue(holder.editText.getText().toString());
                //Log.wtf("AAAD", Integer.toString(holder.editText.getId()));
                //Log.wtf("HNCZ", Integer.toString(position)+holder.editText.getText());
            }catch (Exception e){

            }

        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });

    return convertView;
}

我认为这是由所有具有相同 Id 的 Edittexts 引起的,但我无法找到一种方法来修改它们的 Id 有效。

萨米

我怀疑您TextChangedListener无法访问正确的位置。这里有一些你可以尝试的东西。

添加一个变量来存储您ViewHolder定义的位置

class ViewHolder{
    // Just add this after wherever you're defining your 'editText' and 'button' 
    // variables for your holder
    int position;
}

然后,在您的getView(), 在您的持有人设置后存储位置:

if(convertView == null){
    // your holder set up
}else{
    // your holder retrieval
}

// Store the position inside the holder
holder.position = position;

并使用您的onTextChanged().

try{
    editModelArrayList.get(holder.position).setEditTextValue(holder.editText.getText().toString());
}catch (Exception e){
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章