我有一個餐館的菜單列表視圖,在列表視圖中每行有:項目的獲取列表視圖項的位置onTextChanged
- 項目的圖像 項目
- 名
- 價格
- 編輯文本進入的數量
- 複選框以選擇項
爲了迎合我有一個MenuItem對象類與上面提到的屬性。
我已經實現了自定義適配器的列表視圖。現在我需要在EditText上實現onTextChangedListener,以便每當數量發生變化時,我的對象都會更新,因此會反映在最終訂單帳單中。我已經設置了默認數量爲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;}
}
}
Q1:我如何獲取在視圖中,每當被更改的行的位置一個textchanged發生? Q2:我需要在Activity類中實現onTextChangedListener嗎?如果是這樣,我又如何得到在列表中點擊的項目的位置?
我textChangeListener也存在getview方法內。它是正確的還是應該在活動中定義?列表視圖控件存在的位置。 – Samra
感謝它幫助我 –