Advertisement
Guest User

Untitled

a guest
Jun 19th, 2012
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. Android: oncreateoptionsmenu not called with asyncTask
  2. public class TopNewsActivity extends ListActivity {
  3.  
  4. public static final String LOG_TAG = "Infra";
  5. private ProgressDialog progressDialog;
  6.  
  7. /** Called when the activity is first created. */
  8. @Override
  9. public void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.listplaceholder);
  12. new BackgroundAsyncTask().execute();
  13. }
  14.  
  15. public class BackgroundAsyncTask extends AsyncTask<String, Integer, ArrayList<HashMap<String, String>>> {
  16.  
  17. @Override
  18. protected void onPreExecute() {
  19. progressDialog = new ProgressDialog(TopNewsGroup.group);
  20. progressDialog.setCancelable(true);
  21. progressDialog.setMessage("Loading...");
  22. progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
  23. progressDialog.setProgress(0);
  24. progressDialog.show();
  25. }
  26.  
  27. @Override
  28. protected ArrayList<HashMap<String, String>> doInBackground(String... paths) {
  29.  
  30. ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
  31.  
  32. String xml = XMLfunctions.getTopNewsXML();
  33. Document doc = XMLfunctions.XMLfromString(xml);
  34.  
  35. int numResults = XMLfunctions.numResults(doc);
  36. Log.d(LOG_TAG, "Number of Results: " + numResults);
  37. if ((numResults <= 0)) {
  38. Toast.makeText(TopNewsActivity.this, "No Result Found",Toast.LENGTH_LONG).show();
  39. return null;
  40. }
  41.  
  42. NodeList nodes = doc.getElementsByTagName("result");
  43.  
  44. for (int i = 0; i < nodes.getLength(); i++) {
  45. HashMap<String, String> map = new HashMap<String, String>();
  46.  
  47. Element e = (Element) nodes.item(i);
  48. map.put("id", XMLfunctions.getValue(e, "id"));
  49. map.put("title", XMLfunctions.getValue(e, "title"));
  50. mylist.add(map);
  51. }
  52. return mylist;
  53. }
  54.  
  55. @Override
  56. protected void onProgressUpdate(Integer... values) {
  57. super.onProgressUpdate(values);
  58. }
  59.  
  60. protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
  61.  
  62. ListAdapter adapter = new SimpleAdapter(TopNewsActivity.this, result, R.layout.list_item, new String[] { "title" }, new int[] { R.id.item_title });
  63. setListAdapter(adapter);
  64. progressDialog.dismiss();
  65.  
  66. final ListView lv = getListView();
  67.  
  68. lv.setTextFilterEnabled(true);
  69. lv.setOnItemClickListener(new OnItemClickListener() {
  70. @SuppressWarnings("unchecked")
  71. @Override
  72. public void onItemClick(AdapterView<?> a, View view, final int position, long id) {
  73.  
  74. HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
  75.  
  76. Intent i = new Intent(TopNewsActivity.this, NewsDetails.class);
  77. i.putExtra("content_id", o.get("id"));
  78. i.putExtra("title", o.get("title"));
  79. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  80.  
  81. View v = TopNewsGroup.group.getLocalActivityManager().startActivity("ShowNews", i).getDecorView();
  82.  
  83. // Again, replace the view
  84. TopNewsGroup.group.setContentView(v);
  85. }
  86. });
  87. }
  88. }
  89.  
  90. public class MySimpleAdapter extends SimpleAdapter {
  91. public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
  92. super(context, data, resource, from, to);
  93. // TODO Auto-generated constructor stub
  94. }
  95. }
  96.  
  97. @Override
  98. public boolean onCreateOptionsMenu(Menu menu) {
  99.  
  100. MenuInflater inflater = getMenuInflater();
  101. inflater.inflate(R.menu.optionsmenu, menu);
  102. return true;
  103. }
  104.  
  105. @Override
  106. public boolean onOptionsItemSelected(MenuItem item){
  107.  
  108. switch (item.getItemId()){
  109.  
  110. case R.id.refresh:
  111. startActivity(new Intent(this, TopNewsGroup.class));
  112. return true;
  113.  
  114. case R.id.search:
  115. startActivity(new Intent(this, SearchActivity.class));
  116. return true;
  117.  
  118. case R.id.info:
  119. startActivity(new Intent(this, TopNewsGroup.class));
  120. return true;
  121.  
  122. case R.id.exit:
  123. finish();
  124. return true;
  125. }
  126. return false;
  127. }
  128.  
  129. }
  130.  
  131. @Override
  132. public boolean onCreateOptionsMenu(Menu menu) {
  133. return this.getCurrentActivity().onCreateOptionsMenu(menu);
  134. }
  135.  
  136. @Override
  137. public boolean onOptionsItemSelected(MenuItem item) {
  138. return this.getCurrentActivity().onOptionsItemSelected(item);
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement