Jiangge Zhang avatar Jiangge Zhang committed 28054de

保存笔记时处理笔记本

Comments (0)

Files changed (5)

Project/res/layout/saving_note.xml

     android:layout_height="match_parent"
     android:orientation="vertical" >
 
-    <TextView
-        android:id="@+id/chooseNotebookLabel"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:text="@string/chooseNotebookLabel" />
-
     <Spinner
         android:id="@+id/savingChooseNotebookSpinner"
         android:layout_width="match_parent"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:hint="@string/savingNoteAuthorHint" />
-
-    <EditText
-        android:id="@+id/savingNoteTags"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:hint="@string/savingNoteTabsHint" />
     <!--
     TODO: 同步到云端
 	<CheckBox android:text="@string/savingSyncLabel" android:id="@+id/savingSync"
 		android:layout_width="wrap_content" android:layout_height="wrap_content" />
     -->
-
     <Button
         android:id="@+id/savingQuickSaveButton"
         android:layout_width="fill_parent"

Project/res/values/saving_note.xml

 <?xml version="1.0" encoding="utf-8"?>
 <resources>
-    <string name="chooseNotebookLabel">选择笔记本</string>
     <string name="savingNoteTitleHint">笔记标题</string>
     <string name="savingNoteTabsHint">笔记标签(用逗号分隔)</string>
     <string name="savingNoteAuthorHint">笔记作者</string>

Project/src/com/noodle/note/activity/SavingNoteActivity.java

 package com.noodle.note.activity;
 
+import java.util.List;
+
 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.view.View;
 import android.view.View.OnClickListener;
+import android.widget.ArrayAdapter;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.Spinner;
 import android.widget.Toast;
 
 import com.noodle.note.R;
+import com.noodle.note.gateway.NotebookGateway;
 import com.noodle.note.model.Note;
+import com.noodle.note.model.Notebook;
 import com.noodle.note.persistent.unitofwork.Session;
 
 /**
  */
 public class SavingNoteActivity extends Activity {
 	/**
+	 * 工作单元
+	 */
+	private Session session = Session.getInstance(this);
+	
+	/**
+	 * 笔记本的表数据入口
+	 */
+	NotebookGateway notebookQuery = (NotebookGateway) session.query(Notebook.class);
+	
+	/**
 	 * Note 笔记模型
 	 */
 	private Note note;
 	 */
 	private EditNoteWorkMode workMode;
 
+	/**
+	 * 数组适配器
+	 */
+	private ArrayAdapter<Notebook> notebookAdapter;
+
 	// 视图元素
 
 	/**
 	 * 笔记本下拉框
 	 */
-	protected Spinner notebookListView;
+	protected Spinner notebookSpinner;
 
 	/**
 	 * 标题文本框
 			String title = "快速保存的笔记";
 			String author = "佚名";
 			String[] tags = {};
+			Notebook notebook = notebookQuery.findDefault();
 
-			saveNote(title, author, tags);
+			saveNote(title, author, tags, notebook);
 		}
 	};
 
 			String author = authorTextbox.getText().toString();
 			String tagText = tagTextbox.getText().toString().trim();
 			String[] tags = tagText.split(TAG_SPLIT_PATTERN);
-			
-			saveNote(title, author, tags);
+			Notebook notebook = (Notebook) notebookSpinner.getSelectedItem();
+
+			saveNote(title, author, tags, notebook);
 		}
 	};
 
 		super.onCreate(savedInstanceState);
 		setContentView(R.layout.saving_note);
 		findViews();
+		initViews();
 		setListeners();
 		findModel();
 	}
 	 * 定位视图元素
 	 */
 	protected void findViews() {
-		notebookListView = (Spinner) findViewById(R.id.savingChooseNotebookSpinner);
+		notebookSpinner = (Spinner) findViewById(R.id.savingChooseNotebookSpinner);
 		titleTextbox = (EditText) findViewById(R.id.savingNoteTitle);
 		tagTextbox = (EditText) findViewById(R.id.savingNoteTags);
 		authorTextbox = (EditText) findViewById(R.id.savingNoteAuthor);
 		commonSaveButton = (Button) findViewById(R.id.savingCommonSaveButton);
 	}
 
+	protected void initViews() {
+		// 查询所有笔记本
+		List<Notebook> notebooks = notebookQuery.findAll();
+		
+		// 发布到笔记本视图
+		int itemStyle = android.R.layout.simple_spinner_dropdown_item;
+		notebookAdapter = new ArrayAdapter<Notebook>(this, itemStyle, notebooks);
+		notebookSpinner.setAdapter(notebookAdapter);
+	}
+
 	/**
 	 * 给视图元素安装监听器
 	 */
 	/**
 	 * 保存笔记
 	 */
-	private void saveNote(String title, String author, String[] tags) {
+	private void saveNote(String title, String author, String[] tags, Notebook notebook) {
 		// 验证输入
 		if (title == null || title.trim().length() == 0) {
 			Toast.makeText(this, "请输入标题", Toast.LENGTH_SHORT).show();
 		} else if (author == null || author.trim().length() == 0) {
 			Toast.makeText(this, "请输入作者", Toast.LENGTH_SHORT).show();
 			return;
+		} else if (notebook == null) {
+			Toast.makeText(this, "请选择笔记本", Toast.LENGTH_SHORT).show();
+			return;
 		}
-		// TODO: 处理标签 
+		// TODO: 处理标签
 
 		note.setTitle(title.trim());
 		note.setAuthor(author.trim());
+		note.setNotebook(notebook);
 
 		// 同步到数据库
 		Session session = Session.getInstance(this);
 
 		// 提示
 		Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
-		
+
 		// 跳转
 		Intent intent = new Intent(this, NoteReadActivity.class);
 		intent.putExtra("id", note.getId());
 		startActivity(intent);
 		this.finish();
 	}
-	
+
 }

Project/src/com/noodle/note/gateway/NotebookGateway.java

  * 
  */
 public class NotebookGateway extends TableDataGateway<Notebook> {
+	public final static int DEFAULT_NOTEBOOK_ID = -1;
+	
 	/**
 	 * 数据库元信息
 	 */
 	}
 
 	/**
+	 * 获取默认笔记本
+	 * 
+	 * @return 默认笔记本
+	 */
+	public Notebook findDefault() {
+		return findById(DEFAULT_NOTEBOOK_ID);
+	}
+	
+	/**
 	 * 通过id值获取Notebook对象
 	 * 
 	 * @return 根据主键获取的Notebook对象

Project/src/com/noodle/note/model/Notebook.java

 
 		return attrs;
 	}
+
+	@Override
+	public String toString() {
+		return getName();
+	}
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.