我正在开发一个Android应用程序,我使用SearchView
小部件搜索自定义ListView
用户数据(图像+名称和多个文本字段)。问题是,当我输入查询时,它只搜索名称。有没有办法告诉SearchView
窗口小部件搜索ListView
单元格中的其他字符串?
提前谢谢。
以下代码是我目前在行动栏中搜索的活动:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.redirect_user, menu);
MenuItem menuitem = menu.findItem(R.id.action_search);
searchItem = (SearchView) menuitem.getActionView();
int id = searchItem.getContext().getResources()
.getIdentifier("android:id/search_src_text", null, null);
TextView textView = (TextView) searchItem.findViewById(id);
textView.setTextColor(Color.WHITE);
searchItem.setBackgroundColor(color.white);
searchItem.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
FiltreActivity.getAdapter().getFilter().filter(newText);
return false;
}
});
答案 0 :(得分:0)
试试这个
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
/**
* Shows a list that can be filtered in-place with a SearchView in non-iconified <span id="IL_AD1" class="IL_AD">mode</span>.
*/
public class SearchViewFilterMode extends Activity implements SearchView.OnQueryTextListener {
SearchView mSearchView;
private ListView mListView;
private final String[] mStrings = Cheeses.sCheeseStrings;
@Override
protected 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 boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)) {
mListView.clearTextFilter();
} else {
mListView.setFilterText(newText.toString());
}
return true;
}
public boolean onQueryTextSubmit(String query) {
return false;
}
}