I'm having trouble unchecking checkebox, I have a listview that comes from a database that contains two Check, and has a button that sends that data to another table, everything up here ok, by clicking the "refresh" the activity, uncheck the checkbox (apparently) but if you click the button again without having to check it .. it sends the previous data again, as if the checkboxes were still marked.
public class ChamadaActivity extends AppCompatActivity {
Context context;
ArrayList<Category> array_list;
FavouriteCategoriesJsonParser categoryJsonParser;
FavouriteCategoriesJsonParser categoryJsonParser2;
String categoriesCsv, categoriesCsv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chamada_m_salomao);
context = this;
new asyncTask_getCategories().execute();
}
@SuppressLint("StaticFieldLeak")
public class asyncTask_getCategories extends AsyncTask<Void, Void, Void> {
ProgressDialog dialog = new ProgressDialog(context);
@Override
protected void onPreExecute() {
dialog.setTitle("Please wait...");
dialog.setMessage("Loading Categories!");
dialog.show();
array_list = new ArrayList<>();
categoryJsonParser = new FavouriteCategoriesJsonParser();
categoryJsonParser2 = new FavouriteCategoriesJsonParser();
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
array_list = categoryJsonParser.getParsedCategories();
array_list = categoryJsonParser2.getParsedCategories2();
return null;
}
@Override
protected void onPostExecute(Void s) {
ListView mListViewBooks = findViewById(R.id.category_listView);
final CategoryAdapter categoryAdapter =
new CategoryAdapter(context, R.layout.row_c_m_salomao, array_list);
mListViewBooks.setAdapter(categoryAdapter);
Button button = findViewById(R.id.selectCategoryButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
categoriesCsv = FavouriteCategoriesJsonParser.selectedCategories.toString();
categoriesCsv = categoriesCsv.substring(1, categoriesCsv.length() - 1);
categoriesCsv2 = FavouriteCategoriesJsonParser.selectedCategories2.toString();
categoriesCsv2 = categoriesCsv2.substring(1, categoriesCsv2.length() - 1);
if (categoriesCsv.length() > 0) {
new asyncTask_insertUpdatefavouriteCategories().execute();
finish();
startActivity(getIntent());
} else if (categoriesCsv2.length() > 0) {
new asyncTask_insertUpdatefavouriteCategories2().execute();
finish();
startActivity(getIntent());
} else {
Toast.makeText(context, "Por Favor Selecione ao menos um aluno", Toast.LENGTH_SHORT)
.show();
}
}
});
super.onPostExecute(s);
dialog.dismiss();
}
@SuppressLint("StaticFieldLeak")
public class asyncTask_insertUpdatefavouriteCategories extends AsyncTask<Void, Void, Void> {
String response;
@Override
protected Void doInBackground(Void... params) {
response = InsertUpdateFavouriteCategories.insertUpdateCall(categoriesCsv);
return null;
}
@Override
protected void onPostExecute(Void s) {
Toast.makeText(context, response, Toast.LENGTH_SHORT).show();
super.onPostExecute(s);
}
}
@SuppressLint("StaticFieldLeak")
public class asyncTask_insertUpdatefavouriteCategories2 extends AsyncTask<Void, Void, Void> {
String response2;
@Override
protected Void doInBackground(Void... params) {
response2 = InsertUpdateFavouriteCategories2.insertUpdateCall2(categoriesCsv2);
return null;
}
@Override
protected void onPostExecute(Void s) {
Toast.makeText(context, response2, Toast.LENGTH_SHORT).show();
super.onPostExecute(s);
}
}
}
}
public class CategoryAdapter extends ArrayAdapter<Category> {
private final List<Category> list;
public CategoryAdapter(Context context, int resource, List<Category> list) {
super(context, resource, list);
this.list = list;
}
static class ViewHolder {
TextView categoryName;
CheckBox categoryCheckBox, categoryCheckBox2;
}
@SuppressLint("InflateParams")
@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflator = LayoutInflater.from(getContext());
convertView = inflator.inflate(R.layout.row_c_m_salomao, null);
viewHolder = new ViewHolder();
viewHolder.categoryName = convertView.findViewById(R.id.row_categoryname_textview);
viewHolder.categoryCheckBox = convertView.findViewById(R.id.row_category_checkbox);
viewHolder.categoryCheckBox2 = convertView.findViewById(R.id.row_category_checkbox2);
viewHolder.categoryCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
list.get(getPosition).setSelected(buttonView.isChecked());
if (buttonView.isChecked()) {
if (!FavouriteCategoriesJsonParser.selectedCategories.contains(String.valueOf(list.get(getPosition).getCategory_id()))) {
FavouriteCategoriesJsonParser.selectedCategories.add(String.valueOf(list.get(getPosition).getCategory_id()));
}
} else {
if (FavouriteCategoriesJsonParser.selectedCategories.contains(String.valueOf(list.get(getPosition).getCategory_id()))) {
FavouriteCategoriesJsonParser.selectedCategories.remove(String.valueOf(list.get(getPosition).getCategory_id()));
}
}
}
});
viewHolder.categoryCheckBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
list.get(getPosition).setSelected(buttonView.isChecked());
if (buttonView.isChecked()) {
if (!FavouriteCategoriesJsonParser.selectedCategories2.contains(String.valueOf(list.get(getPosition).getCategory_id()))) {
FavouriteCategoriesJsonParser.selectedCategories2.add(String.valueOf(list.get(getPosition).getCategory_id()));
}
} else {
if (FavouriteCategoriesJsonParser.selectedCategories2.contains(String.valueOf(list.get(getPosition).getCategory_id()))) {
FavouriteCategoriesJsonParser.selectedCategories2.remove(String.valueOf(list.get(getPosition).getCategory_id()));
}
}
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.row_categoryname_textview, viewHolder.categoryName);
convertView.setTag(R.id.row_category_checkbox, viewHolder.categoryCheckBox);
convertView.setTag(R.id.row_category_checkbox2, viewHolder.categoryCheckBox2);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.categoryCheckBox.setTag(position);
viewHolder.categoryCheckBox2.setTag(position);
viewHolder.categoryName.setText(list.get(position).getCategory_Name());
viewHolder.categoryCheckBox.setChecked(list.get(position).isSelected());
viewHolder.categoryCheckBox2.setChecked(list.get(position).isSelected());
return convertView;
}