我有一個列表視圖與複選框從外部數據庫加載的數據。當我檢查任何特定的複選框並按刪除,然後它刪除該特定值。但我的問題是,當我檢查多個複選框並按刪除時,它只刪除最後選中的值而不是所有檢查的值。如何刪除多個條目從列表視圖通過檢查複選框在Android
所以我在做我的代碼錯了。任何人都可以建議請。以下是我的代碼。先謝謝你。
CustomClassAdapter
public class Custom_ClassAdapter extends BaseAdapter {
LayoutInflater inflater;
Context context;
List<List_ClassModel> rowItems;
public Custom_ClassAdapter(Context context, List<List_ClassModel> rowItems) {
inflater = LayoutInflater.from(context);
this.context = context;
this.rowItems = rowItems;
}
private class ViewHolder {
ImageView mImageView;
TextView mTextView;
CheckBox mCheckBox;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_class, null);
holder = new ViewHolder();
holder.mImageView = (ImageView) convertView
.findViewById(R.id.classImage);
holder.mTextView = (TextView) convertView
.findViewById(R.id.classname);
holder.mCheckBox = (CheckBox) convertView
.findViewById(R.id.checkBox1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mImageView.setImageResource(position);
holder.mCheckBox.setTag(position);
holder.mCheckBox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
Code.classDel = (Integer) buttonView.getTag();
}
});
List_ClassModel rowItem = (List_ClassModel) getItem(position);
holder.mTextView.setText(rowItem.getClassName());
return convertView;
}
@Override
public int getCount() {
return rowItems.size();
}
@Override
public Object getItem(int position) {
return rowItems.get(position);
}
@Override
public long getItemId(int position) {
return rowItems.indexOf(getItem(position));
}
public void clear() {
}
}
DeleteProduct
class DeleteProduct extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Classes_Ext_DB.this);
pDialog.setMessage("Deleting Class... Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Deleting product
* */
protected String doInBackground(String... args) {
// Check for success tag
int success;
try {
List_ClassModel mlList_ClassModel = Code.arrayClasses
.get(Code.classDel);
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("ID", ""
+ mlList_ClassModel.getClass_ID()));
// getting product details by making HTTP request
JSONObject json = jParser.makeHttpRequest(url_delete_class,
"POST", params);
// check your log for json response
Log.d("Delete Product", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
pDialog.dismiss();
Code.arrayClasses.remove(Code.classDel);
mCustom_ClassAdapter.notifyDataSetChanged();
}
}
編輯
public static int pm = 0;
public static int dropboxlogout = 0;
public static int currentDel;
public static int classDel;
如果我使用這種上面的代碼然後我不得不這裏通過List_ClassModel mlList_ClassModel = Code.arrayClasses.get(Code.classDel);因爲如果我傳遞Code.arrayList,那麼它顯示錯誤。 – InnocentKiller
我不知道你在Code類中有什麼或做什麼。你必須相應地更新該課程。 – ayorhan
我剛剛聲明classDel爲Code類中的int變量。 public static int classDel; – InnocentKiller