我有ListView
。我通过检查在sqlite数据库中添加单行。(CheckBox
)同时我需要删除我取消选中的行。我需要存储在我的表中的行的ID,因此我可以删除该行。我该怎么做 ?
这是我在CheckBox
中的Adapter
更改代码。
public class MainVacancyAdapter extends ArrayAdapter {
private List<VacancyModel> vacancyModelList;
private int resource;
private LayoutInflater inflater;
ArrayList<Boolean> isSelected;
SQLHelper sqlHelper;
public MainVacancyAdapter(Context context, int resource, List<VacancyModel> objects) {
super(context, resource, objects);
vacancyModelList = objects;
this.resource = resource;
sqlHelper = new SQLHelper(getContext());
inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
isSelected = new ArrayList<>(vacancyModelList.size());
for (int i = 0; i < vacancyModelList.size(); i++) {
isSelected.add(false);
}
}
//Finding textViews and inserting data into them
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(resource, null);
holder.tvHeader = (TextView) convertView.findViewById(R.id.tvHeader);
holder.tvProfileName = (TextView) convertView.findViewById(R.id.tvProfileName);
holder.tvSalary = (TextView) convertView.findViewById(R.id.tvSalary);
holder.tvPostCr = (TextView) convertView.findViewById(R.id.tvPostCr);
//holder.tvBody = (TextView) convertView.findViewById(R.id.tvBody);
holder.cbxFav = (CheckBox) convertView.findViewById(R.id.cbxFav);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
holder.cbxFav.setOnCheckedChangeListener(null);
}
holder.tvHeader.setText(vacancyModelList.get(position).getHeader());
holder.tvProfileName.setText(vacancyModelList.get(position).getProfileName());
holder.tvSalary.setText(vacancyModelList.get(position).getSalary());
holder.tvPostCr.setText(vacancyModelList.get(position).getPostCreated());
//holder.tvBody.setText(vacancyModelList.get(position).getBody());
holder.cbxFav.setChecked(isSelected.get(position));
holder.cbxFav.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
//inserting the data into Favourite Table in sql
isSelected.set(position, true);
sqlHelper.createFavouriteTable(vacancyModelList.get(position));
//Toast.makeText(getContext(), "Added to favourite", Toast.LENGTH_SHORT).show();
} else {
isSelected.set(position, false);
sqlHelper.deleteFromFavouriteDatabase(position);
//Toast.makeText(getContext(), "Deleted from favourite", Toast.LENGTH_SHORT).show();
}
}
});
return convertView;
}
static class ViewHolder {
TextView tvHeader;
TextView tvProfileName;
TextView tvSalary;
TextView tvPostCr;
TextView tvBody;
CheckBox cbxFav;
}
}
<{1}} 中的方法
SQLHelper class
答案 0 :(得分:0)
尝试将删除语句更改为:
db.delete(FAVOURITE.TABLE_NAME, FAVOURITE.KEY_ID + "=?", new String[]{rowID});
答案 1 :(得分:0)
请通过以下方式更改您的getView()方法:
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(resource, null);
holder.tvHeader = (TextView) convertView.findViewById(R.id.tvHeader);
holder.tvProfileName = (TextView) convertView.findViewById(R.id.tvProfileName);
holder.tvSalary = (TextView) convertView.findViewById(R.id.tvSalary);
holder.tvPostCr = (TextView) convertView.findViewById(R.id.tvPostCr);
//holder.tvBody = (TextView) convertView.findViewById(R.id.tvBody);
holder.cbxFav = (CheckBox) convertView.findViewById(R.id.cbxFav);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
holder.cbxFav.setOnCheckedChangeListener(null);
}
holder.cbxFav.setTag(vacancyModelList.get(position));
holder.tvHeader.setText(vacancyModelList.get(position).getHeader());
holder.tvProfileName.setText(vacancyModelList.get(position).getProfileName());
holder.tvSalary.setText(vacancyModelList.get(position).getSalary());
holder.tvPostCr.setText(vacancyModelList.get(position).getPostCreated());
//holder.tvBody.setText(vacancyModelList.get(position).getBody());
holder.cbxFav.setChecked(isSelected.get(position));
holder.cbxFav.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
//inserting the data into Favourite Table in sql
isSelected.set(position, true);
VacancyModel selectedModel = (VacancyModel) holder.cbxFav.getTag();
sqlHelper.createFavouriteTable(selectedModel);
//Toast.makeText(getContext(), "Added to favourite", Toast.LENGTH_SHORT).show();
} else {
isSelected.set(position, false);
//sqlHelper.deleteFromFavouriteDatabase(position);
//Toast.makeText(getContext(), "Deleted from favourite", Toast.LENGTH_SHORT).show();
VacancyModel selectedModel = (VacancyModel) holder.cbxFav.getTag();
sqlHelper.deleteFromFavouriteDatabase(selectedModel.getId());
}
}
});
return convertView;
}
答案 2 :(得分:0)
将Id
的列放在VacancyModel
中,然后将其绑定到删除
int id = vacancyModelList.get(position).getId()
sqlHelper.deleteFromFavouriteDatabase(id);