Android:将ListActivity与BaseActivity一起使用

时间:2016-03-20 06:58:34

标签: android

我正在处理我的项目,我创建了一个使用ListActivity来显示listview内容的类。但我也想要BaseActivity,以便我可以显示抽屉切换和所有。当我复制drawertoggle方法时,它显示一个错误,说请使用BaseActivity扩展类.Below是一段代码:

LibraryShowData

public class LibraryShowData extends ListActivity     {
    private LibraryDatabase libhelper;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LibraryDatabase empClick = new LibraryDatabase(getApplicationContext());
        Cursor cursor = empClick.getDetails();
        ListView listView = getListView();

        View header = getLayoutInflater().inflate(R.layout.library_header_layout, null);

        if (cursor != null)

            listView.addHeaderView(header);
        listView.setAdapter(
                new android.support.v4.widget.SimpleCursorAdapter(this, R.layout.library_disp_layout, cursor, new String[]{
                        "_id", "Author", "Book","ISBN"
                }, new int[]{R.id.id, R.id.auth, R.id.book,R.id.isbn}, 0));






    }

    @Override                                 // it gives error as BaseActivity should be extended
    protected boolean useDrawerToggle() {
        return false;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_gallery)
            return true;

        if (item.getItemId() == android.R.id.home)
            onBackPressed();

        return super.onOptionsItemSelected(item);
    }


}

1 个答案:

答案 0 :(得分:0)

您在代码中使用ListActivity for的唯一方法是getListView()方法。您可以使用BaseActivitysetContentView(android.R.layout.list_content)

findViewById(android.R.id.list)执行相同的操作
public class LibraryShowData extends BaseActivity {
    private LibraryDatabase libhelper;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(android.R.layout.list_content); // the layout used by ListActivity
        ListView listView = findViewById(android.R.id.list);

        LibraryDatabase empClick = new LibraryDatabase(getApplicationContext());
        Cursor cursor = empClick.getDetails();

        View header = getLayoutInflater().inflate(R.layout.library_header_layout, null);