为Firebase数据库中的回收者视图设置搜索视图

时间:2018-07-06 03:49:30

标签: android firebase android-recyclerview android-search

我正在创建一个应用程序,其中我使用了recyclerview从firebase中获取内容,我想添加一个搜索视图。我的主要活动页面是

public class MainActivity extends AppCompatActivity {

// Creating DatabaseReference.
DatabaseReference databaseReference;

// Creating RecyclerView.
RecyclerView recyclerView;

// Creating RecyclerView.Adapter.
RecyclerView.Adapter adapter ;

// Creating Progress dialog
ProgressDialog progressDialog;

// Creating List of ImageUploadInfo class.
List<ImageUploadInfo> list = new ArrayList<>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Assign id to RecyclerView.
    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

    // Setting RecyclerView size true.
    recyclerView.setHasFixedSize(true);

    // Setting RecyclerView layout as LinearLayout.
    recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));

    // Assign activity this to progress dialog.
    progressDialog = new ProgressDialog(MainActivity.this);

    // Setting up message in Progress dialog.
    progressDialog.setMessage("Loading Images From Firebase.");

    // Showing progress dialog.
    progressDialog.show();

    // Setting up Firebase image upload folder path in databaseReference.
    // The path is already defined in MainActivity.
    databaseReference = FirebaseDatabase.getInstance().getReference("Hostel");

    // Adding Add Value Event Listener to databaseReference.
    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {

            List<ImageUploadInfo> list = new ArrayList<>();

            for (DataSnapshot postSnapshot : snapshot.getChildren()) {

                ImageUploadInfo imageUploadInfo = postSnapshot.getValue(ImageUploadInfo.class);

                list.add(imageUploadInfo);
            }

            adapter = new RecyclerViewAdapter(getApplicationContext(), list);

            recyclerView.setAdapter(adapter);

            // Hiding the progress dialog.
            progressDialog.dismiss();
        }




        @Override
        public void onCancelled(DatabaseError databaseError) {

            // Hiding the progress dialog.
            progressDialog.dismiss();

        }
    });

}
}

我的recyclerViewAdapter是:

public class RecyclerViewAdapter extends 
RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

Context context;
List<ImageUploadInfo> MainImageUploadInfoList;

public RecyclerViewAdapter(Context context, List<ImageUploadInfo> TempList) {

    this.MainImageUploadInfoList = TempList;

    this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);

    ViewHolder viewHolder = new ViewHolder(view);





    return viewHolder;
}

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    ImageUploadInfo UploadInfo = MainImageUploadInfoList.get(position);

    holder.imageNameTextView.setText(UploadInfo.getName() );
    holder.email.setText(UploadInfo.getEmail());

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Context context = view.getContext();
            Intent intent = new Intent(context, Main2Activity.class);
            intent.putExtra("name", holder.imageNameTextView.getText().toString());
            context.startActivity(intent);
        }
    });


    //Loading image from Glide library.
    Glide.with(context).load(UploadInfo.getImageAddress()).into(holder.imageView);
}



@Override
public int getItemCount() {

    return MainImageUploadInfoList.size();
}

class ViewHolder extends RecyclerView.ViewHolder {

    public ImageView imageView;
    public TextView imageNameTextView;
    public TextView email;

    public ViewHolder(View itemView) {
        super(itemView);

        imageView = (ImageView) itemView.findViewById(R.id.imageView);

        email = (TextView) itemView.findViewById(R.id.Email);

        imageNameTextView = (TextView) 
itemView.findViewById(R.id.ImageNameTextView);
    }
}
}

This is firebase date structure

这是我的自定义课程

public class ShowImages extends AppCompatActivity {

DatabaseReference databaseReference;
String name_global;
ImageView one,two,three,four;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_images);

    Bundle bundle = getIntent().getExtras();
    final String name = bundle.getString("key").toString();

    databaseReference = FirebaseDatabase.getInstance().getReference("Hostel");

    one = (ImageView) findViewById(R.id.one);
    two = (ImageView) findViewById(R.id.two);
    three = (ImageView) findViewById(R.id.three);
    four = (ImageView) findViewById(R.id.four);



    ValueEventListener postListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            name_global = dataSnapshot.child(name).child("name").getValue().toString();
            String temp_email = dataSnapshot.child(name).child("email").getValue().toString();


            String photoUrl = dataSnapshot.child(name).child("imageAddress").getValue(String.class);
            Glide.with(ShowImages.this).load(photoUrl).into(one);

            String photoUrl1 = dataSnapshot.child(name).child("image2Address").getValue(String.class);
            Glide.with(ShowImages.this).load(photoUrl1).into(two);

            String photoUrl2 = dataSnapshot.child(name).child("image3Address").getValue(String.class);
            Glide.with(ShowImages.this).load(photoUrl2).into(three);

            String photoUrl3 = dataSnapshot.child(name).child("image4Address").getValue(String.class);
            Glide.with(ShowImages.this).load(photoUrl3).into(four);




        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            // Getting Post failed, log a message
        }
    };
    databaseReference.addValueEventListener(postListener);
}
}

无论是在搜索视图中还是在编辑文本中,我都会有一个搜索按钮,但是我尝试了一个,但是出了点问题。如果您能帮助我,我将非常有帮助。请回答这个问题。

1 个答案:

答案 0 :(得分:0)

我得到了所需的输出。 在Main活动中,我创建了以下功能。    @Override     public boolean onCreateOptionsMenu(菜单菜单){

    getMenuInflater().inflate(R.menu.toolbar_menu,menu);
    MenuItem menuItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) menuItem.getActionView();
    searchView.setOnQueryTextListener(this);
    return true;
}

对于以下功能,我实现了SearchView

public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener{

上面的代码是Main Activity的开始

@Override
public boolean onQueryTextSubmit(String s) {
    return false;
}

@Override
public boolean onQueryTextChange(String s) {

    String userinput = s.toLowerCase();
    List<ImageUploadInfo> newList = new ArrayList();
    for(ImageUploadInfo name: list1)
    {
        if(name.getName().toLowerCase().contains(userinput)){
            newList.add(name);
        }

    }

    adapt.updateList(newList);
    recyclerView.setAdapter(adapt);

    return true;
}

然后在Custom Recycler View中,添加以下代码

public void updateList(List<ImageUploadInfo> newList)
{
    MainImageUploadInfoList = new ArrayList<>();
    MainImageUploadInfoList.addAll(newList);
    notifyDataSetChanged();
}

菜单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:title="Search"
    android:id="@+id/action_search"
    app:showAsAction="ifRoom|collapseActionView"
    app:actionViewClass="android.support.v7.widget.SearchView"/> 
</menu>