How to add a SearchView to my existing “contacts” project?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to add a SearchView to my existing “contacts” project?



I have a project that I made before. The application is working exactly like the "contacts" application in all phones. I'm storing the information in one DataBase and I have list view. Now, I want to add one searchView to this app.



that i want to Search in all the fields(name,phone number,email,...) of DataBase by Typing each character and show the results in ListView.



MainActivity


protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

SQLiteDatabase sqLiteDatabase = getBaseContext().openOrCreateDatabase("ContactsDB.db", MODE_PRIVATE, null);

lv = (ListView) findViewById(R.id.contactListView2);
buttonAdd = (Button) findViewById(R.id.buttonAdd);

String tempSQL = "CREATE TABLE IF NOT EXISTS contactsList(_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,firstName TEXT,lastName TEXT,phone TEXT,email TEXT,address TEXT,note TEXT);";
sqLiteDatabase.execSQL(tempSQL);
Log.d(TAG, "onCreate: Table created");

db = openOrCreateDatabase("ContactsDB.db", MODE_PRIVATE, null);

//show name in listView

Cursor cursor2 = db.rawQuery("SELECT firstName FROM contactsList;", null);
ArrayList ar = new ArrayList();//new

while (cursor2.moveToNext())
ar.add(cursor2.getString(0));


ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, ar);
lv.setAdapter(arrayAdapter);
Log.d(TAG, "onCreate: showlist : list showing in listview now.");

//setting for showing the edit layout

lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
Cursor cr = db.rawQuery("SELECT * FROM contactsList WHERE firstName='"+lv.getItemAtPosition(i).toString()+"'",null);
StringBuffer sb = new StringBuffer();
while(cr.moveToNext())
sb.append(cr.getString(0)+"n");

Intent showAddLayout = new Intent(MainActivity.this, editContact.class);
showAddLayout.putExtra("name",sb.toString());
startActivity(showAddLayout);

);



//button click listener to show other LayOut or page

buttonAdd.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)

Intent showAddLayout = new Intent(MainActivity.this, addNewContact.class);
startActivity(showAddLayout);



);

//end of onCreate



and this is the picture of my simple app



Any help is appreciated. Thanks!




1 Answer
1



What do you want to search? Try to add more information, in common:



in activity_menu.xml add


<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_search_category_default"
android:title="@string/menu_search"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />



then add action.items.xml


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_search"
android:icon="@android:drawable/ic_search_category_default"
android:title="@string/menu_search"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />
</menu>



in activity.java, declare


private SearchView mSearchView;



in onCreate,add


Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction()))
mSearchString = intent.getStringExtra(SearchManager.QUERY);
//System.out.println(query);



in onCreateOptionsMenu


getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem searchMenuItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
setupSearchResult(searchMenuItem);



And, add


private void setupSearchResult(MenuItem searchItem)
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
mSearchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));



Hope, I didn't miss nothing important.





thank you ,i'm working on your suggestions , i added more informations
– KhalilPan
Aug 13 at 12:10






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard