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

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

主界面的布局

<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);
				}

			}
		});
	}
}
posted @ 2017-06-12 19:18  八七七八  阅读(138)  评论(0编辑  收藏  举报