Question

That problem is driving me crazy. I've created a button that way to check all the items of my ListView composed with CheckedTextView :

selectAll = (CheckBox) findViewById(R.id.selectAll);
selectAll.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    toggleAll(selectAll.isChecked());
        }
    });

And in the toggleAll function, I try reload the layout with all items selected :

int size = maListViewPerso.getCount();
for ( int i=0; i< taille; i++ ) {
    View v = maListViewPerso.getAdapter().getView(i, null, null);
    CheckedTextView tv = (CheckedTextView) v.findViewById(R.id.eventName);
    tv.setChecked(check);
}

But no item is set to checked in the view. Worst : all the checked item toggle to uncheck.

I've also tried another way by initializing the adapteur of the ListView with all items checked by default... No result. For information, my adapter for the ListView work that way :

public class ListViewColor extends ArrayAdapter<String>{
    public ListViewColor(Context context, int textViewResourceId, List<String> objects, String type) {

        [...]

 }
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
      String[] result = item.split("/");                
      CheckedTextView name = (CheckedTextView) view.findViewById(R.id.eventName);
      name.setText("txt");
      name.setSelected(true);
      return view;
 }

Any ideas ?

Was it helpful?

Solution

First of all, your first adempt is not, how you should treat a listview. Always change it's data via the ListAdapter.

In your second adempt, use name.setChecked(true) instead of name.setSelected(true).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top