2017-09-13 356 views
1

我的問題是,我已經在我的列表視圖項目上實現了可篩選的搜索視圖。它運作良好,但執行搜索後,searchView爲空,我無法獲取以前的項目。我嘗試過notifyDataSetChanged,但它對我無效。我該怎麼辦?刷新列表視圖項目android

.... 
private ListView mListNotes; 
NoteListAdapter na; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mListNotes = (ListView) findViewById(R.id.main_listview); 
     mListNotes.setTextFilterEnabled(true); 

    } 
    @Override 
    protected void onResume() { 
     super.onResume(); 
     //load data into the listview and display 
     mListNotes.setAdapter(na); 
    } 

    @Override 
    public boolean onQueryTextChange(String query) { 
     if (!query.equals("")) { 
      na.getFilter().filter(query); // filter listView items when text is inputted. 

     } else { 
     na.notifyDataSetChanged();   // what do i put here to refresh my list view after search? 

     } 
     return false; 
    } 
} 

// ListView Adapter 


class NoteListAdapter extends ArrayAdapter<Note> implements Filterable{ 
    List<Note> objects; 
    private List<Note> mStringFilterList; 
    Filter filter; 
    private static final int WRAP_CONTENT_LENGTH = 5; 
    public NoteListAdapter(Context context, int resource, List<Note> objects) { 
     super(context, resource, objects); 
     this.objects = objects; 
     this.mStringFilterList = objects; 

    } 

    @Override 
    public int getCount() { 
     return objects.size(); 
    } 

    @Nullable 
    @Override 
    public Note getItem(int position) { 
     return objects.get(position); 
    } 

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


    @Override 
    public Filter getFilter() { 
     filter = new Filter() { 
      @Override 
      protected FilterResults performFiltering(CharSequence constraint) { 
       ArrayList<Note> tempList= new ArrayList<>(); 
       FilterResults results = new FilterResults(); 
       if (constraint != null && objects != null) { 
        for(Note singleNote : objects) { 
         if(singleNote.getTitle().contains(constraint)) 
          tempList.add(singleNote); 
        } 
        results.values = tempList; 
        results.count = tempList.size(); 
       } 

       return results; 
      } 
      @Override 
      protected void publishResults(CharSequence constraint, FilterResults results) { 
       objects = (ArrayList<Note>) results.values; 
       notifyDataSetChanged(); 

      } 
     }; 
     return filter; 

     } 


    } 


} 
+0

你還沒有發佈你的過濾器代碼中的註釋。 –

+0

na.getFilter()。clearFilter();並定義clearFilter() –

+0

clearFilter()包含filterList = originalList和notifyDataSetChanged() –

回答

0

閱讀其中的代碼需要更改或替換 整個適配器類該類

class NoteListAdapter extends ArrayAdapter<Note> implements Filterable 
{ 
List<Note> objects; 
private List<Note> mStringFilterList; 
Filter filter; 
private static final int WRAP_CONTENT_LENGTH = 5; 
public NoteListAdapter(Context context, int resource, List<Note> objects) { 
    super(context, resource, objects); 
    this.objects = objects; 
    this.mStringFilterList = objects; 

} 

@Override 
    public int getCount() { 
    return mStringFilterList.size(); // change this line 
    } 

@Nullable 
@Override 
public Note getItem(int position) { 
    return mStringFilterList.get(position); // change this line 
} 

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


@Override 
public Filter getFilter() { 
    filter = new Filter() { 
     @Override 
     protected FilterResults performFiltering(CharSequence constraint) { 
      ArrayList<Note> tempList= new ArrayList<>(); 
      FilterResults results = new FilterResults(); 
      if (constraint != null && constraint.length() > 0 && objects != null) { 
       for(Note singleNote : objects) { 
        if(singleNote.getTitle().contains(constraint)) 
         tempList.add(singleNote); 
       } 
       results.values = tempList; 
       results.count = tempList.size(); 
      } 

      // add else block. 
      else 
      { 
      results.values = objects; 
       results.count = objects.size(); 
      } 

      return results; 
     } 
     @Override 
     protected void publishResults(CharSequence constraint, FilterResults results) { 
      mStringFilterList = (ArrayList<Note>) results.values; // change this line 
      notifyDataSetChanged(); 

     } 
    }; 
    return filter; 

    } 


} 
+0

你剛剛救了我的一天。謝謝! – Benfight

0

您正在使用過濾的數據替換原始數據。使用下面的代碼:

class NoteListAdapter extends ArrayAdapter<Note> implements Filterable { 
     List<Note> objects; 
     private List<Note> originalList = new ArrayList<>(); 
     Filter filter; 
     private static final int WRAP_CONTENT_LENGTH = 5; 

     public NoteListAdapter(Context context, int resource, List<Note> objects) { 
      super(context, resource, objects); 
      this.objects = objects; 
      this.originalList.addAll(objects); 
     } 

     @Override 
     public int getCount() { 
      return objects.size(); 
     } 

     @Nullable 
     @Override 
     public Note getItem(int position) { 
      return objects.get(position); 
     } 

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


     @Override 
     public Filter getFilter() { 
      filter = new Filter() { 
       @Override 
       protected FilterResults performFiltering(CharSequence constraint) { 
        ArrayList<Note> tempList = new ArrayList<>(); 
        FilterResults results = new FilterResults(); 
        if (objects != null) { 
         if(constraint != null && !constraint.equals("")) { 
          for (Note singleNote : objects) { 
           if (singleNote.getTitle().contains(constraint)) 
            tempList.add(singleNote); 
          } 
          results.values = tempList; 
          results.count = tempList.size(); 
         }else { 
          tempList.clear(); 
          tempList.addAll(originalList); 
          results.values = tempList; 
          results.count = tempList.size(); 
         } 
        } 

        return results; 
       } 

       @Override 
       protected void publishResults(CharSequence constraint, FilterResults results) { 
        objects = (ArrayList<Note>) results.values; 
        notifyDataSetChanged(); 
       } 
      }; 
      return filter; 
     } 
    } 

希望它能幫助:)

+0

請查看我的代碼。我添加了我的適配器類 – Benfight

+0

我已更新我的答案。試試看@Benfight –

+0

這3行代碼tempList.clear(); tempList.addAll(originalList); results.values = tempList;可以被替換爲只有一行result.values = originalList;每當過濾器被稱爲一個新的templist對象將被創建,將永遠是空的。 –