我正在制作一个自定义列表,我想保存 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;
}
}