我制作了一个简单的应用程序,其中我使用了searchview。 searchview代码已从youtube链接复制,我还没有完全理解它。有人可以解释一下所有功能的工作原理吗?
listview的id是ListView1,searchview的id是searchview1。
lv = (ListView) findViewById(R.id.listview1);
sv = (SearchView) findViewById(R.id.searchview1);
adapter = new ArrayAdapter<String>(this, R.android.layout.simple_list_item_1, *array name*);
lv.setAdapter(adapter);
sv.setOnQueryTextListener(new OnQueryTextListener()) {
@Override
public boolean onQueryTextSubmit(String text) {
return false;
}
public boolean onQueryChange(String text) {
adapter.getFilter().filter(text);
return false;
}
}
有人可以提供逐行说明。
答案 0 :(得分:3)
您可以使用http://developer.android.com找到所有信息。在这种情况下,您正在寻找SearchView和SearchView.OnQueryTextListener。
首先,您需要将OnQueryTextListener
回调附加到SearchView
,以便向您提供用户正在做的信息:
onQueryTextSubmit
会在用户更改查询文本时通知您,如果SearchView
应执行显示任何建议(如果可用)的默认操作,则返回false,如果操作由侦听器处理,则返回true onQueryTextSubmit
,如果查询已由侦听器处理,则返回true,false使SearchView执行默认操作在onQueryChange
ArrayAdapter
您正在使用Filter
&#39; Filter
课程来过滤您的列表。如果您想知道public class Activity2 extends AppCompatActivity {
// Private member field to keep track of the count
private int mCount = 0;
private int mSum = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
final TextView countTextView = (TextView) findViewById(R.id.TextViewCount);
final TextView sumTextView = (TextView) findViewById(R.id.textSum);
final ImageButton countButton = (ImageButton) findViewById(R.id.beerCount);
final ImageButton drinkButton = (ImageButton) findViewById(R.id.drinkCount);
countButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mCount++;
mSum += 72;
countTextView.setText("You have been drinking " + mCount + " units!");
sumTextView.setText("Sum:" + mSum + "!");
}
});
drinkButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mCount++;
mSum += 96;
countTextView.setText("You have been drinking " + mCount + " units!");
sumTextView.setText("Sum:" + mSum + "!");
}
});
如何执行过滤,可以查看ArrayFilter
。
答案 1 :(得分:1)
试试这种方式
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<SearchView
android:id="@+id/search_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
的java
public class SearchViewFilterMode extends Activity implements SearchView.OnQueryTextListener {
SearchView mSearchView;
private ListView mListView;
private final String[] mStrings = Cheeses.sCheeseStrings;
@Override
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.searchview_filter);
mSearchView = (SearchView) findViewById(R.id.search_view);
mListView = (ListView) findViewById(R.id.list_view);
mListView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
mStrings));
mListView.setTextFilterEnabled(true);
setupSearchView();
}
private void setupSearchView() {
mSearchView.setIconifiedByDefault(false);
mSearchView.setOnQueryTextListener(this);
mSearchView.setSubmitButtonEnabled(true);
mSearchView.setQueryHint("Search Here");
}
public onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)) {
mListView.clearTextFilter();
} else {
mListView.setFilterText(newText.toString());
}
return true;
}
public boolean onQueryTextSubmit(String query) {
return false;
}