1
我在'ListView'中有一個自定義'ArrayAdapter',我的問題是使用'SearchView'菜單。它工作不正常。我不知道問題是搜索還是顯示結果,因爲什麼也沒有出現。而且,如果可能的話,我想顯示一個自定義的結果列表。謝謝!在自定義ArrayAdapter中的Searchview
片段與適配器
ArrayList<Favoritos> favorites;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.list_item, container, false);
favorites = new ArrayList<Favoritos>();
favorites.add(new Favorites("Title", "addres", 5, false, 7, 22, 4.5f));
favorites.add(new Favorites("Title", "addres", 3.5 , true, 7, 22, 3.0f));
favorites.add(new Favorites("Title", "addres", 2, false, 7, 22, 4.0f));
favorites.add(new Favorites("Titleo", "addres", 4.5, true, 7, 22, 5.0f));
FavoritesAdapter adapter = new FavoritesAdapter(getActivity(), favorites);
ListView listView = (ListView) rootView.findViewById(R.id.list_item);
listView.setAdapter(adapter);
}
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String string) {
search(string);
return true;
}
//This is the search method
public void search(String s) {
if (s == null || s.trim().equals("")) {
cleanSearch(); //In this method I just "reset" the adapter when the user "give up"
return;
}
ArrayList<Favoritos> estacionamentosEncontrados = new ArrayList<Favoritos>(favoritos);
for (int i = estacionamentosEncontrados.size() - 1; i >= 0; i--) {
Favoritos estacionamento = estacionamentosEncontrados.get(i);
Favoritos nenhumResultado = new Favoritos("Nenhum resultado encontrado");
if (!estacionamento.mIndicacao.toUpperCase().contains(s.toUpperCase())) {
estacionamentosEncontrados.remove(estacionamento);
} else {
estacionamentosEncontrados.add(nenhumResultado);
}
}
adapter = new EncontradosAdapter(getActivity(), estacionamentosEncontrados);
ListView listView = (ListView) getActivity().findViewById(R.id.list_item);
listView.setAdapter(adapter);
}
Favorites.java
public String mIndicacao;
public String mEndereco;
private double mPreco;
public boolean mDisponibilidade;
private double mHoraAbre;
private double mHoraFecha;
private float mAvaliacao;
public Favoritos(){
}
public Favoritos(String indicacao, String endereco, double preco, boolean disponibilidade, double horaAbre, double horaFecha, float avaliacao){
mIndicacao = indicacao;
mEndereco = endereco;
mPreco = preco;
mDisponibilidade = disponibilidade;
mHoraAbre = horaAbre;
mHoraFecha = horaFecha;
mAvaliacao = avaliacao;
}
//Getters and Setters
而定製適配器
public class FavoritesAdapter extends ArrayAdapter<Favorites> {
public FavoritesAdapter(Context context, ArrayList<Favorites> favorites){
super(context, 0, favoritos);
}
public static class ItemViewHolder{
TextView indicadorTextView;
TextView enderecoTextView;
TextView precoTextView;
TextView horaAbreTextView;
TextView horaFechaTextView;
RatingBar notaEstacionamento;
ImageView disponivelImg;
TextView disponivelTextView;
ImageView esconderView;
public ItemViewHolder(View view){
indicadorTextView = (TextView) view.findViewById(R.id.indicador);
enderecoTextView = (TextView) view.findViewById(R.id.endereco);
precoTextView = (TextView) view.findViewById(R.id.precoValor);
horaAbreTextView = (TextView) view.findViewById(horaAbre);
horaFechaTextView = (TextView) view.findViewById(horaFecha);
notaEstacionamento = (RatingBar) view.findViewById(R.id.ratingBar);
disponivelImg = (ImageView) view.findViewById(R.id.img_disponibilidade);
disponivelTextView = (TextView) view.findViewById(R.id.disponivel);
esconderView = (ImageView) view.findViewById(R.id.esconder_view_btn);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if an existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.favorites_item, parent, false);
}
Favorites currentFavorito = getItem(position);
final ItemViewHolder holder;
if(listItemView.getTag() == null){
holder = new ItemViewHolder(listItemView);
listItemView.setTag(holder);
} else {
holder = (ItemViewHolder) listItemView.getTag();
}
if(currentFavorito.getDisponibilidade()){
holder.disponivelTextView.setText(currentFavorito.getIndicacao());
} else {
holder.disponivelTextView.setText(currentFavorito.getIndicacao());
}
holder.indicadorTextView.setText(currentFavorito.getIndicacao());
holder.enderecoTextView.setText(currentFavorito.getEndereco());
holder.precoTextView.setText(currentFavorito.getPreco());
holder.horaAbreTextView.setText(currentFavorito.getHoraAbre());
holder.horaFechaTextView.setText(currentFavorito.getHoraFecha());
holder.notaEstacionamento.setRating(currentFavorito.getAvaliacao());
if(currentFavorito.getDisponibilidade()){
holder.disponivelImg.setImageResource(R.drawable.ir_img);
holder.disponivelTextView.setText(R.string.aberto);
holder.disponivelTextView.setTextColor(getContext().getResources().getColor(R.color.verde));
} else {
holder.disponivelImg.setImageResource(R.drawable.fechado_img);
holder.disponivelTextView.setText(R.string.fechado);
holder.disponivelTextView.setTextColor(getContext().getResources().getColor(R.color.vermelho)); }
return listItemView;
}
}
你爲什麼不在列表中 –
我不知道創建自己的搜索serchview,是我的第一個項目。我認爲這是最好的辦法。我如何創建這個? –
https://stackoverflow.com/a/23422665/8648705 –