我在SearchView In ListView having a custom Adapter上关注了此代码 哪个工作正常。我试图在我的项目上实现它,但似乎无法正常工作。 只要从Firebase数据库服务器填充数据。
我删除了firebase的代码,这不是导致错误的原因。 我没有得到任何错误,但它没有工作。 这是我尝试过的:
ShowFriendsActivity.java
public class ShowFriendsActivity extends AppCompatActivity implements SearchView.OnQueryTextListener{
private ListView mMessageListView;
private ShowFriendsAdapter showFriendsAdapter;
SearchView searchView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_online_users);
searchView = (SearchView) findViewById(R.id.myownsearchview);
SettingFirebaseAndListView();
}
private void SettingFirebaseAndListView() {
mMessageListView = (ListView) findViewById(R.id.show_friends_listview);
final List<AuthenticatedUser> authenticatedUserList = new ArrayList<>();
showFriendsAdapter = new ShowFriendsAdapter(this, R.layout.show_friends_item_layout, authenticatedUserList);
mMessageListView.setAdapter(showFriendsAdapter);
mMessageListView.setTextFilterEnabled(true);
setupSearchView();
}
private void setupSearchView() {
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(this);
searchView.setSubmitButtonEnabled(true);
searchView.setQueryHint("Search Friend");
}
@Override
public boolean onQueryTextSubmit(String newText) {
return true;
}
@Override
public boolean onQueryTextChange(String s) {
mMessageListView.setFilterText(s);
return false;
}
}
ShowFriendsAdapter.java CustomAdapter 扩展 ArrayAdapter并实现可过滤接口
public class ShowFriendsAdapter extends ArrayAdapter<AuthenticatedUser> implements Filterable{
Cursor cr;
SQLiteDatabase db;
Context context;
List<AuthenticatedUser> listofAuthenticatedUsers;
List<AuthenticatedUser> listofAuthenticatedUsers2;
public ShowFriendsAdapter(Context context, int resource,List<AuthenticatedUser> objects) {
super(context, resource, objects);
this.context = context;
listofAuthenticatedUsers = objects;
}
public Filter getFilter(){
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults oReturn = new FilterResults();
final ArrayList<AuthenticatedUser> results = new ArrayList<AuthenticatedUser>();
if(listofAuthenticatedUsers2==null)
listofAuthenticatedUsers2=listofAuthenticatedUsers;
if(constraint!=null){
if (listofAuthenticatedUsers2 != null && listofAuthenticatedUsers2.size() > 0){
for (final AuthenticatedUser g : listofAuthenticatedUsers2) {
if (g.getName().toLowerCase()
.contains(constraint.toString()))
results.add(g);
}
}
oReturn.values = results;
}
return oReturn;
}
@Override
@SuppressWarnings("unchecked")
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
listofAuthenticatedUsers = (ArrayList<AuthenticatedUser>) filterResults.values;
notifyDataSetChanged();
}
};
}
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
@NonNull
@Override
public View getView(int position,View convertView,ViewGroup parent) {
if (convertView == null) {
convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.show_friends_item_layout, parent, false);
}
TextView userName = (TextView) convertView.findViewById(R.id.friend_name_textview);
TextView userEmail = (TextView) convertView.findViewById(R.id.friend_email_id_textview);
final ImageView userImage = (ImageView) convertView.findViewById(R.id.friend_name_image);
assert currentItem != null;
userName.setText(currentItem.getName());
userEmail.setText(currentItem.getEmail());
// If true , the app crashes
userImage.setClickable(true);
userImage.setEnabled(true);
userImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
return convertView;
}
}
AuthenticatedUser.java
public class AuthenticatedUser {
private String Name;
private String Email;
private String imageUrl;
// constructor
public AuthenticatedUser(){}
public AuthenticatedUser(String name, String email, String imageUrl) {
Name = name;
Email = email;
this.imageUrl = imageUrl;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
行布局show_friends_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/friend_name_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="8dp"
android:onClick="ChangeTheDisplayImage"
android:src="@drawable/contacts"
app:civ_border_color="#006064"
app:civ_border_width="2dp"
app:srcCompat="@drawable/contacts"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/friend_name_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Name"
android:textColor="@color/black"
android:textSize="@dimen/authen_user_size"/>
<TextView
android:id="@+id/friend_email_id_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Email ID"
android:textColor="@color/black"
android:textSize="8sp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
我的活动的主要布局
show_online_users.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.google.firebase.udacity.friendlychat.friends.ShowFriendsActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="60dp"
style="@style/HeaderBar"
app:theme="@style/Theme.AppCompat.DayNight.DarkActionBar"
app:popupTheme="@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar"
android:elevation="4dp"
/>
<SearchView
android:id="@+id/myownsearchview"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@+id/my_toolbar"
android:layout_marginTop="4dp"
android:tooltipText="Search"/>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefresh_Activity_show_Friends"
android:layout_marginTop="110dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:layout_marginTop="120dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/show_friends_listview"/>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>