我正在创建一个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;
}
}
答案 0 :(得分:1)
更改以下代码段 -
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,FilterResults results) {
clear();
addAll((List<UserData>) results.values)
users = (List<UserData>) results.values; // has the filtered values
notifyDataSetChanged(); // notifies the data with new filtered values
}
这主要解决了这个问题。但是源代码中存在很多不一致之处。您正在扩展已经实现ArrayAdpater
接口的Filterable
。
还要检查为什么需要以下状态变量 -
private List<UserData> messages;
private List<UserData> users;
private List<UserData> mOriginalValues;
请注意,您正在扩展ArrayAdapter
,并且您正在构造函数中传递初始数据集。此后数据集中的任何更改,您需要在父ArrayAdapter
上重置它。只是更改子类中的状态变量,不会有帮助。
答案 1 :(得分:0)
我自己解决了我的问题。它可能会帮助别人。 :)
<强> CustomAdapter.java 强>
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
clear();
addAll((List<UserData>) results.values);
arrayList = (List<UserData>)results.values;
notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults(); // Holds the results of a filtering operation in values
List<UserData> FilteredArrList = new ArrayList<UserData>();
if (mOriginalValues == null) {
mOriginalValues = new ArrayList<UserData>(arrayList);
}
if (constraint == null || constraint.length() == 0) {
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).getName();
if (data.toLowerCase().contains(constraint.toString())) {
FilteredArrList.add(mOriginalValues.get(i));
}
}
// set the Filtered result to return
results.count = FilteredArrList.size();
results.values = FilteredArrList;
}
return results;
}
};
return filter;
}