ListView 和 Edittext 使用时焦点和数据混乱问题的解决办法

1.焦点问题:

xml布局中给listview设置一个descendantFocusability属性 

android:descendantFocusability="beforeDescendants"

beforeDescendants:viewgroup会优先其子类控件而获取到焦点 
afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点 
blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点

2. 数据混乱问题:

利用一个map去保存每个edittext的内容,然后在赋值的时候在从map中取出来,如果有就赋值,否则置空

事例代码:

public class FillInfoAdapter extends BaseAdapter {
    public HashMap<Integer, String> saveMap = new HashMap<>();
    private Context context;
    private ArrayList<FillBean> data;


    public FillInfoAdapter(Context context, ArrayList<FillBean> data) {
        this.context = context;
        this.data = data;
    }

    public void setData(ArrayList<FillBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_fillinfo, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.setData(position);
        return convertView;
    }

    private class ViewHolder {
        TextView tvTitle;
        EditText etValue;

        ViewHolder(View view) {
            tvTitle = view.findViewById(R.id.tvTitle);
            etValue = view.findViewById(R.id.etValue);
            etValue.addTextChangedListener(new MyTextChangedListener(this, saveMap));
        }

        public void setData(int position) {
            tvTitle.setText(data.get(position).getName());

            etValue.setTag(position);//注意设置position
            if (!TextUtils.isEmpty(saveMap.get(position))) {//不为空的时候 赋值给对应的edittext
                etValue.setText(saveMap.get(position));
            } else {//置空
                etValue.getEditableText().clear();
            }


        }

    }

    public class MyTextChangedListener implements TextWatcher {

        public ViewHolder holder;
        public HashMap<Integer, String> contents;

        public MyTextChangedListener(ViewHolder holder, HashMap<Integer, String> contents) {
            this.holder = holder;
            this.contents = contents;
        }

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

        }

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

        }

        @Override
        public void afterTextChanged(Editable editable) {
            if (holder != null && contents != null) {
                int position = (int) holder.etValue.getTag();
                contents.put(position, editable.toString());
        //将edittext中的值赋值进data就可以在activity中data.get(i).getValue()获取输入框中的值
                data.get(position).setValue(editable.toString());
            }
        }
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值