Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 获取listview项onTextChanged的位置_Android_Listview_Android Edittext - Fatal编程技术网

Android 获取listview项onTextChanged的位置

Android 获取listview项onTextChanged的位置,android,listview,android-edittext,Android,Listview,Android Edittext,我在列表视图中有一个餐厅菜单,列表视图中的每一行都有: 物品的图像 项目名称 商品价格 编辑文本以输入数量 复选框以选择项目 为了满足需要,我有一个具有上述属性的MenuItem对象类 我已经为listview实现了自定义适配器。现在我需要在EditText上实现onTextChangedListener,这样每当数量发生变化时,我的对象都会得到更新,从而反映在最终的订单清单中。我已将默认数量设置为1 我的适配器类 public class MenuAdapter extends Array

我在列表视图中有一个餐厅菜单,列表视图中的每一行都有:

  • 物品的图像
  • 项目名称
  • 商品价格
  • 编辑文本以输入数量
  • 复选框以选择项目
为了满足需要,我有一个具有上述属性的MenuItem对象类

我已经为listview实现了自定义适配器。现在我需要在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;}
    }
public类MenuAdapter扩展了ArrayAdapter{
私人语境;
公共菜单适配器(上下文、int textViewResourceId、列表对象){
超级(上下文、textViewResourceId、对象);
this.context=上下文;
}
@凌驾
公共视图getView(int pos、视图转换视图、视图组父视图){
试一试{
android.view.LayoutInflater充气器=android.view.LayoutInflater.from(getContext());
视图customView=充气机。充气(R.layout.itemdetail,父项,false);
MenuItem singleItem=getItem(pos);
//ImageView图标长度=(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);
复选框选择chk=(复选框)customView.findviewbyd(R.id.chkItem);
nameTxt.setText(singleItem.getName());
qtyDdl.setText(“1”);
qtyDdl.addTextChangedListener(新的TextWatcher()
{@覆盖
public void onTextChanged(字符序列、int start、int before、int count){
int i=开始;
}
@凌驾
更改前文本之前的公共void(字符序列s、int start、int count、int after){
//TODO自动生成的方法存根
}
@凌驾
公共无效后文本已更改(可编辑){
//TODO自动生成的方法存根
}});
priceTxt.setText(singleItem.getPrice());
选择chk.setOnCheckedChangeListener((SaltNPeperActivity)上下文);
返回自定义视图;
}捕获(例外e){
Log.e(“MenuAdapter”,e.toString());
返回null;}
}
}

问题1:每当发生文本更改时,如何获取视图中正在更改的行的位置? 问题2:我是否需要在活动类中实现onTextChangedListener?如果是这样,我又该如何获得列表中单击的项目的位置呢?

getView()
方法回调中将Edittext“qtyDdl”设为final,然后设置位置如下:
qtyDdl.setTag(pos)


在此之后,您应该能够通过调用
Integer.parseInt(qtyDdl.getTag().toString())从textChangeListener获取位置

从Hahn那里得到了这个想法,这就是我在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;}
    }

我的textChangeListener也存在于getview方法中。它是正确的还是应该在活动中定义?listview控件所在的位置。?感谢它的帮助