gpt4 book ai didi

java - 自定义 ArrayAdapter 中的 Searchview

转载 作者:太空宇宙 更新时间:2023-11-04 11:03:01 27 4
gpt4 key购买 nike

我在“ListView”中有一个自定义的“ArrayAdapter”,我的问题是使用菜单“SearchView”。它无法正常工作。我不知道问题是搜索还是显示结果,因为什么也没有出现。而且,如果可能的话,我想显示自定义的结果列表。谢谢!

使用适配器进行 fragment

 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);
}

Collection 夹.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;
}

}

最佳答案

步骤:1 在 fragment 中为搜索 View 创建选项菜单,例如

 @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
//menu.clear();
inflater.inflate(R.menu.ticket_fragment_search_menu, menu);
MenuItem actionSearch = menu.findItem(R.id.action_search);

SearchView searchView = (SearchView) MenuItemCompat.getActionView(actionSearch);
searchView.setMaxWidth(Integer.MAX_VALUE);
EditText searchEditText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchEditText.setSingleLine(true);
searchEditText.setTextColor(getResources().getColor(R.color.black));
searchEditText.setHintTextColor(getResources().getColor(R.color.black));
searchView.setQueryHint(getResources().getString(R.string.txt_search_here));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
//apply Filter
adapter.setFilter(newText)
return false;
}
});

searchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e(TAG, "search open");
}
});

searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
Log.e(TAG, "search close");
return false;
}
});

}

第 2 步:在适配器中创建过滤或搜索功能

public void setFilter(String str) {
aList = new ArrayList<>();
if (aListTemp.size() > 0) {
for (int i = 0; i < aListTemp.size(); i++) {
if (aListTemp.get(i).toLowerCase().contains(str.toLowerCase())
) {

aList.add(aListTemp.get(i));
}
}
}
notifyDataSetChanged();
}

关于java - 自定义 ArrayAdapter 中的 Searchview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46689477/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com