我在检查复选框时遇到麻烦,我有一个来自数据库的列表视图,该数据库包含两个复选框,并具有一个按钮,可将数据发送到另一个表,单击“刷新”按钮后,一切正常活动中,取消选中该复选框(显然),但是如果您再次单击该按钮甚至没有选中该复选框,则它会再次发送以前的数据,就像该复选框仍被标记一样。
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;
}
}