我正在尝试在我的应用程序中添加新闻列表。我可以成功地将其创建为单独的页面/活动。现在我想在新闻菜单项中显示listview,这是我的选项菜单之一。此外,我想将每个listview内容显示为单独的活动,点击该特定列表项:
通过此MainActivity.java,我的选项菜单不会显示。问题出在哪儿?
这是我的主要活动.java
package com.example.newlistview;
import android.support.v7.app.ActionBarActivity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
// storing string resources into Array
String[] adobe_products = getResources().getStringArray(R.array.adobe_products);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.news, R.id.label, adobe_products));
ListView lv = getListView();
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch( position ) {
case 0: Intent e = new Intent(view.getContext(), News1.class);
startActivity(e);
break;
case 1: Intent f = new Intent(view.getContext(), About.class);
startActivity(f);
break;
}
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.news:
Intent a = new Intent(getApplicationContext(), News.class);
startActivity(a);
return true;
case R.id.about:
Intent f = new Intent(getApplicationContext(), About.class);
startActivity(f);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
答案 0 :(得分:0)
您是否创建了任何选项菜单?您可能需要为选项菜单创建布局并对其进行充气。
例如:
/res/menu/menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/news" />
<item android:id="@+id/about" />
</menu>
并添加以下代码:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}