我的问题如下,我有这个assynctask获取我想要删除的产品的ID。但我无法传递正确的ID,因为我想使用上下文菜单来选择我想要的项目。我alredy在我的上下文菜单上得到了想要的id但是当我把它传递给我的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并将其传递给名称变量。然后:
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 :(得分: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));