我想在我的应用程序中创建一个SearchView,但我有一个奇怪的错误。我不知道为什么,但我只能搜索列表的第一个元素。我无法搜索其他元素。我必须补充一点,我是Android的业余爱好者,请原谅我这个可能很愚蠢的问题。
以下是我的主要活动类和数据适配器
提前致谢
主要活动:
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private DataAdapter dataAdapter;
private List<ModelJsona> dataArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews(){
recyclerView=(RecyclerView) findViewById(R.id.card_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
loadJSON();
}
private void loadJSON(){
dataArrayList = new ArrayList<>();
Retrofit retrofit=new Retrofit.Builder().baseUrl("https://restcountries.eu/").addConverterFactory(GsonConverterFactory.create()).build();
RequestInterface requestInterface=retrofit.create(RequestInterface.class);
Call<List<ModelJsona>> call= requestInterface.getJSON();
call.enqueue(new Callback<List<ModelJsona>>() {
@Override
public void onResponse(Call<List<ModelJsona>> call, Response<List<ModelJsona>> response) {
dataArrayList = response.body();
dataAdapter=new DataAdapter(getApplicationContext(),dataArrayList);
recyclerView.setAdapter(dataAdapter);
}
@Override
public void onFailure(Call<List<ModelJsona>> call, Throwable t) {
Log.e("Error",t.getMessage());
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem search = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(search);
search(searchView);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
private void search(SearchView searchView) {
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
dataAdapter.getFilter().filter(newText);
return true;
}
});
}
}
数据适配器:
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.MyViewHolder> {
private Context context;
private List<ModelJsona> dataList;
private List<ModelJsona> FdataList;
public DataAdapter(Context context, List<ModelJsona> dataList) {
this.context = context;
this.dataList = dataList;
this.FdataList=dataList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_row, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.country_name.setText(dataList.get(position).getName());
holder.country_region.setText(dataList.get(position).getRegion());
holder.country_topleveldomain.setText(dataList.get(position).getTopLevelDomain().toString());
holder.country_nativename.setText(dataList.get(position).getNativeName());
}
@Override
public int getItemCount() {
return FdataList.size();
}
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
FdataList = dataList;
} else {
ArrayList<ModelJsona> filteredList = new ArrayList<>();
for (ModelJsona androidVersion : dataList) {
if ( androidVersion.getName().toLowerCase().contains(charString) ||
androidVersion.getRegion().toLowerCase().contains(charString) ||
androidVersion.getTopLevelDomain().contains(charString) ||
androidVersion.getNativeName().contains(charString))
{
filteredList.add(androidVersion);
}
}
FdataList = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = FdataList;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
FdataList = (ArrayList<ModelJsona>) filterResults.values;
notifyDataSetChanged();
}
};
}
答案 0 :(得分:0)
将onCreate中的SearchView放在onCreateOptionsMenu中。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchView = (SearchView) findViewById(R.id.searchBar);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
//put your filter here
//contentAdapter.getFilter().filter(s);
return true;
}
});
}