ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-01-01 20:00:04  阅读:230  来源: 互联网

标签:NAME COLUMN Notes NotePad 期中 new 缩略 记事本 String


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

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

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

标签:NAME,COLUMN,Notes,NotePad,期中,new,缩略,记事本,String
来源: https://blog.csdn.net/fjnu_se/article/details/112064697

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有