Yêu cầu thg 3 31, 2018 9:41 SA 872 0 1
  • 872 0 1
0

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

Chia sẻ
  • 872 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 CÂU TRẢ LỜI


Đã trả lời thg 4 1, 2018 11:48 SA
-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

Chia sẻ
Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí