Android listview - 限制适配器上的条目

Android listview - limiting the entries on adapter

提问人:Abm 提问时间:6/21/2023 更新时间:6/21/2023 访问量:23

问:

我正在从列表视图添加项目,但条件是项目总数 = 5。因此,可以有 2 种情况:1) 添加 5 个不同的项目,每个项目的数量为 1,2) 多个项目的组合,总数可以为 5。因此,一旦总数为 5,将显示一个停止的警报框。我想到取另一个计数器=5,每当用户添加项目时,获取数量并从计数器中扣除,因此当计数器-0停止用户但无法实现它时。我怎样才能实现案例 2?任何帮助都表示赞赏。

    public void onClick(View arg0) {
    // TODO Auto-generated method stub
    if (arg0.getId() < 500) {
        System.out.println("In delete button");
        View row = (View) arg0.getParent();
        View row1 = (View) row.getParent();
        row1 = (View) row1.getParent();

        ViewGroup container = ((ViewGroup) row1.getParent());
        int id = arg0.getId();

        if (Datafields.docgiftid.containsKey(brand_pres.get(id))) {
            if (!Datafields.brandReminders.equals("NODATA") && FragmentParent.Gifts != 0) {
                Datafields.deletedDocgiftid.put(Integer.parseInt(brand_pres.get(id).toString()), brand_pres.get(id));
                Datafields.deletedDocgiftvalue.put(Integer.parseInt(brand_pres.get(id).toString()), Datafields.docgiftvalue.get(brand_pres.get(id)));
                Datafields.deletedGiftbal.put(Integer.parseInt(brand_pres.get(id).toString()), Datafields.giftbal.get(brand_pres.get(id)));
            }


            brandsid.remove(id);
            brandsname.remove(id);
            brandvalue.remove(id);
            sbrandid.remove(id);

            Datafields.docgiftid.remove(brand_pres.get(id));
            Datafields.docgiftname.remove(brand_pres.get(id));
            //Datafields.docgiftvalue.remove(brand_pres.get(id));
            Datafields.docbrndid.remove(brand_pres.get(id));
            Datafields.docgiftvalue.put(brand_pres.get(id), 0);
        }
        lv.setItemChecked(id, false);
        container.removeView(row1);
        container.invalidate();
    } else {
        if (arg0.getId() < 1000) {
            System.out.println("In increment button");
            int i = arg0.getId() - 500;

            LinearLayout view = (LinearLayout) view1.findViewById(i);
            TextView pickertv = (TextView) view.getChildAt(1);
            String check = pickertv.getText().toString();

            if (check.equals("")) {
                Toast.makeText(getActivity(), "Quantity cannot be Empty", Toast.LENGTH_LONG).show();

            } else {
                count = Integer.parseInt(check);
                System.out.println("check is: " + check + " n c is: " + count);
                if (count >= 9999) {
                    Toast.makeText(getActivity(), "Quantity cannot be greater than 9999", Toast.LENGTH_LONG).show();

                } else {
                    ++count;
                    String text = Integer.toString(count);
                    pickertv.setText(text);
                    Datafields.docgiftvalue.put(brand_pres.get(i), text);
                    brandvalue.put(i, text);
                }
            }
        } else if (arg0.getId() >= 1000) {

            System.out.println("In decrement button");
            int i = arg0.getId() - 1000;
            LinearLayout view = (LinearLayout) view1.findViewById(i);
            TextView tv = (TextView) view.getChildAt(1);
            String check = tv.getText().toString();

            if (check.equals("")) {
                Toast.makeText(getActivity(), "Quantity cannot be Empty", Toast.LENGTH_LONG).show();

            } else {
                int c = Integer.parseInt(check);
                System.out.println("check is: " + check + " n c is: " + c);
                if (c <= 0) {
                    Toast.makeText(getActivity(), "Quantity cannot be less than zero", Toast.LENGTH_LONG).show();
                } else {
                    c--;
                    String text = Integer.toString(c);
                    tv.setText(text);
                    Datafields.docgiftvalue.put(brand_pres.get(i), text);
                    brandvalue.put(i, text);
                }
            }
        }
    }

}
void showTable(final int position, int FLAG, String id) {
    System.out.println("In showtable");
    mRowCount++;
    FragmentActivity act = getActivity();
    View child = act.getLayoutInflater().inflate(R.layout.listview_adapter_filedcr_promogift, null);
    TextView promo_product = (TextView) child.findViewById(R.id.promo_product);
    final EditText promo_quantity = (EditText) child.findViewById(R.id.promo_quantity);
    ImageView promo_delete = (ImageView) child.findViewById(R.id.delete);
    Button promo_dec = (Button) child.findViewById(R.id.decrement);
    Button promo_inc = (Button) child.findViewById(R.id.increment);
    LinearLayout lin_promo = (LinearLayout) child.findViewById(R.id.lin_promo);
    promo_product.setText(namecount.get(position));
    promo_delete.setId(position);
    promo_delete.setOnClickListener(this);//added by sid
    lin_promo.setId(position);//added by sid
    promo_inc.setId(500 + position);//added by sid
    promo_inc.setOnClickListener(this);//added by sid
    promo_quantity.setId(position + 1500);//added bys sid

    if (FLAG == 1) {
        System.out.println("In showtable and flag=1");
        promo_quantity.setText("" + Datafields.docgiftvalue.get(Integer.parseInt(listid.get(position))));//added bys sid
    } else {

        System.out.println("In showtable and flag!=1");
        if (id.equals("0")) {
            promo_quantity.setText("0");//added bys sid
        } else {
            promo_quantity.setText("1");//added bys sid
        }
    }
    lin_promo.setId(position);//added by sid

    if (id.equals("0")) {
        promo_inc.setEnabled(false);//added by sid
        promo_quantity.setEnabled(false);
        promo_dec.setEnabled(false);

        promo_inc.setVisibility(View.INVISIBLE);//added by sid
        promo_quantity.setVisibility(View.INVISIBLE);
        promo_dec.setVisibility(View.INVISIBLE);

    }


    promo_dec.setId(1000 + position);//added by sid
    promo_dec.setOnClickListener(this);//added by sid
 @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            float v1, v2;
            String valueInString = promo_quantity.getText().toString();//added by sid
            if (valueInString.length() == 0) {
                valueInString = "0";
            }
            Datafields.docgiftvalue.put(brand_pres.get(position), valueInString);

        }


    });

    table.addView(child);

    ((ScrollView) view1.findViewById(R.id.myScroll)).post(new Runnable() {
        @Override
        public void run() {
            System.out.println("In scrollview");
            ((ScrollView) view1.findViewById(R.id.myScroll)).fullScroll(View.FOCUS_DOWN);
        }
    });

}

Image 1

Image 2

Image 3

java android-fragments android-recyclerview android-listview

评论


答: 暂无答案