我尝试按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 console中得到的错误
错误:(65,17)错误:FirebaseRecyclerAdapter类中的构造函数FirebaseRecyclerAdapter无法应用于给定类型;
必需:FirebaseRecyclerOptions
发现:Class,int,Class,Query
原因:实际和形式参数列表的长度不同,其中T,VH是类型变量:错误
T扩展在类FirebaseRecyclerAdapter中声明的对象VH扩展了在类FirebaseRecyclerAdapter中声明的android.support.v7.widget.RecyclerView.ViewHolder
答案 0 :(得分:0)
您的代码中的问题是您正在混合Firebase-UI版本2和版本3.在上一版本中,当创建FirebaseRecyclerAdapter
的新实例时,您需要向构造函数传递一个参数,类型FirebaseRecyclerOptions
,而不是我在代码中看到的四个。由于您要求提供完整的示例, here 是如何从Firebase实时数据库检索数据并使用RecyclerView
在FirebaseRecyclerAdapter
中显示的数据。< / p>