我正在使用Firebase制作类似社交网络的东西,我遇到了一个无法处理自己的问题。我想使用FireBaseRecycler View动态过滤数据。我有这样的事情:
所以,我想从EditText中获取文本并按名称列出。我试过这样的事情:
final Query queryRef = mUsersDatabase.orderByChild("name");
FirebaseRecyclerAdapter<Users, UsersActivity.UsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersActivity.UsersViewHolder>(
Users.class,
R.layout.users_single_layout,
UsersActivity.UsersViewHolder.class,
mUsersDatabase.orderByChild("Users").equalTo(string)
)
从EditText传递字符串
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
@Override
public void afterTextChanged(Editable arg0) {
string = editText.getText().toString();
}
});
但是:
mUsersList.setAdapter(firebaseRecyclerAdapter);
container.addView(view);
return view;
当我放置两个第一个通道(setAdapter和addView)应用程序崩溃时,我总是返回视图,我编辑EditText。更有甚者,这似乎是非常愚蠢和荒谬的选择,但实际上我是大脑滞后。有人知道解决方案吗?
答案 0 :(得分:0)
试试这段代码
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence query, int start, int before, int count) {
query = query.toString().trim().toLowerCase();
final ArrayList<DataModel> filteredList = new ArrayList<>();
final CharSequence finalQuery = query;
new Thread(new Runnable() {
@Override
public void run() {
// Clear the filter list
filteredList.clear();
// If there is no search value, then add all original list items to filter list
if (TextUtils.isEmpty(finalQuery)) {
filteredList.addAll(cities);
} else {
// Iterate in the original List and add it to filter list...
for (DataModel item : cities) {
if (item.getCity_name().toLowerCase().contains(finalQuery.toString().toLowerCase())
) {
// Adding Matched items
filteredList.add(item);
}
}
}
// Set on UI Thread
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
// Notify the List that the DataSet has changed...
adapter = new SearchCityAdapter(SearchCityClass.this, filteredList);
recyclerSearchCity.setAdapter(adapter);
}
});
}
}).start();
}
@Override
public void afterTextChanged(Editable s) {
}
});