码迷,mamicode.com
首页 > 移动开发 > 详细

Android学习笔记_79_ Android 使用 搜索框

时间:2014-06-06 17:32:50      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:des   android   c   style   class   blog   

1、在资源文件夹下创建xml文件夹,并创建一个searchable.xml:

bubuko.com,布布扣
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/sms_search"
    android:hint="@string/sms_search" 
    android:searchSuggestAuthority="cn.itcast.smsmanager.MySuggestionProvider"
    android:searchSuggestSelection=" ?" >
</searchable>
bubuko.com,布布扣

2、配置文件

bubuko.com,布布扣
<activity
            android:name=".SearchableActivity"
            android:theme="@android:style/Theme.NoTitleBar" >
            <!--下面这两个节点必须要有 -->
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>
        <!-- 指定我们要激活的是哪个SearchableActivity -->
        <meta-data
            android:name="android.app.default_searchable"
            android:value=".SearchableActivity" />

bubuko.com,布布扣

3、创建provider

bubuko.com,布布扣
public class MySuggestionProvider extends SearchRecentSuggestionsProvider {

    public final static String AUTHORITY = "cn.itcast.smsmanager.MySuggestionProvider";
    public final static int MODE = DATABASE_MODE_QUERIES;
    
    private final static String[] sms_projection = new String[]{Sms._ID,Sms.ADDRESS,Sms.BODY};
    
    private final static String[] columnNames = new String[]{BaseColumns._ID,
        SearchManager.SUGGEST_COLUMN_TEXT_1,
        SearchManager.SUGGEST_COLUMN_TEXT_2,
        SearchManager.SUGGEST_COLUMN_QUERY};

    public MySuggestionProvider() {
        setupSuggestions(AUTHORITY, MODE);
    }

    
    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        // TODO Auto-generated method stub
        Log.i("i", "query  ...");
        
        if(selectionArgs != null){
            String query = selectionArgs[0];
            
            if(TextUtils.isEmpty(query)){
                return null;
            }
            
            Uri uri1 = Sms.CONTENT_URI;
            String where = Sms.BODY + " like ‘%" + query + "%‘";
            Cursor cursor = getContext().getContentResolver().query(uri1, sms_projection, where, null, Sms.DATE + " desc ");
            return changeCursor(cursor);
        }
        return null;
    }
    
    
    private Cursor changeCursor(Cursor cursor){
        MatrixCursor result = new MatrixCursor(columnNames);
        if(cursor != null){
            while(cursor.moveToNext()){
                Object[] columnValues = new Object[]{cursor.getString(cursor.getColumnIndex(Sms._ID)),
                        cursor.getString(cursor.getColumnIndex(Sms.ADDRESS)),
                        cursor.getString(cursor.getColumnIndex(Sms.BODY)),
                        cursor.getString(cursor.getColumnIndex(Sms.BODY))};
                result.addRow(columnValues);
            }
        }
        return result;
    }
}
bubuko.com,布布扣

 4、存放搜索结果的activity:

bubuko.com,布布扣
public class SearchableActivity extends ListActivity {

    
    private ListView mListView;
    
    private QueryHandler mQueryHandler;
    
    private SearchAdapter mAdapter;
    

    
    
    protected void onCreate(android.os.Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        
        mListView = getListView();
        

        
        mAdapter = new SearchAdapter(this, null);
        
        mListView.setAdapter(mAdapter);
        
        mListView.setBackgroundColor(Color.WHITE);
        
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
          String query = intent.getStringExtra(SearchManager.QUERY);
          doSearch(query);
          
        }
        
        
        mListView.setOnItemClickListener(new MyOnItemClickListener());
    }
}
bubuko.com,布布扣

 

 

http://www.cnblogs.com/zhengbeibei/archive/2013/01/17/2865610.html

 

 

 

 

 

 

 

Android学习笔记_79_ Android 使用 搜索框,布布扣,bubuko.com

Android学习笔记_79_ Android 使用 搜索框

标签:des   android   c   style   class   blog   

原文地址:http://www.cnblogs.com/lbangel/p/3766371.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!