2016-12-01 61 views
1

我有一个餐馆的菜单列表视图,在列表视图中每行有:项目的获取列表视图项的位置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吗?如果是这样,我又如何得到在列表中点击的项目的位置?

回答

0

得到了哈恩的想法,这是我做过什么getView函数内部

@Override 
    public View getView(int pos, View convertView, ViewGroup parent){ 
     try{ 

      android.view.LayoutInflater inflator = android.view.LayoutInflater.from(getContext()); 

      final View customView = inflator.inflate(R.layout.itemdetail, parent, false); 

      final MenuItem singleItem = getItem(pos); 
      //ImageView iconImg = (ImageView) customView.findViewById(R.id.imgMenuItem); 
      TextView nameTxt = (TextView) customView.findViewById(R.id.nameText); 
      final 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){ 
        //pos is the item clicked 
         //activity.SetQuantity(customView.getId(), s); 
         try{ 
         singleItem.setQty(Integer.parseInt(qtyDdl.getText().toString())); 
         } 
         catch(NumberFormatException e){} 
        } 

        @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;} 
    } 
2

充分利用EDITTEXT“qtyDdl”最终在getView()方法回调,然后设置位置如下: qtyDdl.setTag(pos)

在这之后,你应该能够通过调用Integer.parseInt(qtyDdl.getTag().toString());来从textChangeListener位置

+0

我textChangeListener也存在getview方法内。它是正确的还是应该在活动中定义?列表视图控件存在的位置。 – Samra

+0

感谢它帮助我 –