Coder Social home page Coder Social logo

a's Introduction

NotePad

时间戳添加 修改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"
/>

修改时间戳类型 新建笔记在NotePadProvider中的insert方法,修改笔记在NoteEditor中的updateNote方法,我们需要修改这个方法中的时间戳格式 NotePadProvider中的insert方法

  Long now = Long.valueOf(System.currentTimeMillis());
  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 (values.containsKey(NotePad.Notes.COLUMN_NAME_CREATE_DATE) == false) {
values.put(NotePad.Notes.COLUMN_NAME_CREATE_DATE, dateFormat);
}

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);

NoteList的修改

@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
        NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE,//添加修改时间
};

加入修改时间后:

 String[] dataColumns = { NotePad.Notes.COLUMN_NAME_TITLE, NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE };
int[] viewIDs = { android.R.id.text1, R.id.text2 };

添加笔记查询图标 在函数onCreateOptionMenu(Menu menu)

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.list_options_menu, menu);

添加一个搜索图标

<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>

在添加menu_search图标的点击选择事件

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

添加note_search.xml和NoteSearch活动

<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>

为SearchView添加监听

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;
}

}

a's People

Contributors

chenjiaxiong124318 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.