Asked Mar 31st, 2018 9:41 a.m. 860 0 1
  • 860 0 1
0

Làm sao để search ra item là object trong 1 listview

Share
  • 860 0 1

Trang https://www.javatpoint.com/android-searchview có hướng dẫn mình cách search 1 item trong list view. Nhưng trong hướng dẫn của nó listview ấy là dạng thường, mà cái listview của mình là customlistview dạng object class

java code của mình:

{

    ListView listView;
    
    ArrayList<NoteModel> arrayList;
    
    NoteAdapter noteAdapter;
 }

vậy khi sự kiên searchView.setOnQueryTextListener ở dưới mình phải thay đổi thế nào

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {  
        @Override  
        public boolean onQueryTextSubmit(String query) {  

            if(list.contains(query)){  
                adapter.getFilter().filter(query);  
            }else{  
                Toast.makeText(MainActivity.this, "No Match found",Toast.LENGTH_LONG).show();  
            }  
            return false;  
        }  

        @Override  
        public boolean onQueryTextChange(String newText) {  
        //    adapter.getFilter().filter(newText);  
            return false;  
        }  
});

1 ANSWERS


Answered Apr 1st, 2018 11:48 a.m.
-3

Custom lại Adapter

Theo mình vấn đề của bạn đang gặp phải là ở chỗ filter. Bạn có thể viết thêm một public method vào adapter như này chẳng hạn:

# NodeAdapter
public boolean filterByNodeTitle(String newText) {  
    IList filtered = new List();
    foreach(NodeModel note in this.notes) {
        if (note.title.toString().Contains(newText)) {
            filtered.Add(note);
        }
    }
   this.notes = filtered;
   // Thông báo cho ListView thay đổi:
   NotifyDataSetChanged();
} 


# activity:
public boolean onQueryTextChange(String newText) {  
    adapter.filterByNoteTitle(newText);
} 

Hoặc là bạn có thể custom lại getFilter(), hàm này return ra một instance Filter khác, lớp Filter này có method filter() chính là method filterByNoteTitle() ở trên.

Bạn thử xem nhé, chúc bạn thành công. À mà cá nhân mình lại thích dùng RecyclerView hơn là ListView. hihi

Share
Viblo
Let's register a Viblo Account to get more interesting posts.