首页 > 代码库 > Android学习笔记_79_ Android 使用 搜索框

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

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

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

2、配置文件

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

3、创建provider

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

 4、存放搜索结果的activity:

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

 

 

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