java在安卓x上设置了建议。appcompat。小装置。搜索框
我正在制作一个安卓应用程序,其中我使用了一个安卓x.appcompat.widget.SearchView
我有一个arraylist,其中包含不同的值,现在我想从该数组列表中设置安卓x.appcompat.widget.SearchView
的建议。我试了很多,但效果不好。
我还在onQueryTextChange
中toast来测试它是否工作,但它不工作,因为当我更改文本时,我没有得到toasr
public static ArrayList<String> phoneNo=new ArrayList<>(); // This one contains names
public static ArrayList<String> cname=new ArrayList<>(); // This one contains phone numbers
private SimpleCursorAdapter mAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root= inflater.inflate(R.layout.fragment_messages, container, false);
final String[] from = new String[] {"contacts"};
final int[] to = new int[] {安卓.R.id.text1};
mAdapter = new SimpleCursorAdapter(home_Activity,
安卓.R.layout.simple_list_item_1,
null,
from,
to,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
searchView = (SearchView) view.findViewById(R.id.sView);
searchView.setSuggestionsAdapter(mAdapter);
searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
@Override
public boolean onSuggestionClick(int position) {
Cursor cursor = (Cursor) mAdapter.getItem(position);
String txt = cursor.getString(cursor.getColumnIndex("contacts"));
searchView.setQuery(txt, true);
return true;
}
@Override
public boolean onSuggestionSelect(int position) {
// Your code here
return true;
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
populateAdapter(s);
Toast.makeText(home_Activity, "ok", Toast.LENGTH_SHORT).show();
return true;
}
});
}
共 (0) 个答案