我有一个简单的搜索应用,可以搜索国家/地区列表。它只是一个简单的列表视图,只有字符串,但现在我不得不制作一个自定义列表适配器并将标志添加到列表,我的搜索(过滤器)不再有效,因为它总是返回相同的结果(第1结果)和其他几个,没有与我提供的搜索相关。
SearchActivity
public class SearchResults extends AppCompatActivity {
private static final String TAG = SearchResults.class.getSimpleName();
private ListView list;
private TextView emptyResultsTextView;
String[] listOfText = {
"afghanistan",
"albania",
"algeria",
"andorra",
"angola",
"antigua_and_barbuda",
"armenia",
"aruba",
"australia",
"austria",
"azerbaijan",
"bahamas_the",
"bahrain",
"bangladesh",
"barbados"
};
Integer[] listImages = {
R.mipmap.afghanistan,
R.mipmap.albania,
R.mipmap.algeria,
R.mipmap.andorra,
R.mipmap.angola,
R.mipmap.antigua_and_barbuda,
R.mipmap.armenia,
R.mipmap.aruba,
R.mipmap.australia,
R.mipmap.austria,
R.mipmap.azerbaijan,
R.mipmap.bahamas_the,
R.mipmap.bahrain,
R.mipmap.bangladesh,
R.mipmap.barbados
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_results);
CustomListAdapter adapter = new CustomListAdapter(this, listOfText, listImages);
Intent intent = getIntent();
String searchPhrase = intent.getExtras().getString("search");
Log.i(TAG, "Search phrase is " + searchPhrase);
list = (ListView) findViewById(R.id.searchResultsListView);
emptyResultsTextView = (TextView) findViewById(R.id.emptyResultsSearch);
emptyResultsTextView.setVisibility(View.GONE);
adapter.getFilter().filter(searchPhrase, new Filter.FilterListener() {
@Override
public void onFilterComplete(int count) {
if (count == 0) {
emptyResultsTextView.setVisibility(View.VISIBLE);
list.setVisibility(View.GONE);
emptyResultsTextView.setText(R.string.no_results);
}
}
});
adapter.notifyDataSetChanged();
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedItem = listOfText[+position];
Toast.makeText(getApplicationContext(), selectedItem, Toast.LENGTH_SHORT).show();
}
});
}
CustomListAdapter
public class CustomListAdapter extends ArrayAdapter<String>{
private final Activity activity;
private final String[] countryName;
private final Integer[] imgId;
public CustomListAdapter(Activity activity, String[] countryName, Integer[] imgId){
super(activity, R.layout.search_results_row_list, countryName);
this.activity = activity;
this.countryName = countryName;
this.imgId = imgId;
}
public View getView(int position, View view, ViewGroup parent){
LayoutInflater inflater = activity.getLayoutInflater();
View rowView = inflater.inflate(R.layout.search_results_row_list, null, false);
TextView txtCountry = (TextView)rowView.findViewById(R.id.country_text);
ImageView imageViewCountry = (ImageView)rowView.findViewById(R.id.country_flag);
txtCountry.setText(countryName[position]);
imageViewCountry.setImageResource(imgId[position]);
return rowView;
}
}
我怎样才能让它只搜索文本部分,即国家名称并返回?我是否正确使用以下方式进行搜索:
adapter.getFilter().filter(searchPhrase, new Filter.FilterListener() {
@Override
public void onFilterComplete(int count) {
if (count == 0) {
emptyResultsTextView.setVisibility(View.VISIBLE);
list.setVisibility(View.GONE);
emptyResultsTextView.setText(R.string.no_results);
}
}
});
谢谢