Android EditText在ListView中滚动时丢失内容

时间:2015-08-23 15:49:21

标签: android android-listview

我有包含EditText的列表项,我不知道会有多少项。当我在EditText中输入一些文本,然后向下滚动ListView时,我有一个问题,在我再次向上滚动后,我的第一个EditText中没有文本,或者来自ListView的其他EditText中有一些文本。 代码:

public class MatHangAdapter
        extends ArrayAdapter<MatHangModel> {
    private static final int LAYOUT_ITEM_MAP_DOWNLOAD = R.layout.item_mat_hang;

    private final LayoutInflater mInflater;
    Context context;
    public MatHangAdapter(final Context context) {
        super(context,
              LAYOUT_ITEM_MAP_DOWNLOAD,
              new CopyOnWriteArrayList<MatHangModel>());
        this.mInflater = LayoutInflater.from(context);
        this.context = context;

    }


    /**
     * input  data from outside.
     */
    public void fillData(List<MatHangModel> dataModels) {
        this.clear();
        this.addAll(dataModels);
    }

    @Override
    public long getItemId(final int position) {
        return position;
    }

    @Override

    public View getView(int position,
                        View convertView,
                        ViewGroup parent) {
        final ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            final View view =
                    this.mInflater.inflate(LAYOUT_ITEM_MAP_DOWNLOAD,
                                           parent,
                                           false);
            holder = new ViewHolder(getContext(),
                                    view);

            view.setTag(holder);

            convertView = view;
        }
        //fill data.
        holder.fillData(this.getItem(position));

        return convertView;
    }

    public static class ViewHolder {
        private final Context mContext;
        // Fields.
        @InjectView(R.id.tv_scan_barcode)
        TextView tvScanBarcode;
        @InjectView(R.id.tv_qty_price)
        TextView tvQtyPrice;
        @InjectView(R.id.et_amount)
        EditText etAmount;

        public ViewHolder(Context context,
                          View view) {
            ButterKnife.inject(this,
                               view);// Initialize UI controls.
            mContext = context;
            //apply fonts....
//            CommonUtil.setFontForMainTitleTopicViewRecursive(tvItemMenu);

        }

        /**
         * fill data.
         */
        public void fillData(MatHangModel matHangModel) {
            //fill data
            if (matHangModel != null) {
               tvScanBarcode.setText(CommonUtil.getStringData(matHangModel.getBracode()));
               tvQtyPrice.setText(""+CommonUtil.getIntegerData(matHangModel.getPrice()));
                etAmount.setText(""+CommonUtil.getIntegerData(matHangModel.getAmount()));
                etAmount.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(final CharSequence charSequence,
                                                  int start,
                                                  int before, int count) {
                    }

                    @Override
                    public void onTextChanged(final CharSequence charSequence,
                                              final int i,
                                              final int i1,
                                              final int i2) {
                    }

                    @Override
                    public void afterTextChanged(final Editable editable) {

                    }
                });
            }

        }
        @OnClick(R.id.tv_scan_barcode)
        void onSceanBarcode(View view){
            CommonUtil.showViewWithAnimation(Techniques.FadeIn,view);
        }

    }
}

3 个答案:

答案 0 :(得分:2)

由于SqlCommands回收机制,您需要使用ListView的新值更新Adapter数据。

为了获得更好的性能,您可以在EditText的{​​{1}}内进行此操作(仅在完成文本编辑后更新数据):

afterTextChanged()

修改

1)在适配器中添加更新方法:

TextWatcher

2)更改您的@Override public void afterTextChanged(final Editable editable) { // update your model // matHangModel.setAmount(Integer.parseInt(editable.toString())); // update your adapter data with the new value of "matHangModel" } 方法以作为参数传递到位置,因为您在更新中需要它:

public void updateItem(MatHangModel model, int position){
    this.insert(model, position);
}

答案 1 :(得分:1)

如您所知,Android会在ListView中回收视图,因此如果您不存储,您在EditText中输入的文本可能会丢失。

onTextChanged商店的TextWatcher方法中,您输入的文字类似于matHangModel.setAmount(etAmount.getText().toString())

答案 2 :(得分:0)

试试这个:

@Override
 public void onTextChanged(final CharSequence charSequence,
                                              final int i,
                                              final int i1,
                                              final int i2) {

 matHangModel.setAmount(charSequence.toString());  //if amount is int then parse value. In layout EditText set android:inputType="number"
            android:ems="10"

}