具有listactivity和cursoradpater的“android-support-v7-appcompat”的ActionBarActivity在同一活动中

时间:2015-01-23 10:16:37

标签: android android-actionbar-compat

我正在尝试扩展actionbaractivity而不是listactivity现在问题是我需要actionbar活动来实现导航抽屉,并且需要listactivity才能显示列表,因为java dosent支持多重继承我无法工作使用它。我可以在下面的代码

中的相同活动中实现列表和操作栏
public class dbMainactivty extends ListActivity {

    // Declare Variables
    public static final String ROW_ID = "row_id";
    private static final String TITLE = "title";
    private ListView noteListView;
    private CursorAdapter noteAdapter;

    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Locate ListView
        noteListView = getListView();
    //  setContentView(R.layout.list_note);
        //noteListView = (ListView) findViewById(R.id.listview);

        // Prepare ListView Item Click Listener
        noteListView.setOnItemClickListener(viewNoteListener);

        // Map all the titles into the ViewTitleNotes TextView
        String[] from = new String[] { TITLE };
        int[] to = new int[] { R.id.ViewTitleNotes };

        // Create a SimpleCursorAdapter
        noteAdapter = new SimpleCursorAdapter(dbMainactivty.this,
                R.layout.list_note, null, from, to);

        // Set the Adapter into SimpleCursorAdapter
        setListAdapter(noteAdapter);
    }

    // Capture ListView item click
    OnItemClickListener viewNoteListener = new OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {

            // Open ViewNote activity
            Intent viewnote = new Intent(dbMainactivty.this, ViewNote.class);

            // Pass the ROW_ID to ViewNote activity
            viewnote.putExtra(ROW_ID, arg3);
            startActivity(viewnote);
        }
    };

    @Override
    protected void onResume() {
        super.onResume();

        // Execute GetNotes Asynctask on return to MainActivity
        new GetNotes().execute((Object[]) null);
    }

    @Override
    protected void onStop() {
        Cursor cursor = noteAdapter.getCursor();

        // Deactivates the Cursor
        if (cursor != null)
            cursor.deactivate();

        noteAdapter.changeCursor(null);
        super.onStop();
    }

    // Create an options menu
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Menu Title
//      menu.add("Add New Notes")
//              .setOnMenuItemClickListener(this.AddNewNoteClickListener)
//              .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

        return super.onCreateOptionsMenu(menu);
    }

    // Capture menu item click
    OnMenuItemClickListener AddNewNoteClickListener = new OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {

            // Open AddEditNotes activity
            Intent addnote = new Intent(dbMainactivty.this, AddEditNotes.class);
            startActivity(addnote);

            return false;

        }
    };

    // GetNotes AsyncTask
    private class GetNotes extends AsyncTask<Object, Object, Cursor> {
        DatabaseConnector dbConnector = new DatabaseConnector(dbMainactivty.this);

        @Override
        protected Cursor doInBackground(Object... params) {
            // Open the database
            dbConnector.open();

            return dbConnector.ListAllNotes("maincat LIKE 'quiz' AND subcat LIKE 'test'");
        }

        @Override
        protected void onPostExecute(Cursor result) {
            noteAdapter.changeCursor(result);

            // Close Database
            dbConnector.close();
        }
    }
}

我认为listview可以解决我的问题,但我是android新手,不知道如何使用curoradapter实现listview

如何在上面的代码中完成

如果我用listview扩展actionbar活动可以吗?

0 个答案:

没有答案