Reputation: 33
I used AsyncTask
to load data remotely and stored it into a ListView
.
The view loads in onPostExecute()
. I registered the ListView
to context menu but it doesn't respond to long click events.
This is the activity:
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;
}
}
... and this is the AsyncTask class:
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) {
}
}
Upvotes: 0
Views: 392
Reputation: 1436
in the override method onContextItemSelected() you return false thats why you are not getting display context menu. if you want to getting display context menu than return true in this override method.
Upvotes: 0
Reputation: 2863
It does not open since your onCreateContextMenu is empty. You need to create your menu there to have something. An example of adding one entry:
@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);
}
Upvotes: 1