如何在listview复选框和edittext值中进行onresume

时间:2016-04-14 08:02:20

标签: java android

在我的应用程序中,我有listview with checkbox和edittext(quantity),最后在fragment1中提交按钮。我选择一些复选框并在edittext中输入值,然后单击“提交”按钮并将其移至fragment2。

我的问题是..如果我再次回到fragment1意味着,我想检查一下我选择的复选框,并在editext中设置数量值。怎么办。

请有人帮帮我..

我的代码:

片段:

public class LunchFragment extends Fragment {

    public static class MenuItem implements Parcelable {
        String ItemName;
        String Amount;
        String Quantity;
        Boolean selFlag;

        public MenuItem(String name, String amt, String qty) {
            this.ItemName = name;
            this.Amount = amt;
            this.Quantity = qty;
            this.selFlag = false;
        }

            public MenuItem(Parcel parcel) {
            this.ItemName = parcel.readString();
            this.Amount = parcel.readString();
            this.Quantity = parcel.readString();
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel out, int flags) {
            Log.d("Check what is in Parcel", out.toString() + out.readString());
            out.writeString(ItemName);
            out.writeString(Amount);
            out.writeString(Quantity);
        }


        public static Creator<MenuItem> CREATOR = new Creator<MenuItem>() {
            public MenuItem createFromParcel(Parcel in) {
                return new MenuItem(in);
            }
            public MenuItem[] newArray(int size) {
                return new MenuItem[size];
            }
        };
    }
    private DBHelper mydb ;
    private  String quantitys = "1";
    ListView menuList;

    ArrayList<MenuItem> itemList = new ArrayList<MenuItem>();
    ArrayList<MenuItem> selList = new ArrayList<MenuItem>();
    MenuListCustomAdapter adapter;
    Button order;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.menu_list_fragment, container, false);
        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        mydb = new DBHelper(this.getActivity());
        menuList = (ListView)view.findViewById(R.id.menuItem_list);
        order = (Button)view.findViewById(R.id.order_button);

        ArrayList<String> item_list = new ArrayList<String>();
        ArrayList<String> price_list = new ArrayList<String>();
        item_list = mydb.getAllMenuItemName();
        price_list = mydb.getAllMenuItemPrice();

        for(int i=0; i<item_list.size();i++){
            itemList.add(new MenuItem(item_list.get(i), price_list.get(i), quantitys));
        }

        adapter = new MenuListCustomAdapter(itemList, this.getActivity());
        menuList.setAdapter(adapter);


        order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selList.clear();
                Log.d("MenuListCount", String.valueOf(menuList.getCount()) +"--"+ adapter.getCount());
                for(int i=0; i<menuList.getCount();i++) {
                    MenuItem item = (MenuItem) adapter.getItem(i);
                    Log.d("MenuChild", i + " - " + itemList.size() + "," + item.ItemName + "," + item.selFlag + "," + item.Quantity);
                    if(item.selFlag) {
                        selList.add(new MenuItem(item.ItemName, String.valueOf(Integer.parseInt(item.Quantity) * Integer.parseInt(item.Amount)), item.Quantity));
                        //item.selFlag = false;
                    }
                }
                if(selList.size() > 0) {
                    Intent accIntent = new Intent(getActivity(), OrderActivity.class);
                    accIntent.putExtra("item", "Order");
                    accIntent.putExtra("itemList", selList);
                    startActivity(accIntent);
                } else {
                    Toast.makeText(getActivity().getApplicationContext(), "Please pick atleast from menu", Toast.LENGTH_SHORT).show();
                }
            }
        });
        return view;
    }

    public void onResume() {
        super.onResume();
        itemList.clear();
        ArrayList<String> item_list = new ArrayList<String>();
        ArrayList<String> price_list = new ArrayList<String>();
        item_list = mydb.getAllMenuItemName();
        price_list = mydb.getAllMenuItemPrice();

        for(int i=0; i<item_list.size();i++){
            itemList.add(new MenuItem(item_list.get(i), price_list.get(i), quantitys));
        }

        adapter = new MenuListCustomAdapter(itemList, this.getActivity());
        menuList.setAdapter(adapter);
        adapter.notifyDataSetChanged();
  }

}

CustomAdapter:

public class MenuListCustomAdapter extends BaseAdapter implements ListAdapter {
    private ArrayList<LunchFragment.MenuItem> list = new ArrayList<LunchFragment.MenuItem>();
    private Context context;
    private DBHelper mydb ;

    public MenuListCustomAdapter(ArrayList<LunchFragment.MenuItem> list, Context context) {
        this.list = list;
        this.context = context;
        mydb = new DBHelper(context);
    }

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

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final ViewHolder holder;
        Log.d("position", String.valueOf(position));
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) ((Activity)context).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.menu_custom_fragment,null);
            holder = new ViewHolder();

            holder.listItemText = (TextView)view.findViewById(R.id.menu_list_item_string);
            holder.listAmtText = (TextView)view.findViewById(R.id.menu_amt_list_item);
            holder.qty = (EditText)view.findViewById(R.id.quantity);
            holder.checkBox = (CheckBox)view.findViewById(R.id.chckbox);
            holder.plus = (ImageView)view.findViewById(R.id.add);
            holder.minus = (ImageView)view.findViewById(R.id.minus);
            holder.info = (ImageView)view.findViewById(R.id.information);

            view.setTag(holder);
        }else{
            holder=(ViewHolder)view.getTag();
        }


        holder.qty.setFilters(new InputFilter[]{
                new InputFilter() {
                    @Override
                    public CharSequence filter(CharSequence cs, int start,
                                               int end, Spanned spanned, int dStart, int dEnd) {
                        // TODO Auto-generated method stub
                        if (cs.equals("")) { // for backspace
                            return cs;
                        }
                        if (cs.toString().matches("[0-9]+")) {
                            return cs;
                        }
                        return "";
                    }
                }
        });

        holder.listItemText.setText(list.get(position).ItemName);
        holder.listAmtText.setText(list.get(position).Amount);
        holder.qty.setText(list.get(position).Quantity);
        holder.checkBox.setChecked(list.get(position).selFlag);
        Log.d("QTY Id ", holder.qty.toString() + '@' + String.valueOf(position)+"--"+list.get(position).ItemName);

        holder.info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String itemName = list.get(position).ItemName;
                Log.d("Selected Item",itemName);
                ArrayList<String> temp_item_list = new ArrayList<String>();
                temp_item_list = mydb.getComboItem(itemName);
                List<String> temp = new ArrayList<String>();
                Log.d("ComboListSize", String.valueOf(temp_item_list.size()));
                for(int i=0; i<temp_item_list.size();i++){
                    String[] a= temp_item_list.get(i).split("\\$");
                    for(int x=0;x<a.length;x++){
                        temp.add(a[x]);
                    }

                }
                //Create sequence of items
                final CharSequence[] chars = temp.toArray(new String[temp.size()]);
                AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
                dialogBuilder.setTitle(list.get(position).ItemName + " Inside");
                dialogBuilder.setItems(chars, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int item) {
                        String selectedText = chars[item].toString();  
                    }
                });
                //Create alert dialog object via builder
                AlertDialog alertDialogObject = dialogBuilder.create();
                //Show the dialog
                alertDialogObject.show();
            }
        });

        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("Check Box Click", String.valueOf(position));
                if(holder.checkBox.isChecked()){
                    list.get(position).selFlag = true;
                } else {
                    holder.qty.setText("1");
                    list.get(position).selFlag = false;
                }
            }
        });

            holder.plus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(list.size()>0){
                            list.get(position).selFlag = true;
                            holder.checkBox.setChecked(list.get(position).selFlag);
                            String value = list.get(position).Quantity.toString();
                            Log.d("plus Position on Click", String.valueOf(position));
                            int count = Integer.parseInt(value)+1;
                            list.get(position).Quantity = String.valueOf(count);
                            holder.qty.setText(String.valueOf(count));
                            Log.d("plus value", String.valueOf(count));

                    }
                }
            });
            holder.minus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String value = list.get(position).Quantity.toString();
                    if(Integer.parseInt(value)>1){
                        Log.d("minus Position on Click", String.valueOf(position));
                        int count = Integer.parseInt(value)-1;
                        list.get(position).Quantity = String.valueOf(count);
                        holder.qty.setText(String.valueOf(count));
                        Log.d("subtarct value", String.valueOf(count));
                 }
                }
            });

        return view;
    }

    public static class ViewHolder{
        public TextView listItemText;
        public TextView listAmtText;
        public EditText qty;
        public CheckBox checkBox;
        public ImageView plus;
        public ImageView minus;
        public ImageView info;
    }

}

提前致谢!

2 个答案:

答案 0 :(得分:0)

holder.VIEW.set..方法中删除所有onClick()值。并更新list中的值并在下方添加以下行 - notifyItemChanged(position);

还可以为每个视图使用setTag(position);。并使用onClick()方法检索它们。

检查我正在使用的代码 -

holder.chkFavorite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = (int) v.getTag();
                if (objects.get(position).getProduct().isProductWishlistStatus())
                    objects.get(position).getProduct().setProductWishlistStatus(true);
                else
                    objects.get(position).getProduct().setProductWishlistStatus(false);
                notifyItemChanged(position);
            }
        });
holder.chkFavorite.setChecked(objects.get(position).getProduct().isProductWishlistStatus());
holder.chkFavorite.setText(String.valueOf(objects.get(position).getProduct().getProductWishlistCount()));
holder.chkFavorite.setTag(position);

答案 1 :(得分:0)

也许我没有遇到所有问题,但我建议你这样做:

1.如果您保留包含所有更改的列表数据 - 返回fragment时,只需致电adapter.notifyDataSetChanged()即可。这将显示更新的数据。

2.如果您将LunchFragment.MenuItem课程存储在database中 - 在某些更改中会将新值添加到其中。返回到第一个fragment后 - 再次从database读取这些值并创建新的adapter(或替换现有的数据源并调用adapter.notifyDataSetChanged())。

希望这有帮助。