jeudi 17 septembre 2020

Checkbox not saving when reopening

I have 3 classes relating to my checkbox section of my app in Android studio, atm the check box loads, but when selecting and deselecting the value doesn't save when I go bak into it from the main menu. any help would great!!! public class WatchList extends AppCompatActivity {

ArrayList dataModels;
ListView listView;
private WatchListAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i("watchlist", "created watchlist activity");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_watch_list);


    listView = (ListView) findViewById(R.id.listview2);

    dataModels = new ArrayList();

    dataModels.add(new WatchListClass(R.drawable.kookaburra,"Kookaburra","Albury", false));
    dataModels.add(new WatchListClass(R.drawable.cockatoo, "Cockatoo" , "Bathurst", true));
    dataModels.add(new WatchListClass(R.drawable.emu,"Emu", "Echuca", true));
    dataModels.add(new WatchListClass(R.drawable.magpie, "Magpie", "Sydney", true));

    adapter = new WatchListAdapter(dataModels, getApplicationContext());

    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {

            WatchListClass dataModel= (WatchListClass) dataModels.get(position);
            dataModel.checked = !dataModel.checked;
            adapter.notifyDataSetChanged();


        }
    });
}

}

public class WatchListAdapter extends ArrayAdapter {

private ArrayList dataSet;
Context mContext;


private static class ViewHolder {
    TextView birdWatchName, birdWatchLocation;
    ImageView birdWatchImage;
    CheckBox checkBox;
}

public WatchListAdapter(ArrayList data, Context context) {
    super(context, R.layout.watch_list, data);
    this.dataSet = data;
    this.mContext = context;

}
@Override
public int getCount() {
    return dataSet.size();
}

@Override
public WatchListClass getItem(int position) {
    return (WatchListClass) dataSet.get(position);
}


@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {

    ViewHolder viewHolder;
    final View result;

    if (convertView == null) {
        viewHolder = new ViewHolder();
        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.watch_list, parent, false);
        viewHolder.birdWatchImage = (ImageView) convertView.findViewById(R.id.birdWatchImage);
        viewHolder.birdWatchName = (TextView) convertView.findViewById(R.id.birdWatchName);
        viewHolder.birdWatchLocation = (TextView) convertView.findViewById(R.id.birdWatchLocation);
        viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);

        result=convertView;
        convertView.setTag(viewHolder);

    } else {
        viewHolder = (ViewHolder) convertView.getTag();
        result=convertView;
    }

    WatchListClass item = getItem(position);

    viewHolder.birdWatchImage.setImageResource(item.birdWatchImage);
    viewHolder.birdWatchName.setText(item.birdWatchName);
    viewHolder.birdWatchLocation.setText(item.birdWatchLocation);
    viewHolder.checkBox.setChecked(item.checked);


    return result;
}

} public class WatchListClass {

public String birdWatchName, birdWatchLocation;
int birdWatchImage;
boolean checked;

WatchListClass(int birdWatchImage, String birdWatchName,String birdWatchLocation, boolean checked) {
    this.birdWatchName = birdWatchName;
    this.birdWatchLocation = birdWatchLocation;
    this.birdWatchImage = birdWatchImage;
    this.checked = checked;


}

}




Aucun commentaire:

Enregistrer un commentaire