Android简单笔记本解析(期中作业)

1.成品展示

1.1初始界面展示

1.2添加功能展示

1.3查询功能展示

2.搜索功能(根据标题查询)

搜索图标(list_options_menu.xml)

搜索的声明(AndroidManifest.xml)

添加搜索(NoteList–onOptionsItemSelected方法)

case R.id.menu_search:       Intent intent = new Intent();       intent.setClass(NotesList.this,NoteSearch.class);       NotesList.this.startActivity(intent);       return true;

布局搜索页面(note_search_list.xml)
SearchView和ListView分别用来实现搜索功能和显示搜索出来的条目

<?xml version="1.0" encoding="utf-8"?>            

查询按钮的监听器、查询列表的监听器以及查询界面的初始化(NoteSearch)

public class NoteSearch extends ListActivity implementsSearchView.OnQueryTextListener {    private static final String[] PROJECTION = new String[] {            NotePad.Notes._ID, // 0            NotePad.Notes.COLUMN_NAME_TITLE, // 1            //扩展 显示时间 颜色            NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE // 2            //NotePad.Notes.COLUMN_NAME_BACK_COLOR    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.note_search_list);        Intent intent = getIntent();        if (intent.getData()== null) {            intent.setData(NotePad.Notes.CONTENT_URI);        }        SearchView searchview =(SearchView)findViewById(R.id.search_view);        //为查询文本框注册监听器        searchview.setOnQueryTextListener(NoteSearch.this);    }    @Override    public boolean onQueryTextSubmit(String query) {        return false;    }    @Override    public boolean onQueryTextChange(String newText) {        String selection = NotePad.Notes.COLUMN_NAME_TITLE + " Like ? ";        String[] selectionArgs = { "%"+newText+"%" };        Cursor cursor = managedQuery(               getIntent().getData(),                                         PROJECTION,                                    selection,                                        selectionArgs,                                    NotePad.Notes.DEFAULT_SORT_ORDER         );        String[] dataColumns = {NotePad.Notes.COLUMN_NAME_TITLE,  NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE };        int[]viewIDs = { android.R.id.text1, R.id.text1_time };        SimpleCursorAdapter adapter = new SimpleCursorAdapter(                this,                R.layout.noteslist_item,                cursor,                dataColumns,                viewIDs        );        setListAdapter(adapter);        return true;    }    @Override    protected void onListItemClick(ListView l, View v, int position, longid) {               Uri uri = ContentUris.withAppendedId(getIntent().getData(),id);              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));        }    }}

3.时间戳的实现

在文本下方显示时间
文本标题(noteslist_item.xml)

我们要在标题下方添加时间,那么可以再写一个时间的TextView,把标题和时间的TextView放入垂直线性布局内就好了

                

在数据库中存放时间(NotePadProvider)

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"            + ");");}

通过Cursor从数据库中读取出

Cursor cursor = managedQuery(            getIntent().getData(),            // Use the default content URI for the provider.            PROJECTION,                       // Return the note ID and title for each note.            null,                             // No where clause, return all records.            null,                             // No where clause, therefore no where column values.            NotePad.Notes.DEFAULT_SORT_ORDER  // Use the default sort order.        );

通过SimpleCursorAdapter装填

String[] dataColumns = { NotePad.Notes.COLUMN_NAME_TITLE } ;int[] viewIDs = { android.R.id.text1 };SimpleCursorAdapter adapter    = new SimpleCursorAdapter(            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    );// Sets the ListView's adapter to be the cursor adapter that was just created.setListAdapter(adapter);

写到这里差不多就完成了时间戳和查询功能的实现。
作者姓名:游嘉豪
原文链接

更多相关文章

  1. Android(安卓)闹钟设置最新版
  2. Android(安卓)NotePad的简单实现
  3. Android中Toast之间快速切换(连续弹吐司)
  4. Android中使用Timer配合postInvalidate()刷新View
  5. 安卓应用-去掉标题栏
  6. android开发文档之onTouch()
  7. Android可折叠标题栏的使用
  8. LocationManager定位
  9. Android学习之SeekBar控件

随机推荐

  1. Android(安卓)chart、图表、条形图、饼图
  2. android 系统System UI Tuner大概讲解
  3. 飞凌干货丨OK-xx18 Android实时时钟框架
  4. 动态更改屏幕方向的简单例子(LANDSCAPE与
  5. Ubuntu 12.04 编译Android(安卓)4.0.3 的
  6. android:崩溃重启处理
  7. Android(安卓)OpenGLES2.0(八)——纹理贴图
  8. 总结与反思
  9. Android(安卓)中的 Service 全面总结详解
  10. 深入浅出 Android(安卓)Service (5)