期中實驗:記事本實現時間戳、搜尋、正文縮略顯示
期中實驗:記事本實現時間戳、搜尋、正文縮略顯示
1.時間戳的實現
(1)在noteslist_item.xml程式碼新增顯示時間戳的元件。
<TextView
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="12dp"
android:paddingLeft="5dip"
android:paddingTop="@android:dimen/app_icon_size"
android:singleLine="true" />
(2)修改NotePadProvider中的insert方法。
//修改時間形式為yyyy.MM.dd HH:mm:ss
Date date = new Date(now);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateFormat = simpleDateFormat.format(date);
//轉換為yyyy-MM-dd HH:mm:ss
(3)修改NoteEditor中的updateNote方法。
long now = System.currentTimeMillis();
Date date = new Date(now);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateFormat = simpleDateFormat.format(date);
values.put(NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE, dateFormat);
(4)修改NotesList中的PROJECTION。
private static final String[] PROJECTION = new String[] {
NotePad.Notes._ID, // 0
NotePad.Notes.COLUMN_NAME_TITLE, // 1
NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE,//新增修改時間
};
(5)修改NoteList中的dataColums與viewIDs。
String[] dataColumns = { NotePad.Notes.COLUMN_NAME_TITLE, NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE,NotePad.Notes.COLUMN_NAME_NOTE} ;//加入修改時間
int[] viewIDs = { android.R.id.text1, R.id.text2};//加入修改時間
2.搜尋功能的實現。
(1)修改list_options_menu.xml增加搜尋元件。
<item
android:id="@+id/search"
android:icon="@android:drawable/ic_search_category_default"
android:title="Search"
android:actionViewClass="android.widget.SearchView"
android:showAsAction="always" />
(2)在NoteList中的onCreateOptionsMenu方法中新增SearchView。
//搜尋
MenuItem mSearch = menu.findItem(R.id.search);
SearchView mSearchView = (SearchView)mSearch.getActionView();
mSearchView.setQueryHint("搜尋");
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
Cursor cursor = managedQuery(
getIntent().getData(), // Use the default content URI for the provider.
PROJECTION, // Return the note ID and title for each note.
NotePad.Notes.COLUMN_NAME_TITLE+" like ? or "+NotePad.Notes.COLUMN_NAME_NOTE+" like ?", // No where clause, return all records.
new String[]{"%"+s+"%","%"+s+"%"}, // No where clause, therefore no where column values.
NotePad.Notes.DEFAULT_SORT_ORDER // Use the default sort order.
);
String[] dataColumns = { NotePad.Notes.COLUMN_NAME_TITLE, NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE,NotePad.Notes.COLUMN_NAME_NOTE} ;
int[] viewIDs = { android.R.id.text1, R.id.text2, R.id.text3 };//加入修改時間
SimpleCursorAdapter adapter
= new SimpleCursorAdapter(
NotesList.this, // The Context for the ListView
R.layout.noteslist_item, // Points to the XML for a list item
cursor, // The cursor to get items from
dataColumns,
viewIDs
);
setListAdapter(adapter);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
注意:
1.mSearchView.setOnQueryTextListener設定監聽器
2.onQueryTextSubmit當搜尋框的文字提交時呼叫此函式,由於我們的搜尋要求是實時的,所以不管它。
3.onQueryTextChange當搜尋框的文字改變時呼叫此函式,正好符合我們的要求。我們需要在這裡重新寫一個cursor和adapter。
4.cursor和adapter可以直接複製貼上onCreate方法中的cursor和adapter,然後更改cursor中的selection與selectionArgs。
3.正文縮略顯示功能的實現。
(1)修改noteslist_item.xml程式碼新增顯示正文縮略的元件。
<TextView
android:id="@+id/text3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="20dp"
android:ellipsize="end"
android:paddingLeft="5dip"
android:singleLine="true" />
(3)修改NotesList中的PROJECTION。
private static final String[] PROJECTION = new String[] {
NotePad.Notes._ID, // 0
NotePad.Notes.COLUMN_NAME_TITLE, // 1
NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE,//新增修改時間
NotePad.Notes.COLUMN_NAME_NOTE//新增筆記
};
(4)修改NoteList中的dataColums與viewIDs。
String[] dataColumns = { NotePad.Notes.COLUMN_NAME_TITLE, NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE,NotePad.Notes.COLUMN_NAME_NOTE} ;
//加入正文
int[] viewIDs = { android.R.id.text1, R.id.text2, R.id.text3 };//加入正文
4.成品展示。
相關文章
- SAP Hybris裡搜尋顯示結果的實現原理
- QT介面顯示實時時間QT
- qt中實現實時的顯示當前時刻的時間QT
- 測試開發【提測平臺】分享7-實現產品搜尋和優化時間顯示優化
- QT實現動態顯示系統時間QT
- QT學習 實時顯示時間QT
- Qt實時顯示系統時間QT
- 線上直播系統原始碼,實現搜尋後介面顯示商品列表效果原始碼
- 【iCore3 雙核心板】例程十:RTC實時時鐘實驗——顯示日期和時間
- javascript實時顯示時間程式碼例項JavaScript
- asp.net 中實時顯示本地時間ASP.NET
- TextView搜尋文字高亮顯示TextView
- 【iCore4 雙核心板_ARM】例程十:RTC實時時鐘實驗——顯示時間和日期
- QLabel顯示圖片 ,並實現縮放
- 【Java】實現記事本(完整版)Java
- Python實現簡易記事本Python
- 直播開發app,實時搜尋、搜尋引擎框APP
- 帶有星期的時間實時顯示程式碼
- QT4 用QLabel實時顯示時間QT
- 【iCore1S 雙核心板_ARM】例程十一:RTC實時時鐘實驗——顯示時間和日期
- PHP - 實現類似於百度的實時搜尋PHP
- AB實驗坑賊多?騰訊搜尋實驗有妙招
- 七牛 CDN 時間戳防盜鏈簽名實現時間戳
- 【搜尋引擎】 PostgreSQL 10 實時全文檢索和分詞、相似搜尋、模糊匹配實現類似Google搜尋自動提示SQL分詞Go
- PLSQL實現顯示當天是星期幾及當前時間SQL
- Laravel + Elasticsearch 實現中文搜尋LaravelElasticsearch
- Elasticsearch 實現簡單搜尋Elasticsearch
- Jquery + Bootstrap 實現搜尋框jQueryboot
- PHP實現搜尋附近的人PHP
- 實現延遲搜尋功能
- Meteor+MongoDB 實現簡單的即時搜尋MongoDB
- 前端實時搜尋框模擬前端
- Elasticsearch搜尋功能的實現(五)-- 實戰Elasticsearch
- laravel Es搜尋 檢索高亮顯示Laravel
- qt-實時顯示本地時間、以及QDateTime的使用QT
- iview input實現點選和enter同時搜尋技巧View
- 搜尋趨勢:微軟必應新版整合AI和實時搜尋微軟AI
- 如何用Redis實現搜尋介面Redis