forked from hzuapps/android-labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hzuapps#95 hzuapps#145 实验4、9 课程表和日记功能的实现
- Loading branch information
Showing
8 changed files
with
775 additions
and
0 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
...u/hzuapps/androidworks/homeworks/net1314080903127/Net1314080903127_DiaryEditActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
|
||
|
||
package edu.hzuapps.androidworks.homeworks.net1314080903127; | ||
|
||
|
||
import android.app.Activity; | ||
import android.content.Intent; | ||
import android.os.Bundle; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.EditText; | ||
import edu.hzuapps.androidworks.net1314080903127.R; | ||
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(); | ||
} | ||
|
||
}); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
...va/edu/hzuapps/androidworks/homeworks/net1314080903127/Net1314080903127_MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package edu.hzuapps.androidworks.homeworks.net1314080903127; | ||
|
||
import android.app.Activity; | ||
import android.content.Intent; | ||
import android.os.Bundle; | ||
import android.view.Menu; | ||
import android.view.MenuItem; | ||
import android.view.View; | ||
import android.view.View.OnClickListener; | ||
import android.widget.Button; | ||
import android.widget.Toast; | ||
import edu.hzuapps.androidworks.net1314080903127.R; | ||
public class Net1314080903127_MainActivity extends Activity { | ||
|
||
//菜单选项 | ||
public static final int HELP = Menu.FIRST; | ||
public static final int EXIT = Menu.FIRST + 1; | ||
public static final int SCORE = Menu.FIRST + 2; | ||
|
||
/** Called when the activity is first created. */ | ||
@Override | ||
public void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.net1314080903127_main); | ||
setTitle("大学生活管理助手"); | ||
find_and_modify_button(); | ||
Toast.makeText(Net1314080903127_MainActivity.this, | ||
"欢迎使用大学生活管理助手", Toast.LENGTH_SHORT).show(); | ||
} | ||
|
||
private void find_and_modify_button() { | ||
// TODO Auto-generated method stub | ||
Button btn_course = (Button) findViewById(R.id.btn_course); | ||
btn_course.setOnClickListener(course_listener); | ||
Button btn_diary = (Button) findViewById(R.id.btn_diary); | ||
btn_diary.setOnClickListener(diary_listener); | ||
} | ||
private Button.OnClickListener course_listener = new OnClickListener() | ||
{ | ||
@Override | ||
public void onClick(View v) { | ||
// TODO Auto-generated method stub | ||
Intent intent = new Intent(); | ||
intent.setClass(Net1314080903127_MainActivity.this, Net1314080903127_course_activity.class); | ||
startActivity(intent); | ||
} | ||
}; | ||
private Button.OnClickListener diary_listener = new OnClickListener() | ||
{ | ||
@Override | ||
public void onClick(View v) { | ||
// TODO Auto-generated method stub | ||
Intent intent = new Intent(); | ||
intent.setClass(Net1314080903127_MainActivity.this, Net1314080903127_diary_activity.class); | ||
startActivity(intent); | ||
} | ||
}; | ||
|
||
|
||
@Override | ||
/* | ||
* menu.findItem(EXIT_ID);找到特定的MenuItem | ||
* MenuItem.setIcon.可以设置menu按钮的背景 | ||
*/ | ||
public boolean onCreateOptionsMenu(Menu menu) { | ||
super.onCreateOptionsMenu(menu); | ||
menu.add(0, HELP, 0, "帮助").setIcon(R.drawable.net1314080903127_helps); | ||
menu.add(0, EXIT, 0, "退出").setIcon(R.drawable.net1314080903127_exit); | ||
menu.add(0, SCORE, 0, "评分").setIcon(R.drawable.net1314080903127_score); | ||
return true; | ||
} | ||
|
||
public boolean onOptionsItemSelected(MenuItem item) { | ||
switch (item.getItemId()) { | ||
case HELP: | ||
Intent help_intent = new Intent(); | ||
help_intent.setClass(Net1314080903127_MainActivity.this, Net1314080903127_help_activity.class); | ||
startActivity(help_intent); | ||
break; | ||
case SCORE: | ||
Intent score_intent = new Intent(); | ||
score_intent.setClass(Net1314080903127_MainActivity.this, Net1314080903127_score_activity.class); | ||
startActivity(score_intent); | ||
break; | ||
case EXIT: | ||
finish(); | ||
break; | ||
|
||
} | ||
return super.onOptionsItemSelected(item); | ||
} | ||
|
||
} |
196 changes: 196 additions & 0 deletions
196
...edu/hzuapps/androidworks/homeworks/net1314080903127/Net1314080903127_course_activity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
/** | ||
* | ||
*/ | ||
package edu.hzuapps.androidworks.homeworks.net1314080903127; | ||
|
||
import java.sql.Date; | ||
import java.util.Calendar; | ||
|
||
import android.app.ListActivity; | ||
import android.content.Intent; | ||
import android.database.Cursor; | ||
import android.os.Bundle; | ||
import android.util.Log; | ||
import android.view.Menu; | ||
import android.view.MenuItem; | ||
import android.widget.SimpleCursorAdapter; | ||
import android.widget.Toast; | ||
import edu.hzuapps.androidworks.net1314080903127.R; | ||
|
||
|
||
public class Net1314080903127_course_activity extends ListActivity { | ||
|
||
//菜单选项 | ||
public static final int NEW = Menu.FIRST; | ||
public static final int SET = Menu.FIRST + 1; | ||
public static final int DELETE = Menu.FIRST + 2; | ||
public static final int HELP = Menu.FIRST + 3; | ||
|
||
//传值确认的关键字 | ||
private static final int REQUEST_SET = 0; | ||
private static final int REQUEST_NEW = 1; | ||
|
||
//默认的第一周开始时间 | ||
int first_year = 2010; | ||
int first_month = 9; | ||
int first_day = 1; | ||
Date start_date = new Date(first_year,first_month,first_day); | ||
|
||
//新建的课程信息 | ||
String course_name = ""; | ||
String week_start = ""; | ||
String week_end = ""; | ||
String course_index1 = ""; | ||
String course_place = ""; | ||
String week_index = ""; | ||
|
||
//当前日期 | ||
Calendar c = Calendar.getInstance(); | ||
int now_year = c.get(Calendar.YEAR); | ||
int now_month = c.get(Calendar.MONTH); | ||
int now_day = c.get(Calendar.DAY_OF_MONTH); | ||
Date now_date = new Date(now_year,now_month,now_day); | ||
|
||
//现在是第几周 | ||
int interval_weeks = 1; | ||
|
||
//数据库操作 | ||
private Net1314080903127_DbAdapter mDbHelper; | ||
private Cursor mCourseCursor; | ||
|
||
/* (non-Javadoc) | ||
* @see android.app.Activity#onCreate(android.os.Bundle) | ||
*/ | ||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
// TODO Auto-generated method stub | ||
super.onCreate(savedInstanceState); | ||
setTitle("本周课程信息"); | ||
setContentView(R.layout.net1314080903127_course_activity); | ||
|
||
mDbHelper = new Net1314080903127_DbAdapter(this); | ||
updateCourseView(); | ||
} | ||
/** | ||
* 更新listactivity的数据 | ||
*/ | ||
private void updateCourseView() { | ||
// TODO Auto-generated method stub | ||
Log.e("done", "getcourse"); | ||
mDbHelper.open(); | ||
mCourseCursor = mDbHelper.getAllCourses(interval_weeks); | ||
Toast.makeText(Net1314080903127_course_activity.this, | ||
"当前是第"+interval_weeks +"周,点击menu设置", Toast.LENGTH_SHORT).show(); | ||
Log.e("weeks"," " +interval_weeks); | ||
Log.e("done", "donegetcourse"); | ||
setTitle("第"+interval_weeks +"周 "+"课程信息"); | ||
startManagingCursor(mCourseCursor); | ||
|
||
String[] from = new String[] { Net1314080903127_DbAdapter.KEY_NAME, Net1314080903127_DbAdapter.KEY_PLACE, Net1314080903127_DbAdapter.KEY_INDEX, Net1314080903127_DbAdapter.KEY_WEEK_INDEX}; | ||
int[] to = new int[] { R.id.item_name, R.id.item_place, R.id.item_index, R.id.item_week_index }; | ||
SimpleCursorAdapter courses = new SimpleCursorAdapter(this, | ||
R.layout.net1314080903127_course_list_item, mCourseCursor, from, to); | ||
setListAdapter(courses); | ||
mDbHelper.closeclose(); | ||
} | ||
/* (non-Javadoc) | ||
* @see android.app.Activity#onCreateOptionsMenu(android.view.Menu) | ||
*/ | ||
@Override | ||
public boolean onCreateOptionsMenu(Menu menu) { | ||
// TODO Auto-generated method stub | ||
super.onCreateOptionsMenu(menu); | ||
menu.add(0, NEW, 0, "新建").setIcon(R.drawable.net1314080903127_new_course); | ||
menu.add(0, SET, 0, "设置").setIcon(R.drawable.net1314080903127_setting); | ||
menu.add(0, DELETE, 0, "删除").setIcon(R.drawable.net1314080903127_delete); | ||
menu.add(0, HELP, 0, "帮助").setIcon(R.drawable.net1314080903127_helps); | ||
return true; | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem) | ||
*/ | ||
@Override | ||
public boolean onMenuItemSelected(int featureId, MenuItem item) { | ||
// TODO Auto-generated method stub | ||
switch (item.getItemId()) { | ||
case HELP: | ||
Intent help_intent = new Intent(); | ||
help_intent.setClass(Net1314080903127_course_activity.this, Net1314080903127_help_activity.class); | ||
startActivity(help_intent); | ||
return true; | ||
case NEW: | ||
Intent new_course_intent = new Intent(); | ||
new_course_intent.setClass(Net1314080903127_course_activity.this, Net1314080903127_course_new_activity.class); | ||
startActivityForResult(new_course_intent, REQUEST_NEW); | ||
return true; | ||
case SET: | ||
Intent set_intent = new Intent(); | ||
set_intent.setClass(Net1314080903127_course_activity.this, Net1314080903127_course_set_activity.class); | ||
startActivityForResult(set_intent, REQUEST_SET); | ||
return true; | ||
case DELETE: | ||
mDbHelper.open(); | ||
mDbHelper.deleteCourse(getListView().getSelectedItemId()); | ||
mDbHelper.closeclose(); | ||
updateCourseView(); | ||
return true; | ||
} | ||
return super.onOptionsItemSelected(item); | ||
} | ||
|
||
//得到现在是第几周 | ||
private int get_interval_weeks(Date ds, Date de) | ||
{ | ||
long total = (de.getTime()-ds.getTime())/(24*60*60*1000); | ||
Log.e("total"," " +total); | ||
return ((int)(total/7.0) + 1); | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see android.app.Activity#onActivityResult(int, int, android.content.Intent) | ||
*/ | ||
@Override | ||
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | ||
// TODO Auto-generated method stub | ||
super.onActivityResult(requestCode, resultCode, data); | ||
|
||
//设置第一周的回复 | ||
if(requestCode == REQUEST_SET) | ||
{ | ||
if(resultCode == RESULT_OK) | ||
{ | ||
Bundle extras = data.getExtras(); | ||
if(extras != null) | ||
{ | ||
first_year = extras.getInt("year"); | ||
first_month = extras.getInt("month"); | ||
first_day = extras.getInt("day"); | ||
start_date = new Date(first_year,first_month,first_day); | ||
Log.e("now_month"," " +now_month); | ||
Log.e("now_day"," " +now_day); | ||
interval_weeks = get_interval_weeks(start_date, now_date); | ||
Toast.makeText(Net1314080903127_course_activity.this, | ||
"当前是第"+interval_weeks +"周", Toast.LENGTH_LONG).show(); | ||
setTitle("第"+interval_weeks +"周 "+"课程信息"); | ||
Log.e("weeks"," " +interval_weeks); | ||
} | ||
|
||
} | ||
} | ||
|
||
//添加新课程信息的回复 | ||
else if(requestCode == REQUEST_NEW) | ||
{ | ||
if(resultCode == RESULT_OK) | ||
{ | ||
updateCourseView(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
Oops, something went wrong.