如何使用android中的Text Watcher从Recycler View适配器获取Edit文本位置

如何使用android中的Text Watcher从Recycler View适配器获取Edit文本位置,第1张

概述我想知道如何从RecyclerView适配器获取编辑文本的位置.我使用过卡片视图中的水平线性布局有三个视图TextView,EditText视图和TextView.我想从RecyclerViewAdapter中的Textwatcher获取特定的EditText位置.适配器:publicclassCartlistAdapterextendsRecyclerView.Adapter<

我想知道如何从Recycler VIEw适配器获取编辑文本的位置.我使用过
卡片视图中的水平线性布局有三个视图TextVIEw,EditText视图和TextVIEw.
我想从Recycler VIEw Adapter中的Textwatcher获取特定的EditText位置.

适配器:

public class Cart@R_403_2978@ extends RecyclerVIEw.Adapter < Cart@R_403_2978@.VIEwHolder > {    private ArrayList < CartItemoriginal > cartListadp;    private ArrayList < Cartitemoringinaltwo > cartListadp2;    displayImageOptions options;    private Context context;    public static final String MyPREFERENCES = "MyPrefs";    public static final String MYCARTPREFERENCE = "CartPrefs";    public static final String MyCartQtyPreference = "CartatyID";    SharedPreferences.Editor editor;    SharedPreferences shared,    wishshared;    SharedPreferences.Editor editors;    String pos,    qtyDelete;    String date;    String currentDateandTime;    private static final int VIEW_TYPE_ONE = 1;    private static final int VIEW_TYPE_TWO = 2;    private static final int TYPE_header = 0;    private Double orderTotal = 0.00;    DecimalFormat df = new DecimalFormat("0");    Double extPrice;    VIEw layout,    layouts;    SharedPreferences sharedPreferences;    SharedPreferences.Editor QutID;    boolean flag = false;    public Cart@R_403_2978@() {    }    public Cart@R_403_2978@(ArrayList < CartItemoriginal > cartListadp, Context context) {        this.cartListadp = cartListadp;        this.cartListadp2 = cartListadp2;        this.context = context;        options = new displayImageOptions.Builder().cacheOndisk(true).cacheInMemory(true).showImageOnLoading(R.drawable.b2)            .showImageForEmptyUri(R.drawable.b2).build();        if (YelloPage.imageLoader.isInited()) {            YelloPage.imageLoader.destroy();        }        YelloPage.imageLoader.init(ImageLoaderConfiguration.createDefault(context));    }    public int getItemVIEwType(int position) {        if (cartListadp.size() == 0) {            Toast.makeText(context, String.valueOf(cartListadp), Toast.LENGTH_LONG).show();            return VIEW_TYPE_TWO;        }        return VIEW_TYPE_ONE;    }    @OverrIDe    public Cart@R_403_2978@.VIEwHolder onCreateVIEwHolder(VIEwGroup vIEwGroup, int position) {        VIEwHolder vIEwHolder = null;        switch (position) {            case VIEW_TYPE_TWO:                VIEw vIEw2 = LayoutInflater.from(vIEwGroup.getContext()).inflate(R.layout.activity_cart, vIEwGroup, false);                vIEwHolder = new VIEwHolder(vIEw2);                // return vIEw holder for your placeholder                break;            case VIEW_TYPE_ONE:                VIEw vIEw = LayoutInflater.from(vIEwGroup.getContext()).inflate(R.layout.cartitemrow, vIEwGroup, false);                vIEwHolder = new VIEwHolder(vIEw);                // return vIEw holder for your normal List item                break;        }        return vIEwHolder;    }    @OverrIDe    public voID onBindVIEwHolder(Cart@R_403_2978@.VIEwHolder vIEwHolder, int position) {        vIEwHolder.productnames.setText(cartListadp.get(position).getProductname());        vIEwHolder.cartalisname.setText(cartListadp.get(position).getAliasname());        vIEwHolder.cartprice.setText("Rs" + " " + cartListadp.get(position).getPrice());        vIEwHolder.cartdelivery.setText(cartListadp.get(position).getDelivery());        vIEwHolder.cartshippin.setText(cartListadp.get(position).getShippincharge());        vIEwHolder.cartsellername.setText(cartListadp.get(position).getSellername());        vIEwHolder.Error.setText(cartListadp.get(position).getError());        vIEwHolder.qty.setTag(cartListadp.get(position));        if (cartListadp.get(position).getQty() != 0) {            vIEwHolder.qty.setText(String.valueOf(cartListadp.get(position).getQty()));        } else {            vIEwHolder.qty.setText("0");        }        YelloPage.imageLoader.displayImage(cartListadp.get(position).getProductimg(), vIEwHolder.cartitemimg, options);        vIEwHolder.qty.setTag(R.ID.quantity, position);    }    @OverrIDe    public int getItemCount() {        return cartListadp.size();    }    public long getItemID(int position) {        return position;    }    public Object getItem(int position) {        return cartListadp.get(position);    }    public class VIEwHolder extends RecyclerVIEw.VIEwHolder {        private TextVIEw productnames, cartalisname, cartprice, cartdelivery, cartshippin, cartsellername, Error, total;        private ImageVIEw cartitemimg;        private Imagebutton wishbtn, removebtn;        private linearLayout removecart, movewishList;        private CardVIEw cd;        private EditText qty;        private ImageVIEw Wishimg;        public VIEwHolder(final VIEw vIEw) {            super(vIEw);            productnames = (TextVIEw) vIEw.findVIEwByID(R.ID.cartitemname);            cartalisname = (TextVIEw) vIEw.findVIEwByID(R.ID.cartalias);            cartprice = (TextVIEw) vIEw.findVIEwByID(R.ID.CartAmt);            cartdelivery = (TextVIEw) vIEw.findVIEwByID(R.ID.cartdel);            cartshippin = (TextVIEw) vIEw.findVIEwByID(R.ID.shipPingcrg);            cartsellername = (TextVIEw) vIEw.findVIEwByID(R.ID.cartSellername);            cartitemimg = (ImageVIEw) vIEw.findVIEwByID(R.ID.cartimg);            Error = (TextVIEw) vIEw.findVIEwByID(R.ID.error);            removecart = (linearLayout) vIEw.findVIEwByID(R.ID.removecart);            movewishList = (linearLayout) vIEw.findVIEwByID(R.ID.movewishList);            Wishimg = (ImageVIEw) vIEw.findVIEwByID(R.ID.wishimg);            qty = (EditText) vIEw.findVIEwByID(R.ID.quantity);            String pID, qID;            sharedPreferences = vIEw.getContext().getSharedPreferences(MYCARTPREFERENCE, Context.MODE_PRIVATE);            QutID = sharedPreferences.edit();            Log.d("position checking1 ---", String.valueOf(getAdapterposition()));            MyTextWatcher textWatcher = new MyTextWatcher(vIEw, qty, getAdapterposition());            //  qty.addTextChangedListener(new MyTextWatcher(vIEw,getAdapterposition()));            qty.addTextChangedListener(textWatcher);            qty.setonKeyListener(new VIEw.OnKeyListener() {                @OverrIDe                public boolean onKey(VIEw v, int keyCode, KeyEvent event) {                    qty.setSelection(qty.getText().length());                    return false;                }            });            wishshared = vIEw.getContext().getSharedPreferences(MyPREFERENCES, context.MODE_PRIVATE);            editors = vIEw.getContext().getSharedPreferences(MyPREFERENCES, context.MODE_PRIVATE).edit();            shared = vIEw.getContext().getSharedPreferences(MYCARTPREFERENCE, context.MODE_PRIVATE);            editor = vIEw.getContext().getSharedPreferences(MYCARTPREFERENCE, context.MODE_PRIVATE).edit();            cd = (CardVIEw) vIEw.findVIEwByID(R.ID.cv);            productnames.setSingleline(false);            productnames.setEllipsize(TextUtils.TruncateAt.END);            productnames.setMaxlines(2);            totalPrice();            vIEw.setClickable(true);        }        public class inputFilterMinMax implements inputFilter {            private int min, max;            public inputFilterMinMax(int min, int max) {                this.min = min;                this.max = max;            }            public inputFilterMinMax(String min, String max) {                this.min = Integer.parseInt(min);                this.max = Integer.parseInt(max);            }            @OverrIDe            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {                try {                    int input = Integer.parseInt(dest.toString() + source.toString());                    if (isInRange(min, max, input))                        return null;                } catch (NumberFormatException nfe) {}                return "";            }            private boolean isInRange(int a, int b, int c) {                return b > a ? c >= a && c <= b : c >= b && c <= a;            }        }        private class MyTextWatcher implements TextWatcher {            private VIEw vIEw;            private EditText editText;            //private int position;            private MyTextWatcher(VIEw vIEw, EditText editText, int adapterposition) {                this.vIEw = vIEw;                this.editText = editText;                // this.position = adapterposition;                //  cartListadp.get(position).getQty() = Integer.parseInt((Caption.getText().toString()));            }            public voID beforeTextChanged(CharSequence s, int start, int count, int after) {                //do nothing            }            public voID onTextChanged(CharSequence s, int start, int before, int count) {                EditText qtyVIEw = (EditText) vIEw.findVIEwByID(R.ID.quantity);                int position = (int) qtyVIEw.getTag(R.ID.quantity);                Log.i("editextpostion", String.valueOf(position));            }            public voID afterTextChanged(Editable s) {                DecimalFormat df = new DecimalFormat("0");                String qtyString = s.toString();                int quantity = qtyString.equals("") ? 0 : Integer.valueOf(qtyString);                String quty = String.valueOf(quantity);                EditText qtyVIEw = (EditText) vIEw.findVIEwByID(R.ID.quantity);                CartItemoriginal product = (CartItemoriginal) qtyVIEw.getTag();                Log.d("postion is qtytag", "position is: " + product);                qtyVIEw.setFilters(new inputFilter[] {                    new inputFilterMinMax(product.getMinquantity(), product.getMaxquantity())                });                if (product.getQty() != quantity) {                    Double currPrice = product.getExt();                    Double price = Double.parseDouble(product.getPrice());                    int maxaty = Integer.parseInt(product.getMaxquantity());                    int minqty = Integer.parseInt(product.getMinquantity());                    if (quantity < maxaty) {                        extPrice = quantity * price;                    } else {                        Toast.makeText(context, "Sorry" + " " + " " + "we are shipPing only" + " " + " " + maxaty + "  " + " " + "unit of quantity", Toast.LENGTH_LONG).show();                    }                    Double priceDiff = Double.valueOf(df.format(extPrice - currPrice));                    product.setQty(quantity);                    product.setExt(extPrice);                    TextVIEw ext = (TextVIEw) vIEw.findVIEwByID(R.ID.CartAmt);                    if (product.getQty() != 0) {                        ext.setText("Rs." + " " + df.format(product.getExt()));                    } else {                        ext.setText("0");                    }                    if (product.getQty() != 0) {                        qtyVIEw.setText(String.valueOf(product.getQty()));                    } else {                        qtyVIEw.setText("");                    }                    totalPrice();                }                return;            }        }        private voID totalPrice() {            int price = 0;            for (int j = 0; j < cartListadp.size(); j++) {                price += Integer.parseInt(cartListadp.get(j).getPrice()) * (cartListadp.get(j).getQty());                String totalprice = String.valueOf(price);                String count = String.valueOf(cartListadp.size());                CartItems.Totalamt.setText(totalprice);                CartItems.cartcount.setText("(" + count + ")");                CartItems.carttotalcount.setText("(" + count + ")");            }        }    }}

提前致谢.

解决方法:

在适配器的onBindVIEwHolder方法中,为EditText设置标记,如下所示:

holder.editText.setTag(position);

在VIEwHolder中,将TextWatcher添加到EditText

public static class VIEwHolder extends RecyclerVIEw.VIEwHolder {    EditText editText ;    public VIEwHolder(VIEw itemVIEw) {        super(itemVIEw);        editText = itemVIEw.findVIEwByID(R.ID.editText);        MyTextWatcher textWatcher = new MyTextWatcher(editText);        editText.addTextChangedListener(textWatcher);    }}

这是你的TextWatcher:

public class MyTextWatcher implements TextWatcher {    private EditText editText;    public MyTextWatcher(EditText editText) {        this.editText = editText;    }    @OverrIDe    public voID beforeTextChanged(CharSequence s, int start, int count, int after) {    }    @OverrIDe    public voID onTextChanged(CharSequence s, int start, int before, int count) {        int position = (int) editText.getTag();        // Do whatever you want with position    }    @OverrIDe    public voID afterTextChanged(Editable s) {    }}

NOTE: Make sure you call setTag method from your EditText before calling setText method, else it will throw NullPointerException. Or alternatively you can add null check when calling getTag method.

更新1

如果您的EditText已经有另一个标记集,请使用ID来标识标记.例如设置标签时使用此:

holder.editText.setTag(R.ID.editText, position);

其中R.ID.editText是您的任何资源的有效ID(有关详细信息,请参阅documentation).

并且在获得价值时:

int position = (int) editText.getTag(R.ID.editText);
总结

以上是内存溢出为你收集整理的如何使用android中的Text Watcher从Recycler View适配器获取Edit文本位置全部内容,希望文章能够帮你解决如何使用android中的Text Watcher从Recycler View适配器获取Edit文本位置所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1111155.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存