我正在制作一个社交社交应用程序,例如facebook,在这里,您可以在Firebase的帮助下在时间轴上仅看到朋友的帖子。我创建了一个模块,可以将请求发送给他们的朋友,而朋友可以接受请求,并且在他的朋友列表中,但是问题是我不知道如何仅按时显示朋友的帖子。我已经写了一些代码,但是没有用。代码就是这样。.
package xyz.hashtagweb.Dashboard;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.content.Intent;
import android.widget.Toast;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import xyz.hashtagweb.hashtagbackpackers.Common.BottomNavigationViewHelper;
import xyz.hashtagweb.hashtagbackpackers.Common.common;
import xyz.hashtagweb.hashtagbackpackers.Model.PostModel;
import xyz.hashtagweb.hashtagbackpackers.R;
import xyz.hashtagweb.hashtagbackpackers.ViewHolder.FriendRequestViewHolder;
import xyz.hashtagweb.hashtagbackpackers.ViewHolder.PostViewHolder;
public class MainActivity extends AppCompatActivity {
private Context mContext = MainActivity.this;
private final static int ACTIVITY_NUM = 0;
TextView hashtagbackpackerstitle;
Button btnUpdateStatus,btnNewCheckIn;
EditText edtxtPost;
ImageView friends;
Typeface DosisMedium,DosisBold,DosisExtraBold,DosisExtraLight,DosisLight,DosisSemiBold;
RecyclerView rvPosts;
RecyclerView.LayoutManager layoutManager;
DatabaseReference postReferance,checkFriend;
FirebaseRecyclerAdapter<PostModel,PostViewHolder> adapter;
List<String> strings = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hashtagbackpackerstitle = findViewById(R.id.hashtagbackpackerstitle);
btnNewCheckIn = findViewById(R.id.btnNewCheckIn);
btnUpdateStatus = findViewById(R.id.btnUpdateStatus);
friends = findViewById(R.id.friends);
friends.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,FriendsActivity.class);
startActivity(i);
}
});
rvPosts = findViewById(R.id.rvPosts);
layoutManager = new LinearLayoutManager(this);
rvPosts.setLayoutManager(layoutManager);
DosisMedium = Typeface.createFromAsset(getAssets(),"fonts/Dosis-Medium.ttf");
DosisBold = Typeface.createFromAsset(getAssets(),"fonts/Dosis-Bold.ttf");
DosisExtraBold = Typeface.createFromAsset(getAssets(),"fonts/Dosis-ExtraBold.ttf");
DosisExtraLight = Typeface.createFromAsset(getAssets(),"fonts/Dosis-ExtraLight.ttf");
DosisSemiBold = Typeface.createFromAsset(getAssets(),"fonts/Dosis-SemiBold.ttf");
DosisLight = Typeface.createFromAsset(getAssets(),"fonts/Dosis-Light.ttf");
Typeface Satisfy = Typeface.createFromAsset(getAssets(),"fonts/Satisfy-Regular.ttf");
postReferance = FirebaseDatabase.getInstance().getReference("Posts");
hashtagbackpackerstitle.setTypeface(Satisfy);
btnNewCheckIn.setTypeface(DosisBold);
btnUpdateStatus.setTypeface(DosisBold);
setupBottomNavigationView(DosisBold);
btnUpdateStatus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialogBox();
}
});
checkFriend = FirebaseDatabase.getInstance().getReference("FriendList");
checkFriend.child(common.currentUser.getPhone()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChildren()){
String udi = dataSnapshot.getKey();
strings.add(udi);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
getPsosts();
Toast.makeText(mContext, ""+strings.size(), Toast.LENGTH_SHORT).show();
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
public void onStop() {
super.onStop();
if (adapter != null) {
adapter.stopListening();
}
}
private void getPsosts() {
Query query = FirebaseDatabase.getInstance().getReference("Posts").orderByChild("post_user_id").equalTo(String.valueOf(strings));
FirebaseRecyclerOptions<PostModel> options = new FirebaseRecyclerOptions.Builder<PostModel>()
.setQuery(query,PostModel.class)
.build();
adapter = new FirebaseRecyclerAdapter<PostModel, PostViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull final PostViewHolder holder, int position, @NonNull final PostModel model) {
final String userId = model.getPost_user_id();
checkFriend = FirebaseDatabase.getInstance().getReference("FriendList");
checkFriend.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.child(common.currentUser.getPhone()).child(userId).exists()){
holder.postmessgae.setText(model.getPost());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@NonNull
@Override
public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_post,parent,false);
return new PostViewHolder(view); }
};
rvPosts.setLayoutManager(layoutManager);
rvPosts.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
private void showDialogBox() {
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("What's on your mind?");
LayoutInflater inflater = this.getLayoutInflater();
View add_menu_layout = inflater.inflate(R.layout.dialog_update_status,null);
edtxtPost = add_menu_layout.findViewById(R.id.edtxtPost);
builder.setView(add_menu_layout);
builder.setPositiveButton("POST", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Date c = Calendar.getInstance().getTime();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
final String formattedDate = df.format(c);
PostModel postModel = new PostModel();
postModel.setPost(edtxtPost.getText().toString());
postModel.setDate(formattedDate);
postModel.setPost_user_id(common.currentUser.getPhone());
postReferance.child("POST_"+formattedDate).setValue(postModel);
Toast.makeText(mContext, "Your post is live!", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
}
private void setupBottomNavigationView(Typeface dosisBold){
BottomNavigationViewEx bottomNavigationViewEx = (BottomNavigationViewEx) findViewById(R.id.bottomNavViewBar);
BottomNavigationViewHelper.setupBottomNavigationView(bottomNavigationViewEx,dosisBold);
BottomNavigationViewHelper.enableNavigation(mContext,bottomNavigationViewEx);
Menu menu = bottomNavigationViewEx.getMenu();
MenuItem menuItem = menu.getItem(ACTIVITY_NUM);
menuItem.setChecked(true);
}
}
,数据库结构是这样的。 FIREBASE DATABASE STRUCTURE
我仍在学习编码,因此请详细回答,以便我理解。谢谢!