如何将SearchView添加到我现有的“联系人”项目中?

时间:2018-08-10 20:23:17

标签: android add searchview

我有一个以前做过的项目。该应用程序的工作方式与所有电话中的“联系人”应用程序完全相同。我将信息存储在一个数据库中,并且具有列表视图。现在,我想向此应用程序添加一个searchView。

我想通过键入每个字符在数据库的所有字段(名称,电话号码,电子邮件,...)中进行搜索,并在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

感谢您的帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

您要搜索什么?尝试添加更多信息,共同点:

在activity_menu.xml中添加

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

然后添加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>

在activity.java中,声明

private SearchView mSearchView;

在onCreate中添加

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

在onCreateOptionsMenu中

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

然后添加

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

希望,我没有错过任何重要的事情。