Actionbar and ListActivity in one Activity

北城以北 提交于 2019-12-08 00:37:13

问题


I have a program which shows lists of files in directories on the SDCARD and when the user clicks on a file it opens the PDF up. For instance one activity will display the contents of one folder.

I have been wanting to add the ActionBar for navigation and can get the ActionBar to work in its own activity and my ListActivity to work in its own activity, however I cannot get them to work together.

I am not sure how to combine the two so that I end up with one activity that displays the action bar and the file list in the one Activity?

The Actionbar Activity starts with

public class SecondActivity extends FragmentActivity implements ActionBar.OnNavigationListener {

The Activity that displays the clickable list starts with

public class MainActivity extends ListActivity {

My android app programming is quite basic and self taught so I am probably missing something obvious. Thanks in advance


回答1:


I think you can extend your class to ListActivity and override method onCreateOptionsMenu to bind your action bar to the activity:

public class MainActivity extends ListActivity {
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.your_action_bar_name_here, menu);

        return (super.onCreateOptionsMenu(menu));
    }

    // your other methods
}



回答2:


I have posted an alternative solution that separates the concerns of the List and the ActionBar. Makes the code a little tidier IMO.



来源:https://stackoverflow.com/questions/13875090/actionbar-and-listactivity-in-one-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!