我的记事本项目之路(三)

        接下来就是主界面的实现了,主界面的布局的话非常简单,就是采用一个ListView就好,activity_main.xml布局文件代码如下:

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.jishiben.MainActivity"
    android:background="#ADFF2F">

    <ListView
        android:id="@+id/lv_note"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</RelativeLayout>

主界面布局还采用了ActionBar,所以在menu文件夹下的main.xml修改如下:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.jishiben.MainActivity" >

    <item
        android:id="@+id/action_new_note"
        android:icon="@drawable/ic_actionbar_new"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:title="@string/action_new_note"/>

</menu>


这里的话因为要将数据存到数据库里面去,所以需要建一个数据库支持类DBHelper.java,功能是创建一个用来存储数据的表代码如下:

package com.example.jishiben;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {

    public static final String TABLE_NAME = "t_note";

    public DBHelper(Context context) {
        super(context, "db_notes", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_NAME + " ("
                + "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                + "date nvarchar(20)," + "type nvarchar(20),"
                + "introduction TEXT NOT NULL" + ");");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

}


然后就是主界面类的实现了长按弹出上下文菜单,并对ListView实现了分页,这样做的好处是,当事件很多的时候能够更加方便的查找到所需要的信息,并且实现了点击新建按钮跳转到编辑页面的功能,实时刷新数据,MainActivity的代码如下:

package com.example.jishiben;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;

public class MainActivity extends Activity {
    List<Note> notes = new ArrayList<Note>();
    MyBaseAdapter adapter = null;
    ListView listView01 = null;
    NoteManager noteManager;
    int pageNo = 1;
    int pageSize = 100;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        noteManager = new NoteManager(this);
        listView01 = (ListView) findViewById(R.id.lv_note);
        adapter = new MyBaseAdapter(this, notes);
        listView01.setAdapter(adapter);
        this.registerForContextMenu(listView01);
}
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        // 设置了上下文菜单的标题
        menu.setHeaderTitle("操作");
        // 设置上下文菜单的选项
        menu.add(1, 1, 0, "编辑");
        menu.add(1, 2, 1, "删除");
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo acmenuinfo = (AdapterContextMenuInfo) item.getMenuInfo();
        int position = (int) adapter.getItemId(acmenuinfo.position);
        switch (item.getItemId()) {
        case 1:
            // 跳转到编辑界面
            enterEditNoteActivity(position);
            return true;
        case 2:
            noteManager.deleteNote(notes.get(position));
            notes.remove(position);
            adapter.notifyDataSetChanged();
            return true;
        }
        
        return false;
    }


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

    private void refreshNoteData() {
        notes.clear();
        List<Note> list = noteManager.getNotes(pageNo, pageSize);
        notes.addAll(list);
        adapter.notifyDataSetChanged();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_new_note) {
            Intent intent = new Intent();
            intent.setClass(this, EditNoteActivity.class);
            startActivity(intent);
            return true;
        } else {
            onContextItemSelected(item);  
            return false;
        }
    }

    private void enterEditNoteActivity(int position) {
        Note note = notes.get(position);
        Intent intent = new Intent(MainActivity.this, EditNoteActivity.class);
        intent.putExtra("note", note);
        startActivity(intent);
    }
}
预知后事如何,请听下回分解.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值