2014-04-13 187 views
0

我的問題是以下,我有這個assynctask獲取我想要刪除的產品的id。但我不能傳遞正確的ID,因爲我想使用上下文菜單來選擇我想要放棄的項目。我alredy得到了我的上下文菜單上想要的id,但是當我將它傳遞給我的asynctask時,它不知道我能做什麼。AsyncTask上下文菜單

我所做的:

public boolean onContextItemSelected(MenuItem item) { 
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 
    switch(item.getItemId()) { 
    case R.id.item1: 


     HashMap <String, String> product = productsList.get(info.position); 
     final String name = product.get(TAG_ID); 
     // Starting new intent 


     new DeleteProduct().execute(); 

      return true; 

在這裏,我獲得該項目的ID,並把它傳遞給名稱variable.Then:

params.add(new BasicNameValuePair("id", name)); 

我把它傳遞給的AsyncTask但它不承認

全碼:

/***************************************************************** 
* Background Async Task to Delete Product 
* */ 
class DeleteProduct extends AsyncTask<String, String, String> { 

    /** 
    * Before starting background thread Show Progress Dialog 
    * */ 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(AllProductsActivity.this); 
     pDialog.setMessage("Deleting Product..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
    } 

    /** 
    * Deleting product 
    * */ 
    protected String doInBackground(String... args) { 

     // Check for success tag 
     int success; 
     try { 
      // Building Parameters 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("id", name)); 

      // getting product details by making HTTP request 
      JSONObject json = jParser.makeHttpRequest(
        url_delete_product, "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) { 
       // product successfully deleted 
       // notify previous activity by sending code 100 
       Intent i = getIntent(); 
       // send result code 100 to notify about product deletion 
       setResult(100, i); 
       finish(); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 

    /** 
    * After completing background task Dismiss the progress dialog 
    * **/ 
    protected void onPostExecute(String file_url) { 
     // dismiss the dialog once product deleted 
     pDialog.dismiss(); 

    } 

} 



@Override 
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){ 
    getMenuInflater().inflate(R.menu.context, menu); 
} 

public boolean onContextItemSelected(MenuItem item) { 
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 
    switch(item.getItemId()) { 
    case R.id.item1: 


     HashMap <String, String> product = productsList.get(info.position); 
     final String name = product.get(TAG_ID); 
     // Starting new intent 


     new DeleteProduct().execute(); 

      return true; 


    case R.id.item2: 


     return true; 
    default: 
      return super.onContextItemSelected(item); 
    } 
} 
+0

,您可以發送到PARAMS爲的AsyncTask例如 –

回答

1

如果您參考AsyncTask documentation,它應該很清楚。

更改任務的調用:

final String name = product.get(TAG_ID); 
     // Starting new intent 
     new DeleteProduct().execute(name); // pass the name here 

,然後讀出來的的AsyncTask像這樣:

protected String doInBackground(String... args) { 
     String name = args[0]; // receive the argument here 
     // Check for success tag 
     int success; 
     try { 
      // Building Parameters 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("id", name));