我想从firebase数据库中检索数据(图像,名称,电话)。但搜索栏无效。输入一些文字时没有任何反应...
SearchBarActivity.java
public class SearchBarActivity extends AppCompatActivity {
EditText search_edit_text;
RecyclerView recyclerview;
private StorageReference mStorageRef;
private DatabaseReference mDatabaseRef;
FirebaseUser firebaseUser;
ArrayList<String> fullNameList;
ArrayList<String> userNameList;
ArrayList<String> profilePicList;
SearchAdapter searchAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_bar);
search_edit_text = (EditText) findViewById(R.id.search_edit_text);
recyclerview = (RecyclerView) findViewById(R.id.recyclerView);
mDatabaseRef = FirebaseDatabase.getInstance().getReference();
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
recyclerview.setHasFixedSize(true);
recyclerview.setLayoutManager(new LinearLayoutManager(this));
recyclerview.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
/*
* Create a array list for each node you want to use
* */
fullNameList = new ArrayList<>();
userNameList = new ArrayList<>();
profilePicList = new ArrayList<>();
search_edit_text.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (!s.toString().isEmpty()) {
setAdapter(s.toString());
}/* else {
* Clear the list when editText is empty
*
fullNameList.clear();
userNameList.clear();
profilePicList.clear();
recyclerView.removeAllViews();
}
*/}
});
}
private void setAdapter(final String searchedString) {
mDatabaseRef.child("users").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
/*
* Clear the list for every new search
* */
fullNameList.clear();
userNameList.clear();
profilePicList.clear();
recyclerview.removeAllViews();
int counter = 0;
/*
* Search all users for matching searched string
* */
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String uid = snapshot.getKey();
String full_name = snapshot.child("name").getValue(String.class);
String user_name = snapshot.child("phone").getValue(String.class);
String profile_pic = snapshot.child("imageUrl").getValue(String.class);
if (full_name.toLowerCase().contains(searchedString.toLowerCase())) {
fullNameList.add(full_name);
userNameList.add(user_name);
profilePicList.add(profile_pic);
counter++;
} else if (user_name.toLowerCase().contains(searchedString.toLowerCase())) {
fullNameList.add(full_name);
userNameList.add(user_name);
profilePicList.add(profile_pic);
counter++;
}
/*
* Get maximum of 15 searched results only
* */
if (counter == 15)
break;
}
searchAdapter = new SearchAdapter(SearchBarActivity.this, fullNameList, userNameList, profilePicList);
recyclerview.setAdapter(searchAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
SearchAdapter.java
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.SearchViewHolder> {
Context context;
ArrayList<String> fullNameList;
ArrayList<String> userNameList;
ArrayList<String> profilePicList;
class SearchViewHolder extends RecyclerView.ViewHolder {
ImageView profileImage;
TextView full_name, user_name;
public SearchViewHolder(View itemView) {
super(itemView);
profileImage = (ImageView) itemView.findViewById(R.id.imageUrl);
full_name = (TextView) itemView.findViewById(R.id.name);
user_name = (TextView) itemView.findViewById(R.id.phone);
}
}
public SearchAdapter(Context context, ArrayList<String> fullNameList, ArrayList<String> userNameList, ArrayList<String> profilePicList) {
this.context = context;
this.fullNameList = fullNameList;
this.userNameList = userNameList;
this.profilePicList = profilePicList;
}
@Override
public SearchAdapter.SearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.list_layout, parent, false);
return new SearchAdapter.SearchViewHolder(view);
}
@GlideModule
public class MyAppGlideModule extends AppGlideModule {
}
@Override
public void onBindViewHolder(SearchViewHolder holder, int position) {
holder.full_name.setText(fullNameList.get(position));
holder.user_name.setText(userNameList.get(position)); Glide.with(context).load(profilePicList.get(position)).into(holder.profileImage);
holder.full_name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Full Name Clicked", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return fullNameList.size();
}
摇篮
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.google.firebase:firebase-storage:11.0.4'
implementation 'com.google.firebase:firebase-auth:11.0.4'
implementation 'com.android.support:cardview-v7:26.1.0'
compile 'com.android.support:design:26.1.0'
implementation 'com.android.support:recyclerview-v7:26.1.0'
implementation 'com.firebaseui:firebase-ui-database:2.2.0'
implementation 'com.squareup.picasso:picasso:2.5.0'
implementation 'com.github.bumptech.glide:glide:4.0.0'
compile 'de.hdodenhof:circleimageview:2.1.0'
implementation 'com.google.firebase:firebase-database:11.0.4'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
我也试过滑行4.0.0
activity_search_bar.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/search_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Search Text..." />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
列表layout.xml
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/imageUrl"
android:layout_width="55dp"
android:layout_height="55dp"
android:padding="2dp"
android:src="@mipmap/ic_launcher_round"
app:civ_border_color="@color/colorPrimaryDark"
app:civ_border_width="1dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Full name goes here"
android:textColor="#212121"
android:textSize="15sp" />
<TextView
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="User name goes here" />
</LinearLayout>
我不知道错误是什么......没有错误.....我认为..Glide图书馆工作不正常..我浪费了我在Glide图书馆的两天,但我发现没什么..