2017-10-11 226 views
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; 
} 

}

+0

你爲什麼不在列表中 –

+0

我不知道創建自己的搜索serchview,是我的第一個項目。我認爲這是最好的辦法。我如何創建這個? –

+0

https://stackoverflow.com/a/23422665/8648705 –

回答

0

步驟:1片段創建選項菜單烤焦chview像

@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(); 
    } 
相關問題