Skip to content

Commit

Permalink
hzuapps#95 hzuapps#145 日记功能的实现
Browse files Browse the repository at this point in the history
  • Loading branch information
Axin27 committed May 11, 2016
1 parent 75e54ac commit dc311cf
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@


package edu.hzuapps.androidworks.homeworks.net1314080903127;

import com.donglihan.CollegeLifeManager.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Net1314080903127_DiaryEditActivity extends Activity {

private EditText mTitleText;
private EditText mBodyText;
private Long mRowId;
private Net1314080903127_DbAdapter mDbHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDbHelper = new Net1314080903127_DbAdapter(this);
setContentView(R.layout.net1314080903127_diary_edit);

mTitleText = (EditText) findViewById(R.id.title);
mBodyText = (EditText) findViewById(R.id.body);

Button confirmButton = (Button) findViewById(R.id.confirm);

mRowId = null;
Bundle extras = getIntent().getExtras();
//ÅжÏÊÇ·ñΪ±à¼­×´Ì¬
if (extras != null) {
String title = extras.getString(Net1314080903127_DbAdapter.KEY_TITLE);
String body = extras.getString(Net1314080903127_DbAdapter.KEY_BODY);
mRowId = extras.getLong(Net1314080903127_DbAdapter.KEY_ROWID);

if (title != null) {
mTitleText.setText(title);
}
if (body != null) {
mBodyText.setText(body);
}
}

confirmButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mDbHelper.open();
String title = mTitleText.getText().toString();
String body = mBodyText.getText().toString();
if (mRowId != null) {
mDbHelper.updateDiary(mRowId, title, body);
} else
mDbHelper.createDiary(title, body);
Intent mIntent = new Intent();
setResult(RESULT_OK, mIntent);
mDbHelper.closeclose();
finish();
}

});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package edu.hzuapps.androidworks.homeworks.net1314080903127;

import com.donglihan.CollegeLifeManager.R;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class Net1314080903127_diary_activity extends ListActivity {

//回复的关键字
private static final int ACTIVITY_CREATE = 0;
private static final int ACTIVITY_EDIT = 1;

//菜单的选择
private static final int INSERT_ID = Menu.FIRST;
private static final int DELETE_ID = Menu.FIRST + 1;

private Net1314080903127_DbAdapter mDbHelper;
private Cursor mDiaryCursor;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.net1314080903127_diary_list);
mDbHelper = new Net1314080903127_DbAdapter(this);
updateListView();

}

//更新当前的listacvitity
private void updateListView() {
mDbHelper.open();
mDiaryCursor = mDbHelper.getAllNotes();
//交给Activity管理游标
startManagingCursor(mDiaryCursor);
String[] from = new String[] { Net1314080903127_DbAdapter.KEY_TITLE,
Net1314080903127_DbAdapter.KEY_CREATED };
int[] to = new int[] { R.id.text1, R.id.created };
SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
R.layout.net1314080903127_diary_row, mDiaryCursor, from, to);
setListAdapter(notes);
mDbHelper.closeclose();
}

//创建一个菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, INSERT_ID, 0,"新建").setIcon(R.drawable.net1314080903127_new_course);
menu.add(0, DELETE_ID, 0, "删除").setIcon(R.drawable.net1314080903127_delete);
return true;
}

//菜单选择
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case INSERT_ID:
createDiary();
return true;
case DELETE_ID:
mDbHelper.open();
mDbHelper.deleteDiary(getListView().getSelectedItemId());
mDbHelper.closeclose();
updateListView();
return true;
}
return super.onMenuItemSelected(featureId, item);
}

private void createDiary() {
Intent i = new Intent(this, Net1314080903127_DiaryEditActivity.class);
startActivityForResult(i, ACTIVITY_CREATE);
}

@Override
// 需要对position和id进行一个很好的区分
// position指的是点击的这个ViewItem在当前ListView中的位置
// 每一个和ViewItem绑定的数据,肯定都有一个id,通过这个id可以找到那条数据。
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Cursor c = mDiaryCursor;
c.moveToPosition(position);
Intent i = new Intent(this, Net1314080903127_DiaryEditActivity.class);
i.putExtra(Net1314080903127_DbAdapter.KEY_ROWID, id);
i.putExtra(Net1314080903127_DbAdapter.KEY_TITLE, c.getString(c
.getColumnIndexOrThrow(Net1314080903127_DbAdapter.KEY_TITLE)));
i.putExtra(Net1314080903127_DbAdapter.KEY_BODY, c.getString(c
.getColumnIndexOrThrow(Net1314080903127_DbAdapter.KEY_BODY)));
startActivityForResult(i, ACTIVITY_EDIT);
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
updateListView();
}
}
30 changes: 30 additions & 0 deletions app/src/main/res/layout/net1314080903127_diary_edit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="标题"
android:padding="2px" />
<EditText android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1" />
</LinearLayout>

<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="内容" />

<EditText android:id="@+id/body" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1"
android:scrollbars="vertical" android:gravity="top" />

<Button android:id="@+id/confirm" android:text="保存"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

</LinearLayout>
13 changes: 13 additions & 0 deletions app/src/main/res/layout/net1314080903127_diary_list.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ListView android:id="@+id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="@+id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="您还没有开始写日记呢!点击Menu按钮开始写日记吧" />
</LinearLayout>
20 changes: 20 additions & 0 deletions app/src/main/res/layout/net1314080903127_diary_row.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/row"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView android:id="@+id/text1"
android:layout_width="wrap_content" android:layout_height="30px"
android:maxWidth="200dip"
android:textSize="22sp"
android:layout_marginTop="10dip"
android:text="第一组第一项" />
<TextView android:id="@+id/created" android:layout_width="wrap_content"
android:layout_height="35px" android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:layout_marginTop="10dip"
android:text="1999年12月3号" />

</RelativeLayout>

0 comments on commit dc311cf

Please sign in to comment.