1

Currently, I've been involved in an android app development related to online foodItem ordering. I've a listView with following schema:

itemName1  itemPrice1  checkBox
itemName2  itemPrice2  checkBox
itemName3  itemPrice3  checkBox
             .
             .
             .


          GetYourCart[Button]

My task is to keep track of each of the selected itemNames with price and total price when the user presses the button. I've used the SparseBooleanArray sbArray = myMenulist.getCheckedItemPositions(); but it's not showing any result.

How can I implement this? I've gone through several questions asked in stackoverflow and other online tutorials but can't get it.

Edited: Here is the code for selected items that I used to check the result:

btnyourCart.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                String selected = "";
                int cntChoice = myMenulist.getCount();
                SparseBooleanArray sbArray = myMenulist.getCheckedItemPositions();
                for(int i=0;i<cntChoice;i++){
                    if(sbArray.get(i)) {

                        selected += myMenulist.getItemAtPosition(i).toString() + "\n";



                    }


                }
                Log.e("menu","Items "+selected);
                Toast.makeText(getApplicationContext(), selected, Toast.LENGTH_SHORT).show();


            }
        });

Thanks in advance!

3
  • Paste your checkbox check code so that at that place i will update it. Commented Sep 25, 2012 at 5:25
  • @hardikjoshi I've edited the question with code Commented Sep 25, 2012 at 5:37
  • Refer this dj-android.blogspot.com/2012/04/… Commented Sep 25, 2012 at 5:44

2 Answers 2

1
int total=0;

btnyourCart.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            String selected = "";
            int cntChoice = myMenulist.getCount();
            SparseBooleanArray sbArray = myMenulist.getCheckedItemPositions();
            for(int i=0;i<cntChoice;i++){
                if(sbArray.get(i)) {

                    selected += myMenulist.getItemAtPosition(i).toString() + "\n";

                     total+=myMenulist.get(i).price;

                }


            }
            Log.e("menu","Items "+selected);
            Toast.makeText(getApplicationContext(), selected, Toast.LENGTH_SHORT).show();


        }
    });

Now on button click event get total.

button.setOnClickListenernew View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Toast.makeText(
                    this,
                    ""+total,
                    Toast.LENGTH_LONG).show();

        }
    });
Sign up to request clarification or add additional context in comments.

Comments

0

you need to do in your custom adapter in getView() method to keep checkbox value in list position and store that value in the array. This will keep checkbox value with its position.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;

        if(convertView == null){
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.list_layout, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.checkBox = (CheckBox) view.findViewById(R.id.del_many_task_chk);
            viewHolder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    Model element = (Model) viewHolder.checkBox.getTag();
                    element.isChecked = buttonView.isChecked();
                }
            });
            view.setTag(viewHolder);
            viewHolder.checkBox.setTag(list.get(position));
        }else{
            view = convertView;
            ((ViewHolder)view.getTag()).checkBox.setTag(list.get(position));
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.checkBox.setChecked(list.get(position).isChecked);
        return view;
    }

Now when need to know which check box are checked or not then you need to go through your list item using iterator. To get the checked and unchecked item based on checkbox value.

for(int i=0;i<list.size();i++){
    if(all_task_list.get(i).isChecked){
         // to do things for checked item
    }else{
         // to do things for uncheck item
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.