我在 ListView 中有一个餐厅菜单, ListView 中的每一行都有:
- 元素图片
- 项目名称
- 商品价格
- 编辑文本以输入数量
- 用于选择项目的复选框
为了满足我的需要,我有一个具有上述属性的 MenuItem 对象类。
我已经为 ListView 实现了自定义适配器。现在我需要在 EditText 上实现 onTextChangedListener,以便每当数量发生变化时,我的对象都会更新,因此会反射(reflect)在最终订单账单中。我已将默认数量设置为 1。
我的适配器类
public class MenuAdapter extends ArrayAdapter<MenuItem> {
private Context context;
public MenuAdapter(Context context, int textViewResourceId, List<MenuItem> objects) {
super(context, textViewResourceId, objects);
this.context = context;
}
@Override
public View getView(int pos, View convertView, ViewGroup parent){
try{
android.view.LayoutInflater inflator = android.view.LayoutInflater.from(getContext());
View customView = inflator.inflate(R.layout.itemdetail, parent, false);
MenuItem singleItem = getItem(pos);
//ImageView iconImg = (ImageView) customView.findViewById(R.id.imgMenuItem);
TextView nameTxt = (TextView) customView.findViewById(R.id.nameText);
EditText qtyDdl = (EditText) customView.findViewById(R.id.inputQty);
TextView priceTxt = (TextView) customView.findViewById(R.id.priceText);
CheckBox selectChk = (CheckBox)customView.findViewById(R.id.chkItem);
nameTxt.setText(singleItem.getName());
qtyDdl.setText("1");
qtyDdl.addTextChangedListener(new TextWatcher()
{@Override
public void onTextChanged(CharSequence s, int start, int before, int count){
int i = start;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}});
priceTxt.setText(singleItem.getPrice());
selectChk.setOnCheckedChangeListener((SaltnPepperActivity)context);
return customView;
}catch(Exception e){
Log.e("MenuAdapter", e.toString());
return null;}
}
问题 1:每当发生文本更改时,如何获取 View 中正在更改的行的位置? Q2:我需要在 Activity 类中实现 onTextChangedListener 吗?如果是这样,我又如何获得在列表中单击的项目的位置?
最佳答案
在getView()
方法回调中将Edittext "qtyDdl"设为final,然后设置位置如下:
qtyDdl.setTag(pos)
在此之后,您应该能够通过调用 Integer.parseInt(qtyDdl.getTag().toString());
关于android - 获取 ListView 项目 onTextChanged 的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40912028/