I am trying to add an list of news in my application. I could successfully create this as a separate page/activity. Now I want to display the listview in the news menu item which is one of my options menu. Also I want to display each listview content as a separate activity onclick of that particuler listitem:
By this MainActivity.java my options menu is not displayed. Where is the problem?
Here is my main activity.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);
}
}
}
Did you create any option menu? You probably need to create a layout for the option menu and inflate it.
For example:
/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>
And add following code:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.