How to get supportactionbar when my class extends

2019-08-05 07:09发布

问题:

I have a class which extends ExpandableListActivity to display my data as explandable list view, now my app supporting older version till API 7 how to add ActionBar using getSupportActionBar() to this class...Please help me out

public class Facts extends ExpandableListActivity {


public void onCreate(Bundle savedInstanceState) {

    try{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.facts);

        SimpleExpandableListAdapter expListAdapter =
                new SimpleExpandableListAdapter(
                        this,
                        createGroupList(),              // Creating group List.
                        R.layout.facts_grouprow,                // Group item layout XML.
                        new String[] { "questions" },   // the key of group item.
                        new int[] { R.id.row_name },    // ID of each group item.-Data under the key goes into this TextView.
                        createChildList(),              // childData describes second-level entries.
                        R.layout.facts_childrow,                // Layout for sub-level entries(second level).
                        new String[] {"answers"},       // Keys in childData maps to display.
                        new int[] { R.id.grp_child}     // Data under the keys above go into these TextViews.
                        );
        setListAdapter(expListAdapter);     // setting the adapter in the list

    }catch(Exception e){
        System.out.println("Errrr +++ " + e.getMessage());
    }

}

now suggest me to how to achieve it.

回答1:

Looking at the ExpandableListActivity source code, it seems like it would be easy to duplicate the same functionality in your own class and have it extend ActionBarActivity as its base class.



回答2:

I would suggest you to use the ActionBarActivity. There you have the method getSupportActionBar.

To show your ExpandableList you can add a ListFragment from the support-v4 lib. There is a method called setListAdapter() where you can add a ExpandableListAdapter.

Here is an example.