我正在創建一個android應用程序。在應用程序中,我有一個登錄頁面。登錄後顯示用戶列表。用戶列表活動是HomeActivity.java
,其中包含兩個片段,分別命名爲UserListFragment.java
和ChatFragment.java
用戶列表正在從firebase獲取。搜索在操作欄中的片段
UserListFragment.java包含一個具有搜索菜單和註銷菜單的操作欄。註銷菜單工作正常,但搜索不起作用。搜索圖標會顯示,但是當我搜索某些內容時,它不會顯示任何內容,即沒有過濾器。我正在使用firebase來顯示用戶列表。
我不想顯示在ChatFragment.java
的搜索查看我嘗試了各種不同的各種解決方案。但沒有人爲我工作。
這是代碼。
UserListFragment.java
public class UsersListFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
Activity activity;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private static final String TAG = "UsersListFragment";
private OnFragmentInteractionListener mListener;
private ListView listView;
private List<UserData> users;
private CustomAdapter adapter;
SharedPreferences.Editor preferenceEditor;
Timer myTimer;
View view;
ActionBar actionBar;
private static final String PREFRENCES_NAME = "setPreferences";
private ProgressDialog progressBar;
String partnerKeyValue;
// TODO: Rename and change types and number of parameters
public static UsersListFragment newInstance(String param1, String param2) {
UsersListFragment fragment = new UsersListFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Global.getInstance().unreadMessageUsers.clear();
progressBar = new ProgressDialog(getActivity());
progressBar.setCancelable(false);
progressBar.setMessage("Loading...");
progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressBar.setProgress(0);
}
@Override
public void onCreateOptionsMenu(Menu menu,MenuInflater inflater) {
inflater.inflate(R.menu.menu_userlist,menu);
MenuItem item = menu.findItem(R.id.menuSearch);
SearchView searchView = (SearchView)item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
Log.i(TAG,newText);
adapter.getFilter().filter(newText);
return true;
}
});
super.onCreateOptionsMenu(menu,inflater);
}
private void logoutUser(){
Intent I = new Intent(getActivity(), LoginActivity.class);
startActivity(I);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menuSearch :
return true;
case R.id.menuLogout :
logoutUser();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
menu_userlist.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/menuSearch"
android:title="@string/search"
android:icon="@drawable/ic_search"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="always">
</item>
<item android:id="@+id/menuLogout"
android:title="@string/logout"
android:icon="@drawable/ic_logout"
app:showAsAction="always">
</item>
CustomAdapter.java
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,FilterResults results) {
users = (List<UserData>) results.values; // has the filtered values
notifyDataSetChanged(); // notifies the data with new filtered values
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new Filter.FilterResults();
List<String> FilteredArrList = new ArrayList<String>();
if (mOriginalValues != null) {
if (constraint == null || constraint.length() == 0) {
// set the Original result to return
results.count = mOriginalValues.size();
results.values = mOriginalValues;
} else {
constraint = constraint.toString().toLowerCase();
for (int i = 0; i < mOriginalValues.size(); i++) {
String data = mOriginalValues.get(i).toString();
if (data.toLowerCase().contains(constraint.toString())) {
FilteredArrList.add(data);
}
}
// set the Filtered result to return
results.count = FilteredArrList.size();
results.values = FilteredArrList;
}
}
return results;
}
};
return filter;
}
}
是您的濾波方法的工作?您是否檢查了'results'返回變量的值 – Dibzmania
@Dibzmania my ** results **變量包含此值 **[email protected]** 每次我鍵入一些字母表某些值越來越多爲每個字母生成我輸入搜索如上。我不知道它是什麼。 – Rider
@Dibzmania我想我得到空值。當我試圖打印** results.values **時,我得到了空異常錯誤。我該如何解決它? – Rider