如何chekbox在自定义适配器中动态检查

时间:2017-03-19 07:37:45

标签: android

当我点击selectall按钮

时,我试图将所有复选框设置为true

private void selectall(){         selectall.setOnClickListener(new View.OnClickListener(){

import java.io.*;
import java.util.*;
public class projectFour
{
    public static int [] global_numbers;
    public static void main (String[] args)
    {
        read_file();
        print_numbers(global_numbers);
    }
    public static void read_file()
    {
        try
        {
            File file = new File("randomNumbers.txt"); 
            Scanner scan = new Scanner(file);
            int amountOfNumbersInFile = convertingStringToInt(scan.nextLine());
            global_numbers = new int[amountOfNumbersInFile];
            for (int index = 0; index < amountOfNumbersInFile; index++) 
            {
                String line = scan.nextLine(); 
                global_numbers [index] = convertingStringToInt(line); 
            }      
        } catch (IOException ex) {
           ex.printStackTrace();
        }
    }
    public static int convertingStringToInt(String numbers)
    {  
        try {
            return Integer.parseInt(numbers.trim());
        } catch(NumberFormatException n) {
            return -460;
        }         
    }
    public static void print_numbers(int [] numbers) 
    {
      for(int i = 0; i < numbers.length; i++)
            System.out.println(numbers[i]); 
    }
}

1 个答案:

答案 0 :(得分:0)

第1步

初始化自定义适配器说myAdapter

myAdapter = new MyAdapter(NotificatioTest.this,mArrayListSource);

将适配器设置为ListView mListView

mListView.setAdapter(myAdapter);

第2步

对于选择所有CheckBox视图,将setOnCheckedChangeListener侦听器添加为:

chkBoxSelectAll.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        mArrayListSource.clear();
        for(int i=0; i < YourSongsArrayList.size(); i++){
            Module mModule = new Module();
            mModule.isChecked = isChecked;
            mModule.mText = "" + isChecked;
            mArrayListSource.add(mModule);
        }
        myAdapter.notifyDataSetChanged();
    }
});

模块:

class Module {
    public boolean isChecked = false;
    public String mSongName = "";
}