ContextMenu没有在ListView上打开长按

时间:2013-04-22 19:24:04

标签: android-listview android-contextmenu

我使用AsyncTask远程加载数据并将其存储到ListView。 视图在onPostExecute()中加载。我将ListView注册到上下文菜单,但它不响应长按事件。

这是活动:

public class BookmarkDeals extends ListActivity {

Context context;
ListActivity activity;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bookmark_deals);
    context = this;
    activity = this;
    getBookmarkDeals();
    this.registerForContextMenu(getListView());

}

private void getBookmarkDeals() {
    new GetBookmarkDealsTask(context, activity).execute(Constants.getBookmarkApi);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.bookmark_deals, menu);
    return true;
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
    // TODO Auto-generated method stub
    super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
    int position = info.position;
    int id = item.getItemId();
    DealsListAdapter adapter = (DealsListAdapter) getListView().getAdapter();
    int dealCode = adapter.getItem(position).getDealCode();
    switch (id) {
    case R.id.con_delete:
        new UpdateBookmarkTask(context, activity).execute(String.valueOf(dealCode));
    }
    return false;
}

}

...这是AsyncTask类:

public class GetBookmarkDealsTask extends AsyncTask<String, Integer, List<Deal>>{
Context context;
Editor editor;
SharedPreferences settings;
Editor settingsEditor;
private DealsListAdapter listAdapter;
private ListActivity activity;
private GoozHttpConnection httpConnection;
private ProgressDialog progressDialog;
private String status;
private String goozErrorMessage;

public GetBookmarkDealsTask(Context context, ListActivity activity) {
    this.context = context;
    this.activity = activity;
    settings = context.getSharedPreferences(Constants.SETTINGS_FILE_NAME,Context.MODE_PRIVATE);
    settingsEditor = settings.edit();
}

@Override
protected void onPreExecute() {
    progressDialog= ProgressDialog.show(activity, "Progress Dialog Title Text","Process Description Text", true);
}

@Override
protected List<Deal> doInBackground(String... urls) {
    httpConnection = new GoozHttpConnection(Constants.getBookmarkApi, activity); // **
    goozErrorMessage = httpConnection.getGoozErrorMessage();
    try {
        if(goozErrorMessage.equals(Constants.NO_CONNECTION)){
            return null;
        }
    } catch (Exception e2) {
        Log.e("goozErrorMessage", e2.getMessage());
    }
    JSONObject json = new JSONObject();
    httpConnection.AddParam(json);
    httpConnection.AddHeader("Content-type", "application/json"); // **

    try {
        httpConnection.Execute(RequestMethod.POST);
    } catch (Exception e) {
        e.printStackTrace();
    }
    String response = httpConnection.getResponse();
    String errorMessage = httpConnection.getErrorMessage();
    if (errorMessage.equalsIgnoreCase("OK")){
        try { 
            JSONObject responseObject = new JSONObject(response);
            status = (String)responseObject.get("Status");
            } catch (JSONException e) {
                e.printStackTrace();
        }

        if (goozErrorMessage.equals(Constants.NO_CONNECTION)) { // no connection.
            return null;
        }
        if (status.equalsIgnoreCase("Success")){ // connection fine and gooz api return success
            return saveToPreferences(response);
        }else { // connection fine and gooz api return error
            goozErrorMessage = httpConnection.getGoozErrorMessage();
            return null;
        }
    }else{
        return null;
    }
}

private List<Deal> saveToPreferences(String response) {
    List<Deal> deals = null;
    try {
        JSONObject responseObject = new JSONObject(response);

        String securityToken = (String)responseObject.get(Constants.SECURITY_TOKEN);
        settingsEditor.putString(Constants.SECURITY_TOKEN, securityToken).commit();         
        JSONArray results = responseObject.getJSONArray("Results");
        deals = new ArrayList<Deal>();
        for (int i = 0; i < results.length(); i++) { 
            JSONObject jDeal = results.getJSONObject(i);
            int dealCode = jDeal.getInt(Constants.DealCode);
            int rating = jDeal.getInt(Constants.Rating);
            int ourPick = jDeal.getInt(Constants.OurPick);
            String branchName = jDeal.getString(Constants.BranchName);
            String branchAdress = jDeal.getString(Constants.BranchAdress);
            String title = jDeal.getString(Constants.Title);
            int distance = jDeal.getInt(Constants.Distance);
            String endDateTime = jDeal.getJSONObject(Constants.Ending).getString(Constants.ExpirationDateTime);
            endDateTime = new GoozDateFormat(endDateTime, context).getDisplayDate();
            String smallImageUrl = jDeal.getString("SmallPhoto");
            deals.add(new Deal(dealCode, rating, ourPick, title, distance, branchName, smallImageUrl, branchAdress, endDateTime));

        }
    }catch (JSONException e) {
        e.printStackTrace();
    }
    return deals;

}
@Override
protected void onPostExecute(List<Deal> data) {
    if (data == null) {
        progressDialog.dismiss();
        Toast.makeText(activity, goozErrorMessage, Toast.LENGTH_SHORT).show();
    }else{
        listAdapter = new DealsListAdapter(context, data);
        activity.registerForContextMenu(activity.getListView());
        activity.setListAdapter(listAdapter);
        progressDialog.dismiss();
        for (Deal deal : data) {
            deal.loadImage(listAdapter);
        }

    }
}


@Override
protected void onProgressUpdate(Integer... progress) {

}

}

2 个答案:

答案 0 :(得分:1)

由于您的onCreateContextMenu为空,因此无法打开。您需要在那里创建菜单以获得某些东西。添加一个条目的示例:

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
    ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.add(0, DELETE_ID, 0, R.string.menu_delete);
}

答案 1 :(得分:0)

在覆盖方法onContextItemSelected()中

,你返回false,这就是为什么你没有得到显示上下文菜单。如果你想得到显示上下文菜单而不是在这个覆盖方法中返回true。