闹钟记事本(结对项目)—— 主界面

这是我们闹钟记事本的主界面

[](http://images2015.cnblogs.com/blog/1119727/201706/1119727-20170612111626040-57008782.png

)

主界面的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg"
    android:orientation="vertical"
    android:paddingLeft="3dp"
    android:paddingRight="3dp" >

    <ListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:text="欢迎使用--->>>闹钟记事--->>>"
        android:textColor="#0000CD" >
    </TextView>

    <TextView
        android:id="@+id/TextView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是一款带闹钟提醒功能的记事本软件,希望能为你带来全新的体验,请点击 Menu键显示选项菜单开始记事" />

</LinearLayout>

主界面的代码

public class MainActivity extends ListActivity {
    /** Called when the activity is first created. */
    private SQLiteDatabase db;
    private String noteId;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    }

    @Override
    // 调用onStart(),刷新页面
    protected void onStart() {
        query();
        super.onStart();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater flater = getMenuInflater();
        flater.inflate(R.menu.menu1, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        int menuId = item.getItemId();
        switch (menuId) {
        // 新建
        case R.id.news:
            Intent intent = new Intent(MainActivity.this, NextActivity.class);
            startActivity(intent);
            break;
        case R.id.delete:
            // 删除
            if (noteId != null) {
                db.delete(Notes.TABLENAME, Notes._ID + "=?",
                        new String[] { noteId });
                Toast.makeText(MainActivity.this, "删除成功!!", Toast.LENGTH_LONG)
                        .show();
                query();
            } else {
                Toast.makeText(MainActivity.this, "无记录!!", Toast.LENGTH_LONG)
                        .show();
            }

            break;
        }
        return super.onMenuItemSelected(featureId, item);
    }

    public void query() {
        // 创建数据库
        db = openOrCreateDatabase(Notes.DBNAME, Context.MODE_PRIVATE, null);
        int version = db.getVersion();
        if (version < 1) {
            db.execSQL("create table " + Notes.TABLENAME + "(" + Notes._ID
                    + " integer primary key," + Notes.TITLE + " text,"
                    + Notes.CONTENT + " text," + Notes.LTIME + " text)");
            db.setVersion(1);
            Toast.makeText(MainActivity.this, "数据库创建成功!!", Toast.LENGTH_LONG)
                    .show();
        }

        // 查询表信息
        Cursor cursor = db.query(Notes.TABLENAME, new String[] { Notes._ID,
                Notes.TITLE }, null, null, null, null, null);
        ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

        // 提供数据
        if (cursor != null) {
            while (cursor.moveToNext()) {
                HashMap<String, String> map = new HashMap<String, String>();
                String noteId = cursor.getString(cursor
                        .getColumnIndex(Notes._ID));
                String ltime = cursor.getString(cursor
                        .getColumnIndex(Notes.TITLE));
                map.put("noteId", noteId);
                map.put("ltime", ltime);
                list.add(map);
            }

            // 适配器
            SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, list,
                    R.layout.list, new String[] { "noteId", "ltime" },
                    new int[] { R.id.ids, R.id.ltime });
            setListAdapter(adapter);
        } else {
            Toast.makeText(MainActivity.this, "暂无记录!!", Toast.LENGTH_LONG)
                    .show();
        }

        ListView lv = getListView();

        // 选择监听器
        lv.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                TextView txtId = (TextView) arg1.findViewById(R.id.ids);
                noteId = txtId.getText().toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });
        // 点击监听器
        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                TextView txtId = (TextView) arg1.findViewById(R.id.ids);

                noteId = txtId.getText().toString();
                Cursor cursor = db.query(Notes.TABLENAME,
                        new String[] { Notes.CONTENT }, Notes._ID + "=?",
                        new String[] { noteId }, null, null, null);
                if (cursor.moveToNext()) {
                    String content1 = cursor.getString(cursor
                            .getColumnIndex(Notes.CONTENT));
                    Bundle extras = new Bundle();
                    extras.putString("content1", content1);
                    extras.putString("noteId", noteId);
                    Intent intent1 = new Intent(MainActivity.this,
                            NextActivity.class);
                    intent1.putExtras(extras);
                    startActivity(intent1);
                }

            }
        });
    }
}

转载于:https://www.cnblogs.com/kutfine/p/6994577.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值