3

In my Apps, I am selecting the Items to Order. My Problem here is If I check the Checkbox to Order even another Checkbox is also has been checked. Means If I select single Item , two Items are getting checked.

And starting for 5 Items it is checked correctly and displaying in the Ordering page. From 6th Item If I select It is displaying some other Items in the Ordering page.

Here is my Code of the List:

    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final ViewHolder viewHolder;
        if (view == null) {         
              view = inflater.inflate(R.layout.appetiserlistview, null);
            viewHolder = new ViewHolder();
            viewHolder.image=(ImageView)view.findViewById(R.id.appetiserimage);
            viewHolder.text = (TextView) view.findViewById(R.id.appetisertext);
            viewHolder.desc=(TextView)view.findViewById(R.id.appetiserdesc);
            viewHolder.price=(TextView)view.findViewById(R.id.appetiserprice);
            viewHolder.checkbox = (CheckBox) view.findViewById(R.id.bcheck);


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






        viewHolder.image.setImageBitmap(bmps[position]);

        viewHolder.price.setText(prices[position]);
        viewHolder.desc.setText(descs[position]);
        viewHolder.checkbox.setTag(itemnames[position]);
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(itemnames[position]);
        viewHolder.checkbox.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (((CheckBox) v).isChecked())
                {
                    arr.add(itemnames[position]);
                    itemprice.add(prices[position]);
                       image.add(bmps[position]);


                }
Stuthi
  • 53
  • 9
  • 1
    here is a nice example http://www.vogella.com/articles/AndroidListView/article.html#listadvanced_interactive – Akram May 25 '12 at 06:26
  • http://stackoverflow.com/questions/7738527/getting-an-issue-while-checking-the-dynamically-generated-checkbox-through-list/7738854#7738854 – Lalit Poptani May 26 '12 at 12:47

3 Answers3

0

AS per behavior of listview, it does not save state of its item as they Inflayet on scroll time, so you should use a array of boolean values to persist status of check boxes,

public class FriendListAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private boolean isCheck[] = new boolean[Data.fbFriendList.size()];
 // Data.fbFriendList is arraylist contains data
    public FriendListAdapter() {

        mInflater = LayoutInflater.from(mContext);
        Arrays.fill(isCheck, Boolean.FALSE);
    }

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

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {
        View hView = convertView;
        if (convertView == null) {
            hView = mInflater.inflate(R.layout.friend_item, null);
            ViewHolder holder = new ViewHolder();
            holder.profile_pic = (ImageView) hView
                    .findViewById(R.id.profile_pic);
            holder.name = (TextView) hView.findViewById(R.id.name);
            holder.checkBox = (CheckBox) hView.findViewById(R.id.checkBox);
            hView.setTag(holder);
        }

        final ViewHolder holder = (ViewHolder) hView.getTag();

        if (isCheck[position]) {
            holder.checkBox.setChecked(true);
        } else {
            holder.checkBox.setChecked(false);
        }

        try {
            holder.profile_pic.setImageBitmap(Utility.model.getImage(
                    Data.fbFriendList.get(position).getFbFriendFBid(),
                    Data.fbFriendList.get(position)
                            .getFbFriendThumbImgUrl()));
            holder.name.setText(Data.fbFriendList.get(position)
                    .getFbFriendName());

        } catch (Exception e) {
            e.printStackTrace();
            holder.name.setText("");
        }

        holder.checkBox.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.checkBox.isChecked())
                    isCheck[position] = true;
                else
                    isCheck[position] = false;
            }
        });

        return hView;
    }

}

class ViewHolder {
    ImageView profile_pic;
    TextView name;
    CheckBox checkBox;
}

Here isCheck[] is a boolean array used in Adapter class to persist status of checkboxes and Data.fbFriendList is a arraylist.

Anand Tiwari
  • 1,583
  • 10
  • 22
  • @Anand Tiwari Thank you for your reply. I will try now. – Stuthi May 25 '12 at 06:35
  • @Anand Tiwari Can you tell me how to hold the Status of Checkbox in CustomAdapter? – Stuthi May 25 '12 at 07:26
  • just create a array of boolean values itemChecked[] in BaseAdapter class (in constructor) and initialize by false values for initially unchecked, and then use in getView(). – Anand Tiwari May 25 '12 at 07:34
  • I changed in Adapter class but in getView I am getting like this: The type of the expression must be an array type but it resolved to boolean – Stuthi May 25 '12 at 07:59
  • Boolean[] itemChecked = new Boolean[size]; Arrays.fill(itemChecked, Boolean.FALSE); add this to ur adapter. – Anand Tiwari May 25 '12 at 10:47
  • @Anand Tiwari As you told I did but still I am not able to solve it. I am getting force close before I enter to the Selecting the List of Items Activity.. :( As I am new to Android not able to solve that issue. – Stuthi May 29 '12 at 05:39
  • I have updated my answer, I have posted complete adapter class just change array list by your list, its working at my end, further if you get any issue, let me know. – Anand Tiwari May 29 '12 at 06:35
  • @Anand Tiwari ya okai I will try it now. – Stuthi May 29 '12 at 08:27
  • @Anand Tiwari what is ??getFbFriendFBid() , getFbFriendThumbImgUrl() and getFbFriendName() .. – Stuthi May 29 '12 at 09:20
  • these are bean function, used to retrieve values, you should replace them by your data(whatever you using) or customize getView() as per your need... – Anand Tiwari May 29 '12 at 09:33
  • @Anand Tiwari Can you see my ListView class posted to my Question and tell me what I have to replace in the place of those functions?? – Stuthi May 29 '12 at 09:59
0

Its a basic property of ListView not only checkboxes but also Edittext and other input controls will have the same value as the one have what actually happen in list view

the items that are being displayed are only created.

When you scroll them new items are created so as per previous display state the one of check box get checked for current display. Solution 1: Create a list in which store the states of all checkboxes(or list items) and at calling of bindview() method check the status of check box and according to the list check and uncheck them also create checkbox listener so that oncheckchange event change the state of checkbox(or listitem) in list so this way you can get the solution

Solution 2:

if you need only one check box then you can use listview property that enables multi item selection

Edit 1:

set this property

android:choiceMode="multipleChoice"

for you its 2

Follow the Link and also See this Property

Trikaldarshiii
  • 11,174
  • 16
  • 67
  • 95
0

you have to save states of all of your checkboxes and restore them accordingly. Here is an example i have already posted. If any problem you are facing then let me know. I will try to help you.

Check this link

How to implement a button that gets all checkbox's state and adds the value of checked item into arraylist?

Community
  • 1
  • 1
Bharat Sharma
  • 3,926
  • 2
  • 17
  • 29