在自定义列表中保存文本和复选框状态

时间:2012-04-08 08:20:45

标签: java android checkbox custom-lists lost-focus

我正在创建一个自定义列表,我想保存一些在R.id.quantity中的TextFile的文本。

我还想保存复选框的状态。我试图管理列表中复选框的已检查状态。

列表包含其他视图控件。我已将它们放在Viewholder类中。我正在使用一些初始数组值初始化qtyTxtV,一旦在此代码中发生事件,数组的值将更改但列表内容将被删除。请帮忙。

现在这段代码给了我一个空指针异常:

class MyAdapter1 extends BaseAdapter {
Context context;
ArrayList<Integer> price = new ArrayList<Integer>();
ArrayList<String> names = new ArrayList<String>();
public static HashMap<Integer, String> myList = new HashMap<Integer, String>();
static class ViewHolder {
    public TextView tvQuantityName, tvPrice, tvDishName;
    public ImageView imv1, imv2, imv3, imv4, imv5;
    public EditText edtQty;
    public CheckBox chkAdd;
}
public MyAdapter1(VegDetail vegDetail, ArrayList<String> names2,
        ArrayList<Integer> price2) {
    this.context = vegDetail;
    this.names = names2;
    this.price = price2;
    }
@Override
public int getCount() {
    return names.size();
}

@Override
public Object getItem(int position) {
    return null;
}
@Override
public long getItemId(int position) {
    return 0;
}
@SuppressWarnings("null")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View row = convertView;
    ViewHolder holder = null;

    // View row = inflater.inflate(R.layout.rowlayout, parent, false);
    if (row == null) {
        LayoutInflater inflater = LayoutInflater.from(context);
            row = inflater.inflate(R.layout.rowlayout, parent, false);
        holder.edtQty = (EditText) row.findViewById(R.id.quantity);
        holder.imv1 = (ImageView) row.findViewById(R.id.star1);
        holder.imv2 = (ImageView) row.findViewById(R.id.star2);
        holder.imv3 = (ImageView) row.findViewById(R.id.star3);
        holder.imv4 = (ImageView) row.findViewById(R.id.star4);
        holder.imv5 = (ImageView) row.findViewById(R.id.star5);
        holder.tvDishName = (TextView) row.findViewById(R.id.item_name);
        holder.tvPrice = (TextView) row.findViewById(R.id.price1);
        holder.tvQuantityName = (TextView) row.findViewById(R.id.qtyTxtV);
        holder.chkAdd = (CheckBox) row.findViewById(R.id.chkAdd);
        EditText edtQty = (EditText) row.findViewById(R.id.quantity);
        edtQty.setText(VegDetail.qtyArray[position].toString());
        CheckBox chkAdd = (CheckBox) row.findViewById(R.id.chkAdd);
        chkAdd.setSelected(VegDetail.chkArray[position]);           
        holder.edtQty.addTextChangedListener(new TextWatcher() {
            public void onTextChanged(final CharSequence s,
                    final int start, final int before, final int count) {
            }
            public void afterTextChanged(final Editable s) {
                int newValue;
                final String boxContents = s.toString();
                if (!boxContents.isEmpty()) {
                    try {
                        newValue = Integer.parseInt(boxContents);
                        VegDetail.qtyArray[position] = newValue;
                    } catch (final Exception exc) {
                        VegDetail.qtyArray[position] = 0;
                    } finally {
                    }
                } else {
                    VegDetail.qtyArray[position] = 0;
                }
            }
            public void beforeTextChanged(final CharSequence s,
                    final int start, final int count, final int after) {
            }
        });

        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }

    CheckBox chkAdd=(CheckBox)row.findViewById(R.id.chkAdd);
    chkAdd.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            //VegDetail is another class where I gonna use checked list's states
            VegDetail.chkArray[position] = isChecked;               
        }
    });
    final TextView tv1 = (TextView) row.findViewById(R.id.item_name);
    TextView tv2 = (TextView) row.findViewById(R.id.price1);
    final TextView tvQty = (TextView) row.findViewById(R.id.quantity);
    tv1.setText(names.get(position));
    tv1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(context,
                    "itme name is=>" + tv1.getText().toString(),
                    Toast.LENGTH_LONG).show();
        }
    });
    tv2.setText("Rs:" + price.get(position));   
    return row;
}
}

1 个答案:

答案 0 :(得分:0)

NPE必须在线:

    holder.edtQty = (EditText) row.findViewById(R.id.quantity);

,因为holderNULL,正如您刚才所定义的那样...... ViewHolder holder = null