I'm trying to implement search function for items of listview by using SearchView.
This is what I did.
Adding the Search Widget to action bar action item
Defining the searchable configuration in the xml
Creating the activity to handle search query and display the results
Defining the default searchable activity and SEARCH intent filter in AndroidManifest.xml file
I want to do like this. http://ift.tt/1lhjutN
I can set SearchView in ToolBar, but the result of search(from SQLite) cannot be displayed and I cannot find out my problem…
This is my code
----MainActivity---
@Override
public boolean onCreateOptionsMenu(Menu menu){ MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.menu_main,menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search_view).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
---menu_main.xml---
<item
android:id="@+id/search_view"
android:title="@string/search_menu_search_text"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" />
------Searchable.xml-------
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://ift.tt/nIICcg"
android:hint="@string/search_hint"
android:label="@string/app_name" />
----Manifest----------
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main2"
android:theme="@style/AppTheme">
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SearchResultsActivity"
android:parentActivityName=".MainActivity"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
------SearchResultsActivity----
public class SearchResultsActivity extends AppCompatActivity{
private String query;
private ListView listView;
private Toolbar mToolbar;
private ExampleDBHelper dbHelper;
private String FOREIGN_KEY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_results);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
listView = (ListView) findViewById(R.id.listQuery);
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
dbHelper = new ExampleDBHelper(this);
listView = (ListView) findViewById(R.id.listQuery);
Cursor cursor = dbHelper.getAllProjectForSearch(query);
String[] from = new String[]{
ExampleDBHelper.KEY_ID,
ExampleDBHelper.KEY_NAME
};
int[] to = new int[]{
R.id.projectID
R.id.projectName
};
SimpleCursorAdapter cAdapter = new SimpleCursorAdapter(this, R.layout.person_info, cursor, from, to, 0);
listView.setAdapter(cAdapter);
}
}
---------activity_search_results.xml---
<RelativeLayout
xmlns:android="http://ift.tt/nIICcg"
xmlns:app="http://ift.tt/GEGVYd"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/listQuery"
android:layout_below="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
-------ExampleSQLiteOpenHelper--------------
String CREATE_TABLE_PROJECT = "CREATE TABLE "
+ TABLE_PROJECT + "( "
+ KEY_ID + " INTEGER PRIMARY KEY, "
+ KEY_NAME + " TEXT, "
+ KEY_INFO + " TEXT, "
+ KEY_RESULT + " TEXT " + ") ";
public Cursor getAllProjectForSearch(String project_name) {
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_PROJECT
+ " WHERE " + KEY_NAME + " = ' " + project_name + " ' " ;
Cursor c = db.rawQuery(selectQuery, null);
return c;
I read tons of SO Q&A already, but I cannot find out my problem & solution… Thanks in advance....
Aucun commentaire:
Enregistrer un commentaire