samedi 20 juin 2015

Can't get checked checkbox state

i have made an array, which contains my checked or not statements, array consists of 1 or 0 symbols, which represents true or false. The problem is, i cant make checkbox to be checked or unchecked, every time compiler shows NullPointerException error. Any help?

public class ListViewClass extends MainActivity implements CompoundButton.OnCheckedChangeListener, itemsArray {

public static ArrayList<String> items = new ArrayList<>();
private ListView listview;
ArrayList<checkbox> planetList;
checkboxAdapter p1Adapter;
TextView ListName;
CheckBox checkBox;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listview);
    checkBox= (CheckBox) findViewById(R.id.chk_box);
    ListName = (TextView) findViewById(R.id.ListName);
    ListName.setText(FragmentA.readfailopav);

    readCheckus();

    readItems();
    checker();



    listview = (ListView) findViewById(R.id.listView);
    planetList = new ArrayList<checkbox>();
    displayPlanetList();
    //registerForContextMenu(listview);
    listview.setClickable(true);
    listview.setFocusable(true);
    listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
            PopupMenu popupMenu = new PopupMenu(getApplicationContext(), view);
            popupMenu.inflate(R.menu.menu_chooser);
            popupMenu.show();
            popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    int id = item.getItemId();


                    if (id == R.id.reminder) {
                        NotificationCompat.Builder notification = new NotificationCompat.Builder(ListViewClass.this);

                        notification.setSmallIcon(R.mipmap.ic_launcher);
                        notification.setTicker("new");
                        notification.setWhen(System.currentTimeMillis());
                        notification.setContentTitle("How to android");
                        notification.setContentText("This is lox");


                        Uri sound = RingtoneManager.getDefaultUri(Notification.DEFAULT_SOUND);
                        notification.setSound(sound);


                        //  alarmMethod();
                        return true;
                    } else if (id == R.id.delete) {
                        Toast.makeText(ListViewClass.this, itemsArray.items.toString() + " + " + items.get(position), Toast.LENGTH_SHORT).show();
                        items.remove(position);
                        writeItems2();
                        readItems();
                        finish();
                        Intent refresh = new Intent(ListViewClass.this, ListViewClass.class);
                        startActivity(refresh);


                    }

                    return true;
                }
            });
            return true;
        }
    });



}



private void displayPlanetList() {
    for (int i = 0; i < items.size(); i++) {
        planetList.add(new checkbox(items.get(i)));
    }
    p1Adapter = new checkboxAdapter(planetList, this);
    listview.setAdapter(p1Adapter);



}

public void checker(){
    int pos = listview.getPositionForView(checkBox);
    checkbox p = planetList.get(pos);
    for(int i=0;i<items.size();i++){
        if(checkboxcounter.equals("1")){
        p.setSelected(true);

        }
    }


}


@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    int pos = listview.getPositionForView(buttonView);
    if (pos != ListView.INVALID_POSITION) {
        checkbox p = planetList.get(pos);

        p.setSelected(isChecked);
        if(isChecked == true) {

            Toast.makeText(this, "Checked " + p.getName() + ". State is Completed!", Toast.LENGTH_SHORT).show();
            checkboxcounter.set(pos, "1");
            saugoCheckus();

        }

        else {
            Toast.makeText(this, "Unchecked " + p.getName() + ". State is in progress...", Toast.LENGTH_SHORT).show();
            checkboxcounter.set(pos, "0");
            saugoCheckus();
        }
    }
}

public void onBack(View view){
    finish();
}

private void readItems() {
    File filesDir = getFilesDir();
    File todoFile = new File(filesDir, ListName.getText().toString());
    try {
        items = new ArrayList<String>(FileUtils.readLines(todoFile));
    } catch (IOException e) {
        items = new ArrayList<String>();
    }
}
public void writeItems2() {
    File filesDir = getFilesDir();
    File todoFile = new File(filesDir,ListName.getText().toString());
    try {
        FileUtils.writeLines(todoFile, items);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
public void saugoCheckus() {
    File filesDir = getFilesDir();
    File todoFile = new File(filesDir, FragmentA.readfailopav +".chk");
    try {
        FileUtils.writeLines(todoFile,checkboxcounter );
    } catch (IOException e) {
        e.printStackTrace();
    }
}
private void readCheckus() {
    File filesDir = getFilesDir();
    File todoFile = new File(filesDir, FragmentA.readfailopav + ".chk");
    try {
        checkboxcounter = new ArrayList<String>(FileUtils.readLines(todoFile));
    } catch (IOException e) {
        checkboxcounter = new ArrayList<String>();
    }
}

The code Crashes at int pos = listview.getPositionForView(checkBox);




Aucun commentaire:

Enregistrer un commentaire