Android搜索示例不起作用

时间:2013-09-12 18:10:35

标签: android search

我正在关注developers.android搜索示例,使searchableActivity搜索数据库以查询并在活动中显示:

http://developer.android.com/training/search/setup.html http://developer.android.com/training/search/search.html

但它不起作用......

这是我的整个代码:

数据库表的类:

public class DatabaseTable {
private static final String TAG = "DictionaryDatabase";

//The columns we'll include in the dictionary table
public static final String COL_WORD = "WORD";
public static final String COL_DEFINITION = "DEFINITION";

private static final String DATABASE_NAME = "DICTIONARY";
private static final String FTS_VIRTUAL_TABLE = "FTS";
private static final int DATABASE_VERSION = 1;
private final DatabaseOpenHelper mDatabaseOpenHelper;

public DatabaseTable(Context context) {
    mDatabaseOpenHelper = new DatabaseOpenHelper(context);
}
private static class DatabaseOpenHelper extends SQLiteOpenHelper {

    private final Context mHelperContext;
    private SQLiteDatabase mDatabase;

    private static final String FTS_TABLE_CREATE =
                "CREATE VIRTUAL TABLE " + FTS_VIRTUAL_TABLE +
                " USING fts3 (" +
                COL_WORD + ", " +
                COL_DEFINITION + ")";

    DatabaseOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        mHelperContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        mDatabase = db;
        mDatabase.execSQL(FTS_TABLE_CREATE);
        loadDirectory();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + FTS_VIRTUAL_TABLE);
        onCreate(db);
    }

    public long addWord(String word, String definition){
        ContentValues initialValues = new ContentValues();
        initialValues.put(COL_WORD, word);
        initialValues.put(COL_DEFINITION, definition);
        return mDatabase.insert(FTS_VIRTUAL_TABLE, null, initialValues);

    }

    private void loadDirectory(){
        addWord("bravo","svaka cast");
        addWord("genije","da da");
        addWord("bravo","tako je bre, samo tako nastavi");
    }
}

public Cursor getWordMatches(String query, String[] columns){
    String selection = COL_WORD + " MATCHE ?";
    String[] selectionArgs = new String[] {query + "*"};

    return query(selection, selectionArgs, columns);
}

private Cursor query(String selection, String[] selectionArgs, String[] columns){
    SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
    builder.setTables(FTS_VIRTUAL_TABLE);

    Cursor cursor = builder.query(mDatabaseOpenHelper.getReadableDatabase(), columns, 
            selection, selectionArgs, null, null, null);

    if(cursor==null)
        return null;
    else if (!cursor.moveToFirst()){
        cursor.close();
        return null;
    }       
    return cursor;
}
}

MainActivity:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    SearchManager searchManager =
               (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));
        searchView.setSubmitButtonEnabled(true);
    return true;
}

  }

SearchResoultActivity:

public class SearchResoultActivity extends Activity {

DatabaseTable dTable;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);
    dTable = new DatabaseTable(this);
    handleIntent(this.getIntent());

}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    handleIntent(this.getIntent());
}

private void handleIntent(Intent intent){

    if(intent.ACTION_SEARCH.equals(intent.getAction())){
        String query = intent.getStringExtra(SearchManager.QUERY);
        Cursor c = dTable.getWordMatches(query, null);
        ((TextView)findViewById(R.id.txtTekst))
        .setText(c.getString(0) + " = " + c.getString(1));
    }
}

}

清单:     

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name="com.example.search.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
        android:resource="@xml/searchable" />
    </activity>
    <activity android:name = "com.example.search.SearchResoultActivity">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
    </activity>
</application>

</manifest>

search.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/txtTekst"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

searchable.xml

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="@string/search_hint" />

我已经把我的整个代码都用了...有人可以告诉我我做错了什么......我查了例子,一切都还好......

问题是,当我按下搜索按钮时,它会执行任何操作

我正在测试4.1.2版本的app,如果这意味着什么

0 个答案:

没有答案