使用FirebaseRecyclerOptions面对挑战

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用FirebaseRecyclerOptions面对挑战相关的知识,希望对你有一定的参考价值。

我试图遵循this的答案,但没有人给出如何使用FirebaserecyclerOption的明确解释。请提供完整的代码示例请求帮助。

package com.kevin.postify;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;

import java.io.ByteArrayOutputStream;

import static com.kevin.postify.R.layout.activity_posts_list;

    // activity class

public class PostsListActivity extends AppCompatActivity {

RecyclerView mRecyclerView;
FirebaseDatabase mFirebaseDatabase;
DatabaseReference mRef;

@Override
 protected void onCreate(Bundle savedInstanceState) 
{super.onCreate(savedInstanceState);
 setContentView(activity_posts_list);// activity layout

 //Actionbar
 ActionBar actionBar = getSupportActionBar();
//set title
   actionBar.setTitle("Posts List");

   //RecyclerView
   mRecyclerView = findViewById(R.id.recyclerView);
   mRecyclerView.setHasFixedSize(true);

 //set layout as LinearLayout
  mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

 // Send query to FirebaseDatabase

  mFirebaseDatabase = FirebaseDatabase.getInstance();
    mRef = mFirebaseDatabase.getReference("Data");
  }

  //search data
  private void firebaseSearch(String searchText){

   //convert string entered in SearchView to lowercase
    String query = searchText.toLowerCase();

    Query firebaseSearchQuery = 
  mRef.orderByChild("search").startAt(query).endAt(query + "uf8ff");

  // here is where I get some errors in implementing firebaserecyclerOptions 
  //and has been myu MAIN issue plaz can someone help here i will apply to 
  //many hanging projects.

  FirebaseRecyclerOptions<PostsListActivity> options =
        new FirebaseRecyclerOptions.Builder<PostsListActivity>()
                .setLayout(R.layout.activity_posts_list)
                .build();

    // here too I get an error
    FirebaseRecyclerAdapter<Model, ViewHolder> firebaseRecyclerAdapter =
        new FirebaseRecyclerAdapter<Model, ViewHolder>(
                Model.class,
                R.layout.row,
                ViewHolder.class,
                firebaseSearchQuery
        ) {
            @Override

  // to populate don't think it work in latest version or maybe onBindView
  protected void populateViewHolder(ViewHolder viewHolder, 
  Model model, int position) {
                viewHolder.setDetails(getApplicationContext(), 
  model.getTitle(), model.getDescription(), model.getImage());
            }
       // viewHolder
            @Override
            public ViewHolder onCreateViewHolder(ViewGroup parent, int 
    viewType) {

                ViewHolder viewHolder = super.onCreateViewHolder(parent, 
   viewType);

                viewHolder.setOnClickListener(new 
   ViewHolder.ClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        //Views
                        TextView mTitleTv = 
  view.findViewById(R.id.rTitleTv);
                        TextView mDescTv = 
        view.findViewById(R.id.rDescriptionTv);
                        ImageView mImageView = 
  view.findViewById(R.id.rImageView);
                        //get data from views
                        String mTitle = mTitleTv.getText().toString();
                        String mDesc = mDescTv.getText().toString();
                        Drawable mDrawable = mImageView.getDrawable();
                        Bitmap mBitmap = 
        ((BitmapDrawable)mDrawable).getBitmap();



//pass this data to new activity
Intent intent = new Intent(view.getContext(), 
  PostDetailActivity.class);
                        ByteArrayOutputStream stream = new 
   ByteArrayOutputStream();
                        mBitmap.compress(Bitmap.CompressFormat.PNG, 100, 
   stream);
                        byte[] bytes = stream.toByteArray();
                        intent.putExtra("image", bytes); //put bitmap image 
       as array of bytes
                        intent.putExtra("title", mTitle); // put title
                        intent.putExtra("description", mDesc); //put 
 description
                        startActivity(intent); //start activity

                    }

                    @Override
                    public void onItemLongClick(View view, int position) {
                        //TODO do your own implementaion on long item click
                    }
                });

                return viewHolder;
            }
        };



  //set adapter to recyclerview
     mRecyclerView.setAdapter(firebaseRecyclerAdapter);}

 //load data into recycler view onStart

    @Override
    protected void onStart() {
super.onStart(); 


 //Firebase adapter which causes error

FirebaseRecyclerAdapter<Model, ViewHolder> firebaseRecyclerAdapter =
        new FirebaseRecyclerAdapter<Model, ViewHolder>(
                Model.class,
                R.layout.row,
                ViewHolder.class,
                mRef
        ) {
            @Override
            protected void populateViewHolder(ViewHolder viewHolder, Model 
model, int position) {
                viewHolder.setDetails(getApplicationContext(), 
model.getTitle(), model.getDescription(), model.getImage());
            }

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

                ViewHolder viewHolder = super.onCreateViewHolder(parent, 
viewType);

                viewHolder.setOnClickListener(new ViewHolder.ClickListener() 
{
                    @Override
                    public void onItemClick(View view, int position) {
                        //Views
                        TextView mTitleTv = 
view.findViewById(R.id.rTitleTv);
                        TextView mDescTv = 
view.findViewById(R.id.rDescriptionTv);
                        ImageView mImageView = 
view.findViewById(R.id.rImageView);  //image view
                        //get data from views
                        String mTitle = mTitleTv.getText().toString();
                        String mDesc = mDescTv.getText().toString();
                        Drawable mDrawable = mImageView.getDrawable();
         Bitmap mBitmap =((BitmapDrawable)mDrawable).getBitmap();



    //pass this data to new activity

                        Intent intent = new Intent(view.getContext(), 
PostDetailActivity.class);
 ByteArrayOutputStream stream = new ByteArrayOutputStream();
                        mBitmap.compress(Bitmap.CompressFormat.PNG, 100, 
stream);
                        byte[] bytes = stream.toByteArray();
                        intent.putExtra("image", bytes); //put bitmap image 
as array of bytes
                        intent.putExtra("title", mTitle); // put title
                        intent.putExtra("description", mDesc); //put 
description
                        startActivity(intent); //start activity


                    }

                    @Override
                    public void onItemLongClick(View view, int position) {
                        //TODO do your own implementaion on long item click
                    }
                });

                return viewHolder;
            }

        };

   //set adapter to recyclerview
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
}

 @Override
  public boolean onCreateOptionsMenu(Menu menu) {

//inflate the menu; this adds items to the action bar if it present
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String query) {
        firebaseSearch(query);
        return false;
    }
                //query change 

       @Override
    public boolean onQueryTextChange(String newText) {
        //Filter as you type
        firebaseSearch(newText);
        return false;
    }
});
return super.onCreateOptionsMenu(menu);
}

@Override
 public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

//handle other action bar item clicks here

if (id == R.id.action_settings){
    //TODO
    return true;
}

 return super.onOptionsItemSelected(item);
}

}

这是我在android studio控制台中得到的错误

错误:(65,17)错误:类FirebaseRecyclerAdapter中的构造函数FirebaseRecyclerAdapter无法应用于给定类型; 必需:FirebaseRecyclerOptions 发现:Class,int,Class,Query 原因:实际和形式参数列表的长度不同,其中T,VH是类型变量:

错误 T扩展在类FirebaseRecyclerAdapter中声明的Object扩展了在类FirebaseRecyclerAdapter中声明的android.support.v7.widget.RecyclerView.ViewHolder

答案

您的代码中的问题是您正在混合Firebase-UI版本2和版本3.在上一版本中,当创建FirebaseRecyclerAdapter的新实例时,您需要将构造函数传递给一个参数,该参数的类型为FirebaseRecyclerOptions而不是四个,因为我在你的代码中看到。因为您已经要求提供完整的示例,所以here是如何从Firebase实时数据库中检索数据并使用RecyclerView将其显示在FirebaseRecyclerAdapter中的。

以上是关于使用FirebaseRecyclerOptions面对挑战的主要内容,如果未能解决你的问题,请参考以下文章

如何通过微调器过滤 FirebaseRecyclerOptions 中的数据?

Android - FirebaseRecyclerOptions 在数据库中递归

(Android 应用程序)有没有办法在 FirebaseRecyclerOptions 查询的 SnapshotParser 部分中从 Firebase 数据库中检索数据?

firebase 查询如何从子项中检索两个特定值

我想使用 GeoFire 在 android 中填充 Firebase Recycler View

如何按两个字段对 Firebase 记录进行排序(Android)[重复]