我想在我的代码中实现post liking
系统。
我的数据库看起来像: database
一旦用户点击了like按钮,他们的UID就应该存储在数据库中..
individual_row.xml
recycler.java
public class recycler extends AppCompatActivity{
RecyclerView recyclerView;
DatabaseReference myref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler);
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
myref = FirebaseDatabase.getInstance().getReference().child("famuid");
FirebaseRecyclerAdapter<Blog, BlogViewHolder> adapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
Blog.class,
R.layout.individual_row,
BlogViewHolder.class,
myref
) {
@Override
protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {
viewHolder.setName(model.getName());
viewHolder.setCaption(model.getCaption());
viewHolder.setImage(model.getImage());
}
};
recyclerView.setAdapter(adapter);
}}
Blog.java
public class Blog {
private String name,image,caption;
public Blog() {
}
public Blog(String name, String image, String caption) {
this.name = name;
this.caption = caption;
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
BlogViewHolder.java
public class BlogViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
View mView;
TextView textView_title;
TextView textView_decription;
ImageView imageView,likes;
FirebaseAuth mAuth;
DatabaseReference databaseref;
FirebaseAuth.AuthStateListener mAuthListener;
String uid;
public BlogViewHolder(final View itemView) {
super(itemView);
mView = itemView;
textView_title = (TextView)itemView.findViewById(R.id.title);
textView_decription = (TextView) itemView.findViewById(R.id.description);
imageView=(ImageView)itemView.findViewById(R.id.postimg1);
likes = (ImageView) itemView.findViewById(R.id.likes);
// set click event
likes.setOnClickListener(this);}
// onClick Listener for view
@Override
public void onClick(final View v) {
if (v.getId() == likes.getId()){
likes.setImageResource(R.drawable.red);
Toast.makeText(v.getContext(),uid,Toast.LENGTH_LONG).show();
Toast.makeText(v.getContext(), "ITEM PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
}
}
public void setName(String name) {
textView_title.setText(name);
}
public void setCaption(String caption) {
textView_decription.setText(caption);
}
public void setImage(String image) {
String c = "none";
if (image.equals(c))
{
imageView.setVisibility(View.GONE);
}
else {
BlogViewHolder.getBitmap loadBitmap = new BlogViewHolder.getBitmap();
loadBitmap.execute(image);
}
}
public class getBitmap extends AsyncTask<String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... params) {
Bitmap bit = null;
if (params.length == 0)
throw new IllegalArgumentException("You should pass me a url!!");
final String src = params[0];
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
bit = BitmapFactory.decodeStream(input);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bit;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if (bitmap !=null){
imageView = (ImageView) itemView.findViewById(R.id.postimg1);
imageView.setImageBitmap(bitmap);
}
}
}
}
我在喜欢(imageview)上实施了onclick
功能,将其从white heart
更改为red heart
,但如何存储他们的uid
以显示否。每个帖子都喜欢?