2

Possible Duplicate:
Android save Checkbox State in ListView with Cursor Adapter

I have a ListView, where each item contains some text(name,id) and a checkbox.There is a button to get back from ListView page. So a user check some items and go back to previous page. There is also a button is this page to get back to ListView page. When he/she gets back, I want to show him/her which items he/she had selected earlier.

I know about SharedPreferences, but I don't want to use it as I don't want to save that state when user restarts the application. I only want it during application runtime. I do some job on it, save data which he has selected and also check it when he gets back. But I can't able to check the checkboxes. I hope someone would say what wrong I am doing here. I use checkbox from xml,not programmatically.

public class MyCustomArrayAdapter extends ArrayAdapter<Model> {

    private final List<Model> list;

    private final  Activity context;

    public static ArrayList<String> friendName=new ArrayList<String>();
    public static ArrayList<String> friendBirthdate=new ArrayList<String>();
    public static ArrayList<String> friendId=new ArrayList<String>();
    public static ArrayList<String> getSelectedFriendId;
    public MyCustomArrayAdapter(Activity context, List<Model> list) 
    {
        super(context, R.layout.list_layout, list);

        this.context = context;
        this.list =  list ;
    }

    public static int count=0;
    static class ViewHolder 
    {
        protected TextView text, sub;
        protected CheckBox checkbox;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        View view = null;

        if (convertView ==null)
        {
            LayoutInflater inflator = context.getLayoutInflater();

            view = inflator.inflate(R.layout.list_layout, null);

            final ViewHolder viewHolder = new ViewHolder();

            viewHolder.text = (TextView)view.findViewById(R.id.label);

            viewHolder.text.setTextColor(Color.WHITE);

            viewHolder.sub = (TextView)view.findViewById(R.id.sub);

            viewHolder.sub.setTextColor(Color.GRAY);

            viewHolder.checkbox = (CheckBox)view.findViewById(R.id.check);

            if(SelectionPage.get_selectedFriendId()!=null)   //checking whether user checked something earlier
            {
                getSelectedFriendId=SelectionPage.get_selectedFriendId();   //get checked userid from when user selects something

                for( int x = 0 ; x < getSelectedFriendId.size() ; x++ )
                {
                    for(int a=0;a<list.size();a++)
                    {
                        if(list.get(a).getId().contains(getSelectedFriendId.get(x)))
                        {
                            Toast.makeText( context, "matched..", Toast.LENGTH_SHORT).show();
                            viewHolder.checkbox.setChecked(true); //this doesn't check checkbox which is the problem!
                        }
                    }
                }
            }
            viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
            {
                public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
                {
                    Model element = (Model)viewHolder.checkbox.getTag();

                    if(buttonView.isChecked())
                    {
                        count++;

                        friendName.add(element.getName().toString());
                        friendBirthdate.add(element.getPlace().toString());
                        friendId.add(element.getId().toString());
                    }
                    else
                    {
                        friendName.remove(element.getName().toString());
                        friendBirthdate.remove(element.getPlace().toString());
                        friendId.remove(element.getId().toString());
                        count--;
                    }
                }
            });

            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.text.setText(list.get(position).getName());
        holder.sub.setText( list.get(position).getPlace());
        return view;
    }
}
Community
  • 1
  • 1
ridoy
  • 6,274
  • 2
  • 29
  • 60

2 Answers2

1

Are you firing off code to refresh the listview after a view's content changes? You sometimes have to do that, otherwise a ListView does who knows what. There's a bunch of commands like invalidateViews() that can do this. Having the view in getView invalidate itself might also work

The code definitely could use optimization, but that's another topic

Joe Plante
  • 6,308
  • 2
  • 30
  • 23
1

Thanks to all for responding.I got answer myself after 2 hours playing with it.I bring some changes in checking which item was selected previous.Give my updated code below for those who faces same problem like me.

    if(SelectionPage.get_selectedFriendId()!=null)
    {
        String s=list.get(position).getId().toString();

        getSelectedFriendId=SelectionPage.get_selectedFriendId();

        for( int x = 0 ; x < getSelectedFriendId.size() ; x++ )
        {
            if(s.contains(getSelectedFriendId.get(x)))
            {
                Toast.makeText( context, "matched!..", Toast.LENGTH_SHORT).show();
                holder.checkbox.setChecked(true);

            }
        }
    }
ridoy
  • 6,274
  • 2
  • 29
  • 60
  • It is working fine for me, but when again I click on matched or selected checkbox it is not unselecting..it still remain selected. guide me @ridoy ...thanks – vijay chhalotre Jul 18 '17 at 19:55
  • Simply you can check toogle() method or even write code, when the checkbox isChecked(), then make it's setChecked(false). I mean something like these things. – ridoy Jul 20 '17 at 17:17