我正在开发一个应用程序,搜索视图有问题,我无法从数据库中看到结果,这是代码
MainActivity.Java
公共类MainActivity扩展ActionBarActivity实现OnClickListener{
ActionBar actionBar;
Button a;
Button b;
Button c;
Button d;
Button e;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar = getSupportActionBar();
actionBar.setTitle("Trial");
initialiseOnClick();
a.setOnClickListener(this);
b.setOnClickListener(this);
c.setOnClickListener(this);
d.setOnClickListener(this);
e.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// Configure the search info and add any event listeners
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true); // Do not iconify the widget; expand it by default
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
onSearchRequested();
return true;
default:
return false;
}
}
private void initialiseOnClick() {
// TODO Auto-generated method stub
a = (Button) findViewById (R.id.button_a);
b = (Button) findViewById (R.id.button_b);
c = (Button) findViewById (R.id.button_c);
d = (Button) findViewById (R.id.button_d);
e = (Button) findViewById (R.id.button_e);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.button_a:
Intent a = new Intent(MainActivity.this, EuropeActivity.class);
startActivity(a);
break;
case R.id.button_b:
Intent b = new Intent(MainActivity.this, AmericasActivity.class);
startActivity(b);
break;
case R.id.button_c:
Intent c = new Intent(MainActivity.this, AsiaActivity.class);
startActivity(c);
break;
case R.id.button_d:
Intent d = new Intent(MainActivity.this, OceaniaActivity.class);
startActivity(d);
break;
case R.id.button_e:
Intent e = new Intent(MainActivity.this, AfricaActivity.class);
startActivity(e);
break;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="#000000"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="vertical" >
<Button
android:id="@+id/button_a"
style="@style/ButtonMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/blue_button"
android:text="@string/europe" />
<Button
android:id="@+id/button_b"
style="@style/ButtonMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/red_button"
android:text="@string/americas" />
<Button
android:id="@+id/button_c"
style="@style/ButtonMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/yellow_button"
android:text="@string/asia" />
<Button
android:id="@+id/button_d"
style="@style/ButtonMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/green_button"
android:text="@string/oceania" />
<Button
android:id="@+id/button_e"
style="@style/ButtonMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/black_button"
android:text="@string/africa" />
</LinearLayout>
</ScrollView>
SearchableActivity.Java
public class SearchableActivity extends ListActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
}
DatabaseTable db = new DatabaseTable(this);
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Cursor c = db.getWordMatches(query, null);
//process Cursor and display results
}
}
}
search.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
数据库表Java
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 Cursor getWordMatches(String query, String[] columns) {
String selection = COL_WORD + " MATCH ?";
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;
}
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);
loadDictionary();
}
@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);
}
private void loadDictionary() {
new Thread(new Runnable() {
public void run() {
try {
loadWords();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}).start();
}
private void loadWords() throws IOException {
final Resources resources = mHelperContext.getResources();
InputStream inputStream = resources.openRawResource(R.raw.definitions);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String line;
while ((line = reader.readLine()) != null) {
String[] strings = TextUtils.split(line, "-");
if (strings.length < 2) continue;
long id = addWord(strings[0].trim(), strings[1].trim());
if (id < 0) {
Log.e(TAG, "unable to add word: " + strings[0].trim());
}
}
} finally {
reader.close();
}
}
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);
}
}
}
definitions.txt
abbey - n. a monastery ruled by an abbot
abide - v. dwell; inhabit or live in
abound - v. be abundant or plentiful; exist in large quantities
absence - n. the state of being absent
absorb - v. assimilate or take in
abstinence - n. practice of refraining from indulging an appetite especially alcohol
absurd - j. inconsistent with reason or logic or common sense
abundant - j. present in great quantity
abusive - j. characterized by physical or psychological
academic - j. associated with school or learning
academy - n. a school for special training
accept - v. consider or hold as true
access - v. reach or gain access to
accessible - j. easily obtained
acclaim - n. enthusiastic approval
accommodate - v. provide with something desired or needed
Manifest.xml
<activity android:name=".SearchableActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
<activity
android:name="com.example.trial.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.default_searchable" android:value=".SearchableActivity" />
</activity>
问题是我可以看到操作栏,我可以在搜索对话框中写,但我看不到结果。我知道出了什么问题,但我不知道在哪里或什么!
searchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String key) {
//After you press Search/Enter control comes here with entered text in "key"
return true;
}
@Override
public boolean onQueryTextChange(String key) {
//Each time you enter/modify a charcter, control comes here with entered text in "key"
return true;
}
});