期中实验:记事本实现时间戳、搜索、正文缩略显示

期中实验:记事本实现时间戳、搜索、正文缩略显示

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.成品展示。
时间戳与正文缩略显示

搜索功能展示
参考博客:时间戳参考
作者:汪振龙
原文链接:添加链接描述

Guess you like

Origin blog.csdn.net/fjnu_se/article/details/112064697