2014-01-11 21 views
0

我有一個列表視圖與複選框從外部數據庫加載的數據。當我檢查任何特定的複選框並按刪除,然後它刪除該特定值。但我的問題是,當我檢查多個複選框並按刪除時,它只刪除最後選中的值而不是所有檢查的值。如何刪除多個條目從列表視圖通過檢查複選框在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; 

回答

0

綜觀海峽你的代碼的編碼,我認爲它可以用來刪除一個單一的項目。

這就是發生了什麼事情:當您檢查項目x時,此項目被存儲以供刪除。然後,當您檢查項目y時,先前的id會被新的id覆蓋。所以,當你向服務器發出請求時,你只有最後一個選中的項目。

你可以做些什麼:創建一個ArrayList。每當選中任何項目時,將該項目添加到ArrayList。因此,您可以根據需要存儲儘可能多的項目以進行刪除。最後,在向服務器發送請求時,遍歷ArrayList以獲取所有項目的ID並將其以所需格式發送到服務器。

0

在下面的監聽器中,您將要刪除的項目存儲在Code類中定義的公共靜態變量中。任何時候你選中或取消選中(是的,取消選中,因爲你不檢查isChecked變量)classDel變量被覆蓋。因此,您只能刪除您所交互的最後一個項目。

holder.mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
    @Override 
    public void onCheckedChanged(CompoundButton buttonView, 
         boolean isChecked) { 
     // TODO Auto-generated method stub 
     Code.classDel = (Integer) buttonView.getTag(); 
    } 
}); 

你想要做的是創建一個數組列表並相應地修改它。以下是一個示例代碼:

holder.mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
    @Override 
    public void onCheckedChanged(CompoundButton buttonView, 
         boolean isChecked) { 
     if(isChecked){ // checked an item, add it to the list 
      Code.arrayList.add(buttonView.getTag()); 
     } else { // unchecked an item, remove it from the list 
      Code.arrayList.remove(buttonView.getTag()) 
     } 
    } 
}); 

並在您的異步任務中遍歷此列表並刪除所有項目。

protected void onPostExecute(String file_url) { 
    pDialog.dismiss(); 
    for(int item : Code.arrayList) 
     Code.arrayClasses.remove(item); 
    mCustom_ClassAdapter.notifyDataSetChanged(); 
} 
+0

如果我使用這種上面的代碼然後我不得不這裏通過List_ClassModel mlList_ClassModel = Code.arrayClasses.get(Code.classDel);因爲如果我傳遞Code.arrayList,那麼它顯示錯誤。 – InnocentKiller

+0

我不知道你在Code類中有什麼或做什麼。你必須相應地更新該課程。 – ayorhan

+0

我剛剛聲明classDel爲Code類中的int變量。 public static int classDel; – InnocentKiller

相關問題