Notepad扩展-时间戳添加和笔记查询
@author 068LaiChangping
1.时间戳添加
1.1. 修改noteslist_item.xml
noteslist_item.xml布局文件是笔记每个条目的布局,原应用中noteslist_item.xml代码如下:
<RelativeLayout android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="5dip"
android:singleLine="true"
/>
</RelativeLayout>
我们可以看出,原应用中,每个笔记条目中,只有一个TextView, 用来显示笔记的标题,而要想显示时间戳,必须再加入一个TextView, 修改后的noteslist_item.xml的代码为:
<RelativeLayout android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="5dip"
android:singleLine="true"
/>
<TextView
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dip"
android:singleLine="true"
android:gravity="center_vertical"
/>
</RelativeLayout>
其中,第二个的TextView用于时间戳的显示。
1.2. 修改时间戳类型
在新建笔记和修改笔记中,我们的时间并不是我们习惯的格式,需要我们修改一下时间戳的格式
新建笔记在NotePadProvider中的insert方法,修改笔记在NoteEditor中的updateNote方法,我们需要修改这个方法中的时间戳格式
NotePadProvider中的insert方法:
Long now = Long.valueOf(System.currentTimeMillis());
//修改 需要将毫秒数转换为时间的形式yy.MM.dd HH:mm:ss
Date date = new Date(now);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
String dateFormat = simpleDateFormat.format(date);//转换为yy.MM.dd HH:mm:ss形式的时间
// If the values map doesn't contain the creation date, sets the value to the current time.
if (values.containsKey(NotePad.Notes.COLUMN_NAME_CREATE_DATE) == false) {
values.put(NotePad.Notes.COLUMN_NAME_CREATE_DATE, dateFormat);
}
// If the values map doesn't contain the modification date, sets the value to the current
// time.
if (values.containsKey(NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE) == false) {
values.put(NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE, dateFormat);
}
NoteEditor中的updateNote方法:
long now = System.currentTimeMillis();
Date date = new Date(now);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
String dateFormat = simpleDateFormat.format(date);
values.put(NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE, dateFormat);
1.3. NoteList的修改
首先,如果查看到NotePadProvider中关于数据表的创建,我们可以发现,表中已经存在创建时间和修改时间的列
@Override
public void onCreate(SQLiteDatabase db) {
//创建数据表
db.execSQL("CREATE TABLE " + NotePad.Notes.TABLE_NAME + " ("
+ NotePad.Notes._ID + " INTEGER PRIMARY KEY,"
+ NotePad.Notes.COLUMN_NAME_TITLE + " TEXT,"
+ NotePad.Notes.COLUMN_NAME_NOTE + " TEXT,"
+ NotePad.Notes.COLUMN_NAME_CREATE_DATE + " INTEGER,"
+ NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE + " INTEGER"
+ ");");
}
而在NotesList中,列的投影PROJECTION为:
private static final String[] PROJECTION = new String[] {
NotePad.Notes._ID, // 0
NotePad.Notes.COLUMN_NAME_TITLE, // 1
};
只投影出了ID和每条笔记的标题,如果我们要加入时间戳,必须要将修改时间的列也投影出来.
所以我们将PROJECTION添加上修改时间:
private static final String[] PROJECTION = new String[] {
NotePad.Notes._ID, // 0
NotePad.Notes.COLUMN_NAME_TITLE, // 1
NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE,//添加修改时间
};
PROJECTION只是定义了需要被取出来的数据列,而之后用Cursor进行数据库查询,再之后用Adapter进行装填。
在看完源码之后,Cursor不用变化,我们需要将显示列dataColumns和他们的viewIDs加入修改时间这一属性
原来的代码:
String[] dataColumns = { NotePad.Notes.COLUMN_NAME_TITLE};
int[] viewIDs = { android.R.id.text1 };
加入修改时间后:
String[] dataColumns = { NotePad.Notes.COLUMN_NAME_TITLE, NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE }//加入修改时间;
int[] viewIDs = { android.R.id.text1, R.id.text2 }//加入修改时间;
再到Adapter进行装配和显示
1.4. 显示效果
2. 笔记查询
2.1. 添加笔记查询图标
在函数onCreateOptionsMenu(Menu menu)中,我们通过代码:
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.list_options_menu, menu);
所以,找到list_option_menu.xml,添加一个搜索图标
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is our one standard application action (creating a new note). -->
<item android:id="@+id/menu_add"
android:icon="@drawable/ic_menu_compose"
android:title="@string/menu_add"
android:alphabeticShortcut='a'
android:showAsAction="always" />
<!-- If there is currently data in the clipboard, this adds a PASTE menu item to the menu
so that the user can paste in the data.. -->
<item android:id="@+id/menu_paste"
android:icon="@drawable/ic_menu_compose"
android:title="@string/menu_paste"
android:alphabeticShortcut='p' />
<!--添加一个item-->
<item
android:id="@+id/menu_search"
android:icon="@android:drawable/ic_search_category_default"
android:showAsAction="always"
android:title="search">
</item>
</menu>
2.2.在添加menu_search图标的点击选择事件
在函数boolean onOptionsItemSelected(MenuItem item)中添加case:
case R.id.menu_search:
Intent intent = new Intent();
intent.setClass(this, NoteSearch.class);
this.startActivity(intent);
return true;
2.3. 添加note_search.xml和NoteSearch活动
新建一个NoteSearch的Activity,它的布局文件为note_search.xml
编辑note_search.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<SearchView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:iconifiedByDefault="false"
android:queryHint="搜索">
</SearchView>
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
而在NoteSearch.java中,我们需要取到note_search.xml的ListView和SearchView,并且为SearchView添加监听,使得SearchView中的文本发生变化后,就执行一次查询;为ListView添加监听,使得查询出来的每个item在被点击后,能够查看笔记的内容,代码如下:
public class NoteSearch extends Activity implements SearchView.OnQueryTextListener{
ListView listview;//
SQLiteDatabase sqLiteDatabase;
private static final String[] PROJECTION = new String[] {
NotePad.Notes._ID, // 0
NotePad.Notes.COLUMN_NAME_TITLE, // 1
NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE // 修改时间
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏显示
super.setContentView(R.layout.note_search);
Intent intent = getIntent();
// If there is no data associated with the Intent, sets the data to the default URI, which
// accesses a list of notes.
if (intent.getData() == null) {
intent.setData(NotePad.Notes.CONTENT_URI);
}
listview= (ListView) findViewById(R.id.list_view);//获取listview
sqLiteDatabase=new NotePadProvider.DatabaseHelper(this).getReadableDatabase();//对数据库进行操作
SearchView search= (SearchView) findViewById(R.id.search_view);//获取搜索视图
search.setOnQueryTextListener(NoteSearch.this);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//为每个item添加点击事件,点击可以查看笔记具体内容
Uri uri = ContentUris.withAppendedId(getIntent().getData(), l);
String action = getIntent().getAction();
if (Intent.ACTION_PICK.equals(action) || Intent.ACTION_GET_CONTENT.equals(action)) {
setResult(RESULT_OK, new Intent().setData(uri));
} else {
startActivity(new Intent(Intent.ACTION_EDIT, uri));
}
}
});
}
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
@Override
public boolean onQueryTextChange(String newText) {//实现模糊查询,通过标题或者内容进行查询
Cursor cursor=sqLiteDatabase.query(
NotePad.Notes.TABLE_NAME,
PROJECTION,
NotePad.Notes.COLUMN_NAME_TITLE+" like ? or "+NotePad.Notes.COLUMN_NAME_NOTE+" like ?",
new String[]{"%"+newText+"%","%"+newText+"%"},
null,
null,
NotePad.Notes.DEFAULT_SORT_ORDER);
int[] viewIDs = { R.id.text3,R.id.text4};
SimpleCursorAdapter adapter
= new SimpleCursorAdapter(
NoteSearch.this,
R.layout.searchlist_item,
cursor,
new String[]{NotePad.Notes.COLUMN_NAME_TITLE,NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE},
viewIDs
);
listview.setAdapter(adapter);
return true;
}
}
2.4. 查看结果