android - 获取 ListView 项目 onTextChanged 的​​位置

标签 android listview android-edittext

我在 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());

从 textChangeListener 获取位置

关于android - 获取 ListView 项目 onTextChanged 的​​位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40912028/

相关文章:

java - BaseAdapter OnItemClickListener 从未被调用

android - ListView 中的 setOnItemClickListener 影响多行

c# - 为什么 SubItems.Clear() 也会删除 Name 属性?

android - 如何避免两种口味的应用打开同一应用?

android - 在 Activity 组中添加新 Activity 时出现 java.lang.StackOverflowError

android - PhoneGap 在尝试构建 Android APK 时构建 Give Error

android - 如何在警告对话框中缩放图像

android - 如何禁止在 Android EditText 中输入 GIF?

android - EditText中的多种语言,不带特殊字符

android - 试图在另一个 Activity 中将两个 EditTexts 的产品显示为 TextView