我有一个选项卡式活动,其中有三个选项卡。第二个标签应显示用户从Firebase在RecyclerView内的帖子。问题在于所讨论的选项卡似乎只是一个空白的空白屏幕,没有任何信息。它应该显示Firebase中具有特定用户ID的每个帖子,并且我有一个查询,该查询是我在FirebaseRecyclerAdapter内部传递的。 知道为什么帖子不显示吗?
这是选项卡式活动的主要Java类:
delete
这是我尝试显示帖子的片段java类:
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
private String thisPagesUsersId;
private String currentUidToUse;
private String tabTitles[] = new String[]{"Info", "Spots", "Likes"};
private FirebaseAuth firebaseAuth;
private DatabaseReference usersRef;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_users_page);
firebaseAuth = FirebaseAuth.getInstance();
usersRef = FirebaseDatabase.getInstance().getReference().child("Users");
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
finish();
}
});
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
//GETTING THE INFO WHOM INFO IS IN THIS PAGE
if(getIntent().hasExtra("uid"))
{
thisPagesUsersId = getIntent().getExtras().get("uid").toString();
currentUidToUse = thisPagesUsersId;
changeToolbarTitle(currentUidToUse);
UsersInfo fragmentInfo = new UsersInfo();
UsersSpots fragmentSpots = new UsersSpots();
fragmentInfo.setName(thisPagesUsersId);
fragmentSpots.setName(thisPagesUsersId);
}
else
{
currentUidToUse = firebaseAuth.getCurrentUser().getUid();
changeToolbarTitle(currentUidToUse);
UsersInfo fragmentInfo = new UsersInfo();
UsersSpots fragmentSpots = new UsersSpots();
fragmentInfo.resetName();
fragmentSpots.resetName();
}
}
//SET TOOLBAR TITLE TO USER NAME THAT IS BEING SPECTATED
private void changeToolbarTitle(String uid)
{
usersRef.child(uid).addValueEventListener(new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
String currentUserName = dataSnapshot.child("username").getValue().toString();
toolbar.setTitle(currentUserName);
}
@Override
public void onCancelled(DatabaseError databaseError)
{
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_users_page, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment
{
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment()
{
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber)
{
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = rootView = inflater.inflate(R.layout.fragment_users_info, container, false);
return rootView;
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter
{
public SectionsPagerAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public Fragment getItem(int position)
{
switch (position) {
case 0:
// Top Rated fragment activity
return new UsersInfo();
case 1:
// Games fragment activity
return new UsersSpots();
case 2:
// Movies fragment activity
return new UsersLikes();
}
return null;
}
@Override
public int getCount()
{
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position)
{
return tabTitles[position];
}
}
以下是该片段的xml:
RecyclerView spotsList;
FirebaseAuth firebaseAuth;
DatabaseReference postsRef, userReference, likesRef, seenRef;
public static String thisPagesUsersId = null;
private String currentUserId;
boolean detailsOpen;
boolean hasLiked = false;
boolean hasSeen = false;
private View mainView;
//GETTING THE USER WHOM INFO IS CURRENTLY IN THIS PAGE
public void setName(String string)
{
thisPagesUsersId = string;
}
//RESET PREVIOUS USER IS USER WANTS TO SPECTATE HIS OWN ACCOUNT
public void resetName()
{
thisPagesUsersId = null;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
mainView = inflater.inflate(R.layout.fragment_users_spots, container, false);
firebaseAuth = FirebaseAuth.getInstance();
currentUserId = firebaseAuth.getCurrentUser().getUid();
postsRef = FirebaseDatabase.getInstance().getReference().child("Posts");
userReference = FirebaseDatabase.getInstance().getReference().child("Users");
likesRef = FirebaseDatabase.getInstance().getReference().child("Likes");
seenRef = FirebaseDatabase.getInstance().getReference().child("Seen");
spotsList = (RecyclerView)mainView.findViewById(R.id.usersSpotsRV);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
spotsList.setHasFixedSize(true);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
spotsList.setLayoutManager(linearLayoutManager);
// Inflate the layout for this fragment
return mainView;
}
@Override
public void onStart()
{
super.onStart();
Query currentUserQuery = postsRef.orderByChild("userId").startAt(thisPagesUsersId).endAt(thisPagesUsersId + "\uf8ff");
FirebaseRecyclerAdapter<Posts, postsViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Posts, postsViewHolder>
(Posts.class, R.layout.spot_layout, postsViewHolder.class, currentUserQuery)
{
@Override
protected void populateViewHolder(final postsViewHolder viewHolder, Posts model, int position)
{
final String postKey = getRef(position).getKey();
final DatabaseReference currentPostRef = FirebaseDatabase.getInstance().getReference().child("Posts").child(postKey);
viewHolder.setDetails(model.getDetails());
viewHolder.setDate(model.getDate());
viewHolder.setImage(model.getImage());
viewHolder.setManufacturer(model.getManufacturer());
viewHolder.setModel(model.getModel());
viewHolder.setProfileImage(model.getProfileImage());
viewHolder.setTime(model.getTime());
viewHolder.setUsername(model.getUsername());
viewHolder.setLikeButtonStatus(postKey);
viewHolder.setSeenButtonStatus(postKey);
}
};
spotsList.setAdapter(firebaseRecyclerAdapter);
}
public static class postsViewHolder extends RecyclerView.ViewHolder
{
View mView;
TextView detailsTextView;
ImageView infoIW;
ImageView menuDotsIW;
ImageView locationIW;
ImageView likeIW;
ImageView seenIW;
TextView likesAmountTV;
TextView seenAmountTV;
int likesCount;
int seenCount;
String currentUserId;
DatabaseReference likesRef;
DatabaseReference seenRef;
public postsViewHolder(View itemView)
{
super(itemView);
mView = itemView;
detailsTextView = (TextView)mView.findViewById(R.id.postDetailTV);
detailsTextView.setVisibility(View.GONE);
infoIW = (ImageView)mView.findViewById(R.id.postInfoIW);
menuDotsIW = (ImageView)mView.findViewById(R.id.menuDotsIW);
locationIW = (ImageView)mView.findViewById(R.id.postLocateIW);
likeIW = (ImageView)mView.findViewById(R.id.likeIW);
seenIW = (ImageView)mView.findViewById(R.id.seenIW);
likesAmountTV = (TextView)mView.findViewById(R.id.likesAmountTV);
seenAmountTV = (TextView)mView.findViewById(R.id.seenTV);
likesRef = FirebaseDatabase.getInstance().getReference().child("Likes");
seenRef = FirebaseDatabase.getInstance().getReference().child("Seen");
currentUserId = FirebaseAuth.getInstance().getCurrentUser().getUid();
}
//SET ALL THE TEXTS AND IMAGES
public void setDate(String date)
{
TextView postDate = (TextView) mView.findViewById(R.id.postDateTV);
postDate.setText(date);
}
public void setDetails(String details)
{
TextView details_text = (TextView) mView.findViewById(R.id.postDetailTV);
details_text.setText(details);
}
public void setImage(String image)
{
ImageView profileImage = (ImageView) mView.findViewById(R.id.postTheImage);
Picasso.get().load(image).into(profileImage);
}
public void setManufacturer(String manufacturer)
{
TextView manufact_name = (TextView) mView.findViewById(R.id.postManufactTV);
manufact_name.setText(manufacturer);
}
public void setModel(String model)
{
TextView model_name = (TextView) mView.findViewById(R.id.postModelTV);
model_name.setText(model);
}
public void setProfileImage(String profileImage)
{
CircleImageView profImage = (CircleImageView) mView.findViewById(R.id.postProfpicIW);
Picasso.get().load(profileImage).into(profImage);
}
public void setTime(String time)
{
TextView postTime = (TextView) mView.findViewById(R.id.postTimeTV);
postTime.setText(time);
}
public void setUsername(String username)
{
TextView user_name = (TextView) mView.findViewById(R.id.postUsernameTV);
user_name.setText(username);
}
}
答案 0 :(得分:0)
private void setUpFirebaseAdapter() {
mFirebaseAdapter = new FirebaseRecyclerAdapter<Restaurant, FirebaseRestaurantViewHolder>
(Restaurant.class, R.layout.restaurant_list_item, FirebaseRestaurantViewHolder.class,
mRestaurantReference) {
@Override
protected void populateViewHolder(FirebaseRestaurantViewHolder viewHolder,
Restaurant model, int position) {
viewHolder.bindRestaurant(model);
}
};
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mFirebaseAdapter);
}
这是我在onCreateView中的代码