2012-06-11 5 views
1

텍스트와 이미지가 모두 포함 된이 Listview가 있습니다. 각 목록 항목에는 네 가지 핵심 요소가 포함되어 있습니다. 스피너에서 선택한 옵션에 따라 세 가지 TextView 중 하나를 사용하여 목록보기를 필터링 할 수 있기를 원합니다. 이것은 내가 지금까지 무엇을했는지 있습니다 :안드로이드의 회 전자를 사용하여 목록보기 필터링

public class LazyVenueAdapter extends BaseAdapter implements Filterable { 

     private Activity activity; 
     private ArrayList<HashMap<String, String>> data; 
     private static LayoutInflater inflater = null; 
     public ImageLoader imageLoader; 

     public LazyVenueAdapter(Activity a, ArrayList<HashMap<String, String>> d) { 
      activity = a; 
      data = d; 
      inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      imageLoader = new ImageLoader(activity.getApplicationContext()); 
     } 

     public int getCount() { 
      return data.size(); 
     } 

     public Object getItem(int position) { 
      return position; 
     } 

     public long getItemId(int position) { 
      return position; 
     } 

     @Override 
     public void notifyDataSetChanged() { 
      super.notifyDataSetChanged(); 
     } 

     public View getView(int position, View convertView, ViewGroup parent) { 
      View vi = convertView; 
      if (convertView == null) 
       vi = inflater.inflate(R.layout.venue_list_item, null); 

      TextView title = (TextView) vi.findViewById(R.id.venueName); 
      TextView location = (TextView) vi.findViewById(R.id.venueLocation); 
      TextView tags = (TextView) vi.findViewById(R.id.venueTags); 
      ImageView thumb_image = (ImageView) vi.findViewById(R.id.venueImage); 

      HashMap<String, String> venue = new HashMap<String, String>(); 
      venue = data.get(position); 

      // Setting all values in listview 
      title.setText(venue.get(VenuesFragment.KEY_TITLE)); 
      location.setText(venue.get(VenuesFragment.KEY_LOCATION)); 
      tags.setText(venue.get(VenuesFragment.KEY_TAGS)); 
      imageLoader.DisplayImage(venue.get(VenuesFragment.KEY_THUMB_URL), 
        thumb_image); 

      return vi; 
     } 

     @Override 
     public Filter getFilter() { 

      Filter filter = new Filter() { 

       @Override 
       protected FilterResults performFiltering(CharSequence constraint) { 

        FilterResults results = new FilterResults(); 
        ArrayList<HashMap<String, String>> filteredArrayVenues = new ArrayList<HashMap<String, String>>(); 
        data.clear(); 

        if (constraint == null || constraint.length() == 0) { 
         results.count = data.size(); 
         results.values = data; 
        } else { 
         constraint = constraint.toString(); 
         for (int index = 0; index < data.size(); index++) { 
          HashMap<String, String> dataVenues = data.get(index); 

          if (dataVenues.get(VenuesFragment.KEY_TAGS).toString().startsWith(
          constraint.toString())) { 
           filteredArrayVenues.add(dataVenues); 
          } 
         } 

         results.count = filteredArrayVenues.size(); 
         System.out.println(results.count); 

         results.values = filteredArrayVenues; 
         Log.e("VALUES", results.values.toString()); 
        } 

        return results; 
       } 

       @SuppressWarnings("unchecked") 
       @Override 
       protected void publishResults(CharSequence constraint, 
       FilterResults results) { 

        data = (ArrayList<HashMap<String, String>>) results.values; 
        notifyDataSetChanged(); 
       } 

      }; 

      return filter; 
     } 
    } 

내가 드롭 다운 목록에서 옵션을 선택하면 내가 다시 빈 결과 집합을 얻을 수 있다는 점이다 데 문제.

답변

1

당신은 수신하지 않는 결과에 notifyDataSetChanged()를 호출합니다. 나는 확실히 그렇게 할 것입니다,

data.clear(); 
+0

전적으로 도움을 주신 귀하의 제안에 진심으로 감사드립니다. 그래도 작동하려면 한 가지 더해야했습니다. TAGS ** startswith() **를 확인하고 ** contains() **를 사용하는 대신 –

+0

내 제안이 도움이 되었기 때문에 기쁩니다. 올바른 답을 확인해주십시오. 커뮤니티에서 질문이 해결되었음을 알리고 응답자에게 평판을 바칩니다. (가장 좋은 답변은 거의 완벽하지 않으며 가장 생산적인 응답을 선택하십시오.) – Sam

0

당신은 필터링 할 필요가 당신의 당신은 당신이 data.clear();과에서 결과를 구축을 위해 노력하고있는 정보를 삭제하기 때문에 ArrayList<HashMap<String, String>> data 첫째, 당신의 adapter

+0

확인하고 그 결과가 무엇인지를 참조하십시오 문제를 해결할 수있는이 줄을 제거

for (int index = 0; index < data.size(); index++) { 

을 : data (즉 data.size() == 0) 지금 비어있는 경우,이 루프는 실행되지 않습니다 . –

+0

내가 아직도 가지고있는 문제는 드롭 다운 목록에서 옵션을 선택했을 때 빈 결과 집합이 반환된다는 것입니다. –

관련 문제