我曾经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) {
}
}