修改Android搜索ListView



我是android应用程序开发的新手,目前正试图找出如何将搜索添加到应用程序中。我做了一些搜索和教程,这对我来说是最简单的理解在这里。

应用程序正常运行和搜索。唯一的问题是,该教程是搜索ListView。活动,我的应用程序的搜索是托管在其他细节(itemview, textview),需要在屏幕上,所以一个ListView不会为我想做的工作。如何修改此示例,以便在用户输入搜索框时提供建议的下拉列表?理想情况下,我希望搜索如图所示。

ItemSearch.java

import android.app.SearchManager;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.List;
public class ItemSearch extends AppCompatActivity {
    ArrayAdapter<String> adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_search_activity);
        final String[] months = {"January", "February", "March", "April",
                "May", "June", "July", "August", "September", "October",
                "November", "December"};
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, months);
        final ListView listview = (ListView) findViewById(R.id.listView1);
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                String text = (String) listview.getItemAtPosition(position);
                Toast.makeText(getApplicationContext(), text,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_search, menu);

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }
            @Override
            public boolean onQueryTextChange(String newText) {
                adapter.getFilter().filter(newText);
                return false;
            }
        });
        return true;
    }
}

item_search_acitivity.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/item_view_activity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.me.ItemSearch">

    <ImageView
        />
    <ImageView
        />
    <ImageView
       />
    <TextView
        />
    <TextView
         />
    <TextView
        />
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>
    </ScrollView>

res/menu/menu_search下的menu .xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/search"
            android:title="@string/hint_search"
            android:icon="@android:drawable/ic_menu_search"
            app:showAsAction="collapseActionView|ifRoom"
            app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

res/xml/searchable

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="@string/search_hint"
    android:searchSuggestIntentAction="android.intent.action.VIEW">
</searchable>

在AndroidManifest.xml中我添加了

<activity android:name=".ItemSearch" android:launchMode="singleTop">
            <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable"/>
        </activity>

对于这种类型的下拉建议功能,您可以使用AutoCompleteTextView

用法示例如下:

在你的布局文件中:

<AutoCompleteTextView
    android:id="@+id/auto_complete_tv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/key"/>

资源文件(如arrays.xmlstrings.xml):

<string-array name="suggestions">
    <item>January</item>
    <item>February</item>
    <item>March</item>
    ...
</string-array>

在你的片段或活动中:

AutoCompleteTextView autoCompleteTv;
...
AutoCompleteTextView autoCompleteTv = (AutoCompleteTextView) view.findViewById(R.id.auto_complete_tv);
autoCompleteTv.setAdapter(ArrayAdapter.createFromResource(view.getContext(), R.array.suggestions, android.R.layout.simple_dropdown_item_1line));

相关内容

  • 没有找到相关文章

最新更新