我已经像这样实施了firebase回收商
public class HomeActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final String REQUIRED = "Required";
// [START declare_database_ref]
private DatabaseReference mDatabase;
// [END declare_database_ref]
private FirebaseAuth mAuth;
private static String LOG_TAG = "CardViewActivity";
public static class PostHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
View mView;
public PostHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
mView = itemView;
}
public void setName(String name) {
TextView field = (TextView) mView.findViewById(R.id.name);
field.setText(name);
}
public void setText(String text) {
TextView field = (TextView) mView.findViewById(R.id.body);
field.setText(text);
}
public void setImage(String image){
ImageView pp = (ImageView) mView.findViewById(R.id.image);
Picasso.with(Application.getAppContext()).load(image).placeholder(R.drawable.nodp).error(R.drawable.nodp).transform(new CircleTransform()).into(pp);
}
@Override
public void onClick(View mView) {
//what to do here
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
findViewById(R.id.progressBar3).setVisibility(View.VISIBLE);
findViewById(R.id.nopost).setVisibility(View.GONE);
//mListview = (ListView) findViewById(R.id.listview);
mAuth = FirebaseAuth.getInstance();
//the post list shit is happening here
final DatabaseReference root = FirebaseDatabase.getInstance().getReference();
FirebaseUser user = mAuth.getCurrentUser();
DatabaseReference postRef = root.child("users").child(user.getUid().toString()).child("posts");
RecyclerView recycler = (RecyclerView) findViewById(R.id.recyclerview);
recycler.setHasFixedSize(true);
recycler.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerAdapter mAdapter = new FirebaseRecyclerAdapter<PostList, PostHolder>(PostList.class, R.layout.row_one, PostHolder.class, postRef) {
@Override
public void populateViewHolder(PostHolder postViewHolder, PostList postList, int position) {
//try catch block to catch events of no posts, it will most likely return a null error, so im catching it, else
//find its exception and catch it
try {
String firstname = postList.getFirstname();
String lastname = postList.getLastname();
firstname = firstname.substring(0, 1).toUpperCase() + firstname.substring(1); //convert first string to uppercase
lastname = lastname.substring(0, 1).toUpperCase() + lastname.substring(1);// same thing happening here
String name = (firstname + " " + lastname); // concatenate firstname and lastname variable.
postViewHolder.setName(name);
postViewHolder.setText(postList.getBody()); // set the vew holder
//note that picasso view holder was applied in the view holder instead
String image = postList.getImgUrl().toString();
postViewHolder.setImage(image);
findViewById(R.id.progressBar3).setVisibility(View.GONE);
}
catch(NullPointerException e) {
findViewById(R.id.nopost).setVisibility(View.VISIBLE);
}
}
};
recycler.setAdapter(mAdapter);
}
}
并且它完全适用于我的用例,我已经解决了有关如何为回收器适配器设置onClick侦听器的问题,我想我已经在上面的代码中看到了它。
我的问题是:
intent.putExtra()
来实现此目的。但我的问题是我的观看者持有者课程不在活动onCreate()
之内(这是它似乎工作的唯一方式)所以我无法在ViewHolder中调用开始活动, 主要问题:由于我的数据所来自的PostList
尚未在ViewHolder中设置,因此我无法postList.getName()
和getBody()
在意图中,但我认为如果我的onClick()
可以进入firebase块本身就可以了,在这里:
FirebaseRecyclerAdapter mAdapter = new FirebaseRecyclerAdapter<PostList, PostHolder>(PostList.class, R.layout.row_one, PostHolder.class, postRef) {
@Override
public void populateViewHolder(PostHolder postViewHolder, PostList postList, int position) {
//try catch block to catch events of no posts, it will most likely return a null error, so im catching it, else
//find its exception and catch it
try {
String firstname = postList.getFirstname();
String lastname = postList.getLastname();
firstname = firstname.substring(0, 1).toUpperCase() + firstname.substring(1); //convert first string to uppercase
lastname = lastname.substring(0, 1).toUpperCase() + lastname.substring(1);// same thing happening here
String name = (firstname + " " + lastname); // concatenate firstname and lastname variable.
postViewHolder.setName(name);
postViewHolder.setText(postList.getBody()); // set the vew holder
//note that picasso view holder was applied in the view holder instead
String image = postList.getImgUrl().toString();
postViewHolder.setImage(image);
findViewById(R.id.progressBar3).setVisibility(View.GONE);
}
catch(NullPointerException e) {
findViewById(R.id.nopost).setVisibility(View.VISIBLE);
}
}
//onclick goes here with all the put extra and startactivity stuff
};
recycler.setAdapter(mAdapter);
答案 0 :(得分:0)
你的意思是你无法获得创建意图的原始活动?你试过 getContext(), getActivity()或 getBaseContext()吗?
Context context = getContext();
final Intent intent = new Intent(context, FirstActivity.class);
// all your stuff
context.startActivity(intent);
答案 1 :(得分:0)
如果您想以正确的方式执行好的项目点击功能,那么为FirebaseRecyclerAdapter
创建一个单独的类,并使用Interface
作为项目点击监听器,以便您可以从intent
执行HomeActivity
功能{1}}。
OR
创建HomeActivity
类的引用并在
@Override
public void onClick(View mView) {
//what to do here
//mcontect will be your class reference
Intent i = new (mcontext,SecondActivity.class);
startActivity(i);
}