2

I have used ListView with custom adapter, so my ListView includes CheckBox, but problem is that when I scroll down, checked CheckBoxes uncheck, how can I do it with this code?

UPDATE

My custom adapter:

    public class AppsArrayAdapter extends ArrayAdapter<String> {
    private final Context context;

    private List<Integer> checkedItems;

    private final String DefaultApps = "def_apps";

    public AppsArrayAdapter(Context context, String[] values, List<Integer> checkedItems) {//XXX
        super(context, R.layout.apps, values);
        this.context = context;
        this.checkedItems = checkedItems;//XXX
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SharedPreferences myPrefsApps =context.getSharedPreferences("myPrefsApps", Context.MODE_PRIVATE);
        String prefNameDefaultApps = myPrefsApps.getString(DefaultApps, "");
        String prefNameDefaultAppsVer = myPrefsApps.getString(DefaultAppsVer, "");


        LayoutInflater inflater = (LayoutInflater)context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.apps, parent, false);
        TextView text_apps = (TextView)rowView.findViewById(R.id.text_apps);
        CheckBox check = (CheckBox)rowView.findViewById(R.id.check_apps);
        text_apps.setText(prefNameDefaultApps.split("\n")[position]);

        check.setChecked(checkedItems.contains((Integer) position));
        return rowView;
    }   
}

And my Activity:

    public class AppsActivity extends ListActivity {
    private final String DefaultApps = "def_apps";

    private List<Integer> checkedItems = new ArrayList<Integer>();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AppsArrayAdapter adapter = new AppsArrayAdapter(this, prefNameDefaultApps.split("\n"), checkedItems);
        setListAdapter(adapter);
}
}
@Override
    public void onListItemClick(ListView l, View v, int position, long id) {
       if(checkedItems.contains((Integer) position)) checkedItems.remove((Integer) position);
       else checkedItems.add((Integer) position);
    }
Adam
  • 2,028
  • 4
  • 33
  • 43
  • Look at my answer http://stackoverflow.com/questions/9309250/checkbox-gets-unchecked-on-scroll-in-a-custom-listview/14724554#14724554 – Nauman Zubair Feb 06 '13 at 08:37

4 Answers4

0

you should use MyAdapter newad=(MyAdapter)adapter.getAdapter(); newad.notifyDataSetChanged(); this code in onitem click method and MyAdapter is class of Adapter and make new object like this one and in adapter class you should have to write a line

LayoutInflater in; in=(LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); in adapter constructor simple

0
// initialise the list
private List<Integer> selection = new ArrayList<Integer>();

for (String str: prefNameDefaultApps.split("\n")) {
        selection.add(0);
}


// setting the list 
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
        if(CheckBox is checked) {//(check if the checkbox is checked )
               selection.set(position, 1);
        }
        else{
               selection.set(position, 0);
        }
}

// show in the listView
@Override
public View getView(int position, View convertView, ViewGroup parent) {

        ....
        ....
        check.setChecked(selection.get(position) == 0 ? false : true);

}

This should get you going :)

1HaKr
  • 1,050
  • 1
  • 11
  • 18
  • Still doesn't work. I don't know where to place this code for (String str: prefNameDefaultApps.split("\n")) { selection.add(0); } I tried to place it to public AppsArrayAdapter(Context context, String[] values) and then to public View getView(int position, View convertView, ViewGroup parent), but still doesn't work. – Adam Feb 21 '12 at 19:22
0

I'm not entirely sure whether there is a standard solution in Android, but this should do the trick:

public class AppsArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private List<Integer> checkedItems;//Here we save the position of the selected items.

private final String DefaultApps = "def_apps";
//This will be invoked when one of the CheckBoxes gets clicked. It will update the checkedItems list.
private View.OnClickListener onClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Integer position = (Integer) v.getTag();
        if(checkedItems.contains(position)) checkedItems.remove(position);
        else checkedItems.add(position);
    }
};

public AppsArrayAdapter(Context context, String[] values, List<Integer> checkedItems) {//XXX
    super(context, R.layout.apps, values);
    this.context = context;
    this.checkedItems = checkedItems;//XXX
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    SharedPreferences myPrefsApps =context.getSharedPreferences("myPrefsApps", Context.MODE_PRIVATE);
    String prefNameDefaultApps = myPrefsApps.getString(DefaultApps, "");

    //You shouldn't inflate the View everytime getView() is called,
    /*LayoutInflater inflater = (LayoutInflater)context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.apps, parent, false);*/
    //rather let the superclass handle it as it's more efficient.
    View rowView = super.getView(position, convertView, parent);

    TextView text_apps = (TextView)rowView.findViewById(R.id.text_apps);
    CheckBox check = (CheckBox)rowView.findViewById(R.id.check_apps);
    text_apps.setText(prefNameDefaultApps.split("\n")[position]);

    check.setChecked(checkedItems.contains((Integer) position));//XXX
    check.setTag((Integer) position);//XXX
    check.setOnClickListener(onClickListener);//XXX
    return rowView;
}   

}

In your ListActivity, add a field private List<Integer> checkedItems = new ArrayList<Integer>;. You have to pass this List in the Adapter constructor!
If you want to check whether the nth item in your ListView was selected, just look whether n is saved in checkedItems, i.e.

if(checkedItems.contains(requestedItemPosition)) {
    //Item was selected.
} else {
    //Item was not selected.
}

I really recommend you not to inflate the views by yourself.

m1ntf4n
  • 493
  • 2
  • 14
  • Tell me if this requires further explanation. – m1ntf4n Feb 21 '12 at 19:15
  • Could you give me some example or detailed explanation how should look my Activity. MyActivity extends ListActivity. I tried to manage it, but I got a Force Close. – Adam Feb 21 '12 at 19:30
  • Sorry, the onListItemClick signature was incomplete. – m1ntf4n Feb 21 '12 at 20:09
  • Do you get an exception? Or is the checkbox state not getting saved? – m1ntf4n Feb 22 '12 at 17:26
  • Look at my code, I updated it how my code looks now. Checkbox state is not saved. My onCreate also contains some codes for SharedPreferences. – Adam Feb 22 '12 at 17:29
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/8086/discussion-between-m1ntf4n-and-adam) – m1ntf4n Feb 22 '12 at 18:35
  • Did this work? If not please tell me, and I will try to improve my code. – m1ntf4n Feb 26 '12 at 17:31
0

Try this: (your custom adapter)

 public class AppsArrayAdapter extends ArrayAdapter<String> {
private final Context context;

private List<Integer> checkedItems;

private final String DefaultApps = "def_apps";
LayoutInflater inflater;

public AppsArrayAdapter(Context context, String[] values, List<Integer> checkedItems) {//XXX
    super(context, R.layout.apps, values);
    this.context = context;
    this.checkedItems = checkedItems;//XXX
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    SharedPreferences myPrefsApps =context.getSharedPreferences("myPrefsApps", Context.MODE_PRIVATE);
    String prefNameDefaultApps = myPrefsApps.getString(DefaultApps, "");
    String prefNameDefaultAppsVer = myPrefsApps.getString(DefaultAppsVer, "");


View rowView = convertView;
ViewHolder viewH = new ViewHolder();

if ( rowView == null){
        rowView = inflater.inflate(R.layout.apps, parent, false);
        viewH.text_apps = (TextView)rowView.findViewById(R.id.text_apps);
        viewH.check = (CheckBox)rowView.findViewById(R.id.check_apps);
    rowView.setTag(viewH);
}else{
    viewH = (ViewHolder) rowView.getTag();
}

    viewH.text_apps.setText(prefNameDefaultApps.split("\n")[position]);
    viewH.check.setChecked(checkedItems.contains((Integer) position));

    return rowView;
}   

static class ViewHolder {
    public TextView text_apps;
    public CheckBox check;  

}

}

Hope this helps :)

Ratan
  • 1,677
  • 2
  • 18
  • 26