我想在ListView中搜索项目。我添加了搜索功能。当我运行该应用程序时,我会看到搜索栏,但是当我在其中键入内容时,它并没有过滤ListView中的项目。我不知道错误在哪里。
public class HotellijstActivity extends AppCompatActivity {
private HotelArrayAdapter mijnAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hotellijst);
// Database
Thread mijnThread = new Thread(new Runnable() {
@Override
public void run() {
if (ApplicatieDatabase.getDatabase(HotellijstActivity.super.getApplicationContext()).getHotelDao().getAlleHotels().size() == 0) {
ApplicatieDatabase.getDatabase(HotellijstActivity.super.getApplicationContext()).getHotelDao().voegNieuweHotelToe(new Hotel(0, "Disneyland Hotel", "5min. lopen", R.drawable.disneyland_hotel));
ApplicatieDatabase.getDatabase(HotellijstActivity.super.getApplicationContext()).getHotelDao().voegNieuweHotelToe(new Hotel(0, "Disney's Hotel New York", "10min. lopen", R.drawable.disney_hotel_new_york));
}
final List<Hotel> alleHotels = ApplicatieDatabase.getDatabase(HotellijstActivity.super.getApplicationContext()).getHotelDao().getAlleHotels();
runOnUiThread(new Runnable() {
@Override
public void run() {
// fill up the list
vulLijstOp(alleHotels);
}
});
}
});
mijnThread.start();
}
使用数据库中的数据填充列表
public void vulLijstOp(final List<Hotel> alleHotels) {
mijnAdapter = new HotelArrayAdapter(this, alleHotels);
//my ListView id
ListView listview = (ListView)findViewById(R.id.lstHotels);
//my EditText id
EditText editText = (EditText)findViewById(R.id.editSearch);
listview.setAdapter(mijnAdapter);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
(HotellijstActivity.this).mijnAdapter.getFilter().filter(charSequence);
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
}