列表视图edittext数据在Android中滚动时丢失

时间:2015-05-29 11:23:03

标签: android listview android-edittext

我希望listview有2个edittexts来保存值,即使在滚动后变得不可见。当edittext失去焦点时,我希望每个edittext值都保存在一个arraylist中(2个arraylist - 一个用于quantites,一个用于价格),我可以稍后将其保存到数据库中。 我尝试在OntextChanegd方法中使用代码,但它看起来并不正确。

public class CustomAdapter extends BaseAdapter {
ArrayList<String> names;
Context context;
ArrayList<String> itemPrices = new ArrayList<>();
ArrayList<String> quantities = new ArrayList<>();

CustomAdapter(ArrayList v, Context c) {
    names = v;
    context = c;
}

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

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

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    try {
        final ViewHolder holder;
        // TODO Auto-generated method stub
        if (convertView == null) {
            holder = new ViewHolder();

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.row, null);

            holder.textView = (TextView) convertView.findViewById(R.id.rowText);
            holder.editQty = (EditText) convertView.findViewById(R.id.qty);
            holder.editprice = (EditText) convertView.findViewById(R.id.price);
            holder.textView.setTextSize(20);
            holder.textView.setTextColor(Color.BLACK);

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.ref = position;

        holder.textView.setText(names.get(position));
        holder.editQty.setHint("Quantity");
        holder.editprice.setHint("Price");
        holder.editQty.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        holder.editprice.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });



        return convertView;
    }catch (NumberFormatException ex){
        Toast.makeText(context,"!!!",Toast.LENGTH_SHORT).show();
        ex.printStackTrace();
    }


    return convertView;
}




private class ViewHolder {
    TextView textView;
    EditText editQty;
    EditText editprice;

    int ref;
}

}

2 个答案:

答案 0 :(得分:3)

将听众添加到ViewHolder

 private class ViewHolder {
        TextView textView;
        EditText editQty;
        EditText editprice;
        TextWatcher qtyWatcher;
        TextWatcher priceWatcher;
        int ref;
    }

现在在getView()方法中删除edittext的现有监视,然后更新edittext中的文本并设置新的文本监视器。

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    try {
        final ViewHolder holder;
        // TODO Auto-generated method stub
        if (convertView == null) {
             holder = new ViewHolder();

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.row, null);

        holder.textView = (TextView) convertView.findViewById(R.id.rowText);
        holder.editQty = (EditText) convertView.findViewById(R.id.qty);
        holder.editprice = (EditText) convertView.findViewById(R.id.price);
        holder.textView.setTextSize(20);
        holder.textView.setTextColor(Color.BLACK);

        convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.ref = position;
        if (holder.qtyWatcher != null) {
            holder.editQty.removeTextChangedListener(holder.qtyWatcher);
        }
        if (holder.priceWatcher != null) {
            holder.editprice.removeTextChangedListener(holder.priceWatcher);
        }
        holder.textView.setText(names.get(position));
        holder.editQty.setHint("Quantity");
        holder.editprice.setHint("Price");
        if(position<quantities.size())
            holder.editQty.setText(quantities.get(position));
        else
            holder.editQty.setText("");
        if(position<itemPrices.size())
            holder.editprice.setText(itemPrices.get(position));
        else
          holder.editprice.setText("");
        holder.qtyWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start,
                    int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start,
                    int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                //Update the quantity
            }
        };
        holder.editQty.addTextChangedListener(holder.qtyWatcher);
        holder.priceWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start,
                    int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start,
                    int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                //Update you price
            }
        };
        holder.editprice.addTextChangedListener(holder.priceWatcher);

        return convertView;
    } catch (NumberFormatException ex) {
        Toast.makeText(context, "!!!", Toast.LENGTH_SHORT).show();
        ex.printStackTrace();
    }
    return convertView;
}

答案 1 :(得分:1)

您需要将更新值存储到数据数组中,因为list使用循环视图。

aa