如果项目名称相同,则Arraylist重复条目

时间:2015-03-20 08:08:09

标签: java android listview android-fragments arraylist

我的代码

中遇到了问题

该方案

从微调器添加项目时,它会被添加到侧面列表中 并且在按下订单按钮之前第一次将其文本设置为蓝色,您也可以修改数量 enter image description here

- 你添加第二个项目 enter image description here

- 按下订单按钮后,订单信息被发送到数据库,文本设置为黄色,订单已下,您不应该修改项目 enter image description here

- 您将不同的项添加到下一个订单enter image description here

- 你下了第二个订单 enter image description here

- 如果您添加相同的项目'Captain Morgan',那么它会被添加到第二个订单enter image description here

以下是处理上述操作的所有代码

我正在使用两个片段MainFragment和SideFragment

SidePanelFragment.java

    public class SidePanelFragment extends android.app.Fragment {

        ListView list1;



        ArrayList<Items> itemsArray = new ArrayList<Items>();
        ItemsAdapter1 adapter = null;
        boolean isPresent = false;
        Button btnCancel;
        Button btnOrder;


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


            View view = inflater.inflate(R.layout.fragment_side_panel, container, false);
            list1 = (ListView) view.findViewById(R.id.listViewSide);
            btnCancel=(Button)view.findViewById(R.id.button);
            btnCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for(int i=0;i<itemsArray.size();i++){
                       if(!itemsArray.get(i).Ordered)
                           itemsArray.remove(i);
                    }
                    //itemsArray.clear();
                    adapter.notifyDataSetChanged();
                }
            });

            btnOrder=(Button)view.findViewById(R.id.btnOrder);
            btnOrder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    for(int i=0;i<itemsArray.size();i++){

                        if(!itemsArray.get(i).Ordered) {
                           // itemsArray.get(i).
                            itemsArray.get(i).Ordered = true;
                        }
                    }
                    adapter.notifyDataSetChanged();
                }
            });
            adapter = new ItemsAdapter1(getActivity(), itemsArray);
            adapter.notifyDataSetChanged();
            list1.invalidateViews();
            list1.setAdapter(adapter);
            return view;
        }



        public void getAddedItems(Items item) {


            for (int i = 0; i < itemsArray.size(); i++) {
                if (item.ItemName == itemsArray.get(i).ItemName) {
                    int quant = itemsArray.get(i).prevQuant;
                    item.quantity = item.quantity + quant;


                    itemsArray.set(i, item);

                    itemsArray.get(i).prevQuant = item.quantity;

                    isPresent = true;

                }
            }

            if (!isPresent) {

                itemsArray.add(item);
                itemsArray.get(itemsArray.size() - 1).prevQuant = item.quantity;


            }



            isPresent = false;
            adapter = new ItemsAdapter1(getActivity(), itemsArray);
            adapter.notifyDataSetChanged();
            list1.invalidateViews();
            list1.setAdapter(adapter);


        }



        class ItemsAdapter1 extends BaseAdapter {


            Context context;
            ArrayList<Items> itemList;

            ItemsAdapter1(Context context, ArrayList<Items> itemList1) {
                itemList = new ArrayList<Items>();
                this.context = context;
                itemList = itemList1;


            }


            @Override
            public int getCount() {
                return itemList.size();
            }

            @Override
            public Object getItem(int position) {
                return itemList.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }


            class ViewHolder {

                TextView txtView1;
                TextView txtView2;
                TextView txtView3;
                ImageButton imgBtn;
                Button btnQuantPlus;
                Button btnQuantMinus;

                ViewHolder(final View V) {

                    txtView1 = (TextView) V.findViewById(R.id.txtSideItemName);
                    txtView2 = (TextView) V.findViewById(R.id.txtSideQuant);
                    txtView3 = (TextView) V.findViewById(R.id.txtAmountSide);
                    //if (itemList.get().Ordered==false)
                    imgBtn = (ImageButton) V.findViewById(R.id.btndelete);
                    btnQuantPlus=(Button)V.findViewById(R.id.btnQuantPlus);
                    btnQuantMinus=(Button)V.findViewById(R.id.btnQuantMinus);


                }


            }


            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {

                LayoutInflater inflator = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
                View row = convertView;
                ViewHolder holder = null;
                if (row == null) {


                    row = inflator.inflate(R.layout.side_panel_list, parent, false);
                    holder = new ViewHolder(row);


                } else {
                    holder = (ViewHolder) row.getTag();
                }
                Items it = itemList.get(position);
                holder.txtView1.setText(it.ItemName);
                holder.txtView2.setText(String.valueOf(it.Price));
                holder.txtView3.setText(String.valueOf(it.quantity));

                if(itemList.get(position).Ordered==false) {
                    holder.btnQuantMinus.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if ((itemList.get(position).quantity) > 1) {
                                Items it = new Items();
                                it = itemList.get(position);

                                it.quantity = it.prevQuant - 1;
                                it.prevQuant = it.quantity;
                                itemList.set(position, it);
                                adapter.notifyDataSetChanged();
                            }


                        }

                    });


                    holder.btnQuantPlus.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            // if(it.quantity>1)
                            Items it = new Items();
                            it = itemList.get(position);

                            it.quantity = it.prevQuant + 1;
                            it.prevQuant = it.quantity;
                            itemList.set(position, it);
                            adapter.notifyDataSetChanged();


                        }
                    });


                    holder.imgBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            itemList.remove(position);
                            adapter.notifyDataSetChanged();


                        }
                    });
                }
                else
                {
                    holder.imgBtn.setVisibility(View.GONE);
                    holder.btnQuantMinus.setVisibility(View.GONE);
                    holder.btnQuantPlus.setVisibility(View.GONE);
                }


                //holder.imgBtn.setVisibility(View.GONE);

                row.setTag(holder);


                holder.txtView3.setText(String.valueOf(itemList.get(position).quantity));
                setRowColor(row,position);
                return row;
            }

            private void setRowColor(View view, int var) {
                if(itemList.get(var).Ordered==true){
                    view.setBackgroundColor(Color.parseColor("#e7ff00"));
                }else
                    view.setBackgroundColor(Color.parseColor("#4585f2"));

                    //view.setBackgroundColor(Color.rgb(0,0,255));

            }

        }
    }

MainPanelFragment.java

public class MainPanelFragment extends android.app.Fragment implements View.OnClickListener {

    Spinner spinner;
    //ArrayList<String> listCatagories= null;
    ItemsAdapter adapter;
    ArrayAdapter adapter1;
    CategoriesAdapter catAdapter;
    ListView list;
    TextView itemName;
    TextView itemPrice;
    EditText quantity;
    Button quantPlus;
    Button quantMinus;
    Button addItem;
    int quantCount=1;

    ArrayList<Categories> catForAdapter=null;
    public String selectedItemName="";
    public double selectedItemPrice=0;
    public Items  selectedItem=null;
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {


        View view =inflater.inflate(R.layout.fragment_order_menu,container,false);

        spinner=(Spinner)view.findViewById(R.id.spinnerCatagory);

        list=(ListView)view.findViewById(R.id.listView);

        itemName=(TextView)view.findViewById(R.id.textViewItem);
        itemName.setText("Select an item");

        quantity=(EditText)view.findViewById(R.id.editText);
        quantity.setText(String.valueOf(quantCount));
        quantity.setEnabled(false);

        quantPlus=(Button)view.findViewById(R.id.buttonPlus);
        quantMinus=(Button)view.findViewById(R.id.buttonMinus);
        //addItem=(Button)view.findViewById(R.id.btnAdd);

        quantPlus.setEnabled(false);
        quantMinus.setEnabled(false);



        itemPrice=(TextView)view.findViewById(R.id.textViewPrice);


        final ArrayAdapter<String> spinnerArrayAdapter =new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, catagories);
        spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);

        catAdapter= new CategoriesAdapter(this.getActivity(),catForAdapter);
        spinner.setAdapter(catAdapter);        

 (this.getActivity(),android.R.layout.simple_list_item_1, drinks);


        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               selectedItem=(Items)parent.getItemAtPosition(position);

                selectedItemName=selectedItem.ItemName;
                selectedItemPrice=selectedItem.Price;
                itemName.setText(selectedItemName);
                itemPrice.setText(String.valueOf(selectedItemPrice));

                quantPlus.setEnabled(true);
                quantMinus.setEnabled(true);
                quantCount=1;
                quantity.setText(String.valueOf(quantCount));
                addItem.setEnabled(true);
               // Toast.makeText(getActivity().getApplicationContext(),selectedItem, Toast.LENGTH_LONG).show();
            }
        });

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {

                addItem.setEnabled(false);
                Categories ct= new Categories();
               ct= (Categories)spinner.getSelectedItem();
                    SoapCallClass s= new SoapCallClass();


                adapter=new ItemsAdapter(getActivity(),s.getItemsForCategory(ct.TOUCHCODE));
                quantCount=1;
                quantity.setText(String.valueOf(quantCount));
                itemName.setText("Select Item");
                itemPrice.setText("0.00");
                quantPlus.setEnabled(false);
                quantMinus.setEnabled(false);

                list.setAdapter(adapter);
            }



            @Override
            public void onNothingSelected(AdapterView<?> parentView) {

            }

        });

        quantPlus.setOnClickListener(
                new View.OnClickListener() {

                    public void onClick(View v) {

                            quantCount++;
                        quantity.setText(String.valueOf(quantCount));
                        itemPrice.setText(String.valueOf(quantCount*selectedItemPrice));

                        }


                }
        );

        quantMinus.setOnClickListener(
                new View.OnClickListener() {

                    public void onClick(View v) {
                        if(quantCount>1)
                        quantCount--;
                        quantity.setText(String.valueOf(quantCount));
                        itemPrice.setText(String.valueOf(quantCount*selectedItemPrice));

                    }


                }
        );


        return  view;
    }
    OnAddItemPressed addItemPressed;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        addItemPressed=(OnAddItemPressed)getActivity();
        addItem=(Button)getActivity().findViewById(R.id.btnAdd);
        addItem.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        selectedItem.quantity=  quantCount;
       selectedItem.Ordered=false;
        addItemPressed.onAddItemPressed(selectedItem);
        addItem.setEnabled(false);
        quantCount=1;
        quantity.setText(String.valueOf(quantCount));
        itemName.setText("Select Item");
        itemPrice.setText("0.00");
        quantPlus.setEnabled(false);
        quantMinus.setEnabled(false);
    }
}

class Items{

    public String ItemName;
    public int ItemCode;
    public double Price;
    public int quantity;
    public int prevQuant;

    public String RESCODE;
    public int KOTDATE;
    public int KOTNUB;
    public int KOTTYP;
    public int STEWCODE;

    public String TBLNUB;
    public int PAXPER;
    public String PERSON;
    public String SESSON;
    public int SRLNUMB;

    public float ITMQTY;
    public float ITMRAT;
    public float ITMVAL;

    public int TAXSTR;
    public String KITCOD;

    public int MENTYP;
    public int GRPCOD;
    public int BILNUB;
    public int UPDFLG;
    public int BILSEQ;
    public int LSTDAT;

    public String LSTTIM;
    public String USERID;

    public float NCKPER;
    public String MODNAM;
    public String RORDER;

    public boolean Ordered;

}


class ItemsAdapter extends BaseAdapter {

    ArrayList<Items> itemList;
    Context context;

    ItemsAdapter(Context context, ArrayList<Items> itemsArray) {
        itemList = new ArrayList<Items>();
        this.context = context;
        itemList=itemsArray;

    }


    @Override
    public int getCount() {
        return itemList.size();
    }

    @Override
    public Object getItem(int position) {
        return itemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }




    class ViewHolder {


        TextView txtView1;
        TextView txtView2;

        ViewHolder(View V) {
            txtView1 = (TextView) V.findViewById(R.id.txtItemName);
            txtView2=(TextView) V.findViewById(R.id.txtItemPrice);
        }
    }

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

        LayoutInflater inflator = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        View row = convertView;
        ViewHolder holder = null;
        if (row == null) {


            row = inflator.inflate(R.layout.single_item, parent, false);
            holder = new ViewHolder(row);
            row.setTag(holder);

        } else {
            holder = (ViewHolder) row.getTag();
        }
        Items it = itemList.get(position);
        holder.txtView1.setText(it.ItemName);

        holder.txtView2.setText(String.valueOf(it.Price));

        return row;
    }

}


class CategoriesAdapter extends BaseAdapter {

    ArrayList<Categories> catList;
    Context context;

    CategoriesAdapter(Context context, ArrayList<Categories> catArray) {
        catList = new ArrayList<Categories>();
        this.context = context;
        catList=catArray;

    }


    @Override
    public int getCount() {
        return catList.size();
    }

    @Override
    public Object getItem(int position) {
        return catList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }



    class ViewHolder {


        TextView txtView1;
        //TextView txtView2;

        ViewHolder(View V) {
            txtView1 = (TextView) V.findViewById(R.id.txtcatrel);
           // txtView2=(TextView) V.findViewById(R.id.txtItemPrice);
        }
    }

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

        LayoutInflater inflator = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        View row = convertView;
        ViewHolder holder = null;
        if (row == null) {


            row = inflator.inflate(R.layout.categorylyt, parent, false);
            holder = new ViewHolder(row);
            row.setTag(holder);

        } else {
            holder = (ViewHolder) row.getTag();
        }
        Categories ct = catList.get(position);
        holder.txtView1.setText(ct.Name);
        return row;
    }

}

从主面板按下添加项目按钮的代码(文本几乎不可见的按钮)

   OnAddItemPressed addItemPressed;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        addItemPressed=(OnAddItemPressed)getActivity();
        addItem=(Button)getActivity().findViewById(R.id.btnAdd);
        addItem.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        selectedItem.quantity=  quantCount;
       selectedItem.Ordered=false;
        addItemPressed.onAddItemPressed(selectedItem);
        addItem.setEnabled(false);
        quantCount=1;
        quantity.setText(String.valueOf(quantCount));
        itemName.setText("Select Item");
        itemPrice.setText("0.00");
        quantPlus.setEnabled(false);
        quantMinus.setEnabled(false);
    }
}

在Menu.java中实现的接口代码

@Override
public void onAddItemPressed(Items item){

    FragmentManager manager= getFragmentManager();

    SidePanelFragment sideObj= (SidePanelFragment) manager.findFragmentById(R.id.side_panel);
    sideObj.getAddedItems(item);


}

下订单的代码

     btnOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                for(int i=0;i<itemsArray.size();i++){

                    if(!itemsArray.get(i).Ordered) {
                       // itemsArray.get(i).
                        itemsArray.get(i).Ordered = true;
                    }
                }
                adapter.notifyDataSetChanged();
            }
        });
        adapter = new ItemsAdapter1(getActivity(), itemsArray);
        adapter.notifyDataSetChanged();
        list1.invalidateViews();
        list1.setAdapter(adapter);
        return view;
    }

用于填充和显示列表视图中的项目的代码

    public void getAddedItems(Items item) {


        for (int i = 0; i < itemsArray.size(); i++) {
            if (item.ItemName == itemsArray.get(i).ItemName) {
                int quant = itemsArray.get(i).prevQuant;
                item.quantity = item.quantity + quant;


                itemsArray.set(i, item);

                itemsArray.get(i).prevQuant = item.quantity;

                isPresent = true;

            }
        }

        if (!isPresent) {

            itemsArray.add(item);
            itemsArray.get(itemsArray.size() - 1).prevQuant = item.quantity;


        }



        isPresent = false;
        adapter = new ItemsAdapter1(getActivity(), itemsArray);
        adapter.notifyDataSetChanged();
        list1.invalidateViews();
        list1.setAdapter(adapter);


    }

将商品添加到订单并下订单后,来自Ordered类的Item的{​​{1}}属性设置为true

现在, 如果您选择商品并点击下订单然后添加相同的商品,订购商品Items属性会在下订单后立即成为Ordered false它会被添加到那个地方并变成蓝色,这不应该发生。如果你添加不同的项目并下订单,那么这个问题就不会出现了。希望我已经正确地解释了我的问题。

2 个答案:

答案 0 :(得分:0)

您在项目本身上设置了有序标志,并在您点击后立即将其重置为false。您应该考虑在引用项目本身之外的其他地方管理有序标志。您可能希望使用已订购项目列表来管理订单对象。

有缺陷的代码

@Override
    public void onClick(View v) {
        selectedItem.quantity=  quantCount;
        selectedItem.Ordered=false; // Here you reset the flag of "reference" item
        addItemPressed.onAddItemPressed(selectedItem);
        addItem.setEnabled(false);
        quantCount=1;
        quantity.setText(String.valueOf(quantCount));
        itemName.setText("Select Item");
        itemPrice.setText("0.00");
        quantPlus.setEnabled(false);
        quantMinus.setEnabled(false);
    }

答案 1 :(得分:0)

此代码段解决了问题

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

               selectedItem=new Items();

          Items getItms=(Items)parent.getItemAtPosition(position);

            selectedItem.ItemCode=getItms.ItemCode;
            selectedItem.ItemName=getItms.ItemName;
            selectedItem.Price=getItms.Price;

                selectedItemName=selectedItem.ItemName;
                selectedItemPrice=selectedItem.Price;
                itemName.setText(selectedItemName);
                itemPrice.setText(String.valueOf(selectedItemPrice));

                quantPlus.setEnabled(true);
                quantMinus.setEnabled(true);
                quantCount=1;
                quantity.setText(String.valueOf(quantCount));
                addItem.setEnabled(true);
               // Toast.makeText(getActivity().getApplicationContext(),selectedItem, Toast.LENGTH_LONG).show();
            }
        });