Android:oncreateoptionsmenu 未使用 asyncTask 调用
我创建了一个具有选项卡活动的应用程序,该应用程序又具有 5 个不同的列表活动,其中有选项菜单。到目前为止,它工作正常,但由于我添加了 asyncTask,选项菜单 在第一个实例中不起作用,但是当我切换选项卡并返回到我的第一个选项卡时,只有选项菜单在单击时打开。
代码
public class TopNewsActivity extends ListActivity {
public static final String LOG_TAG = "Infra";
private ProgressDialog progressDialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
new BackgroundAsyncTask().execute();
}
public class BackgroundAsyncTask extends AsyncTask<String, Integer, ArrayList<HashMap<String, String>>> {
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(TopNewsGroup.group);
progressDialog.setCancelable(true);
progressDialog.setMessage("Loading...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setProgress(0);
progressDialog.show();
}
@Override
protected ArrayList<HashMap<String, String>> doInBackground(String... paths) {
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
String xml = XMLfunctions.getTopNewsXML();
Document doc = XMLfunctions.XMLfromString(xml);
int numResults = XMLfunctions.numResults(doc);
Log.d(LOG_TAG, "Number of Results: " + numResults);
if ((numResults <= 0)) {
Toast.makeText(TopNewsActivity.this, "No Result Found",Toast.LENGTH_LONG).show();
return null;
}
NodeList nodes = doc.getElementsByTagName("result");
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nodes.item(i);
map.put("id", XMLfunctions.getValue(e, "id"));
map.put("title", XMLfunctions.getValue(e, "title"));
mylist.add(map);
}
return mylist;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
ListAdapter adapter = new SimpleAdapter(TopNewsActivity.this, result, R.layout.list_item, new String[] { "title" }, new int[] { R.id.item_title });
setListAdapter(adapter);
progressDialog.dismiss();
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
@SuppressWarnings("unchecked")
@Override
public void onItemClick(AdapterView<?> a, View view, final int position, long id) {
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Intent i = new Intent(TopNewsActivity.this, NewsDetails.class);
i.putExtra("content_id", o.get("id"));
i.putExtra("title", o.get("title"));
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
View v = TopNewsGroup.group.getLocalActivityManager().startActivity("ShowNews", i).getDecorView();
// Again, replace the view
TopNewsGroup.group.setContentView(v);
}
});
}
}
public class MySimpleAdapter extends SimpleAdapter {
public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
// TODO Auto-generated constructor stub
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.optionsmenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.refresh:
startActivity(new Intent(this, TopNewsGroup.class));
return true;
case R.id.search:
startActivity(new Intent(this, SearchActivity.class));
return true;
case R.id.info:
startActivity(new Intent(this, TopNewsGroup.class));
return true;
case R.id.exit:
finish();
return true;
}
return false;
}
}
请帮忙。
I have created an application having tab activity which in turn has 5 different list activities in which the options menu is there. Till now it was working fine but since i've added asyncTask the Options Menu does not work at first instance but when i switch the tabs and come back to my first tab then only the options menu opens on click.
Code
public class TopNewsActivity extends ListActivity {
public static final String LOG_TAG = "Infra";
private ProgressDialog progressDialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
new BackgroundAsyncTask().execute();
}
public class BackgroundAsyncTask extends AsyncTask<String, Integer, ArrayList<HashMap<String, String>>> {
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(TopNewsGroup.group);
progressDialog.setCancelable(true);
progressDialog.setMessage("Loading...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setProgress(0);
progressDialog.show();
}
@Override
protected ArrayList<HashMap<String, String>> doInBackground(String... paths) {
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
String xml = XMLfunctions.getTopNewsXML();
Document doc = XMLfunctions.XMLfromString(xml);
int numResults = XMLfunctions.numResults(doc);
Log.d(LOG_TAG, "Number of Results: " + numResults);
if ((numResults <= 0)) {
Toast.makeText(TopNewsActivity.this, "No Result Found",Toast.LENGTH_LONG).show();
return null;
}
NodeList nodes = doc.getElementsByTagName("result");
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nodes.item(i);
map.put("id", XMLfunctions.getValue(e, "id"));
map.put("title", XMLfunctions.getValue(e, "title"));
mylist.add(map);
}
return mylist;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
ListAdapter adapter = new SimpleAdapter(TopNewsActivity.this, result, R.layout.list_item, new String[] { "title" }, new int[] { R.id.item_title });
setListAdapter(adapter);
progressDialog.dismiss();
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
@SuppressWarnings("unchecked")
@Override
public void onItemClick(AdapterView<?> a, View view, final int position, long id) {
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Intent i = new Intent(TopNewsActivity.this, NewsDetails.class);
i.putExtra("content_id", o.get("id"));
i.putExtra("title", o.get("title"));
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
View v = TopNewsGroup.group.getLocalActivityManager().startActivity("ShowNews", i).getDecorView();
// Again, replace the view
TopNewsGroup.group.setContentView(v);
}
});
}
}
public class MySimpleAdapter extends SimpleAdapter {
public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
// TODO Auto-generated constructor stub
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.optionsmenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.refresh:
startActivity(new Intent(this, TopNewsGroup.class));
return true;
case R.id.search:
startActivity(new Intent(this, SearchActivity.class));
return true;
case R.id.info:
startActivity(new Intent(this, TopNewsGroup.class));
return true;
case R.id.exit:
finish();
return true;
}
return false;
}
}
Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我使用 ActivityGroup 时,我需要重写以下函数来显示选项菜单
As i use ActivityGroup i needed to override the following functions to display the optionsmenu