问题
I am trying to hide a CheckBox
for the user that is logged in.
I display my data from firebase in a RecyclerView
with populateViewholder
. And I want to hide the CheckBox
from the current user logged in. But I am struggling to get it right.
MainActivity:
public class WishItemsActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
private DatabaseReference UsersRef, WishListRef, WishItemsRef;
static String currentUserID;
private ImageButton ibaddnewwishlistitem;
private String Key, databaseUserID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wish_items);
Key = getIntent().getExtras().get("Key").toString();
mAuth = FirebaseAuth.getInstance();
currentUserID = mAuth.getCurrentUser().getUid();
UsersRef = FirebaseDatabase.getInstance().getReference().child("Brukere");
WishListRef = FirebaseDatabase.getInstance().getReference().child("Ønskelister");
WishItemsRef = FirebaseDatabase.getInstance().getReference().child("Ønsker");
mToolbar = (Toolbar) findViewById(R.id.main_page_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle(" Ønsker");
ibaddnewwishlistitem = (ImageButton) findViewById(R.id.ib_addnewwishitem);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
WishItemList = (RecyclerView) findViewById(R.id.rv_wishitemslist);
WishItemList.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
WishItemList.setLayoutManager(linearLayoutManager);
ibaddnewwishlistitem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SendUserToAddWishItemActivity();
}
});
DisplayUserWishItems();
}
private void DisplayUserWishItems() {
FirebaseRecyclerAdapter<WishItemClass, WishItemViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<WishItemClass, WishItemViewHolder>
(
WishItemClass.class,
R.layout.single_list_item,
WishItemViewHolder.class,
WishItemsRef.child(Key)
) {
@Override
protected void populateViewHolder(WishItemViewHolder viewHolder, WishItemClass model, final int position) {
final String PostKey = getRef(position).getKey();
viewHolder.setWishitemname(model.getWishitemname());
viewHolder.setChecked(model.isChecked());
viewHolder.mView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
Intent cliconwishitem = new Intent(WishItemsActivity.this, ClickOnWishItemActivity.class);
cliconwishitem.putExtra("PostKey", PostKey);
cliconwishitem.putExtra("Key", Key);
startActivity(cliconwishitem);
return false;
}
});
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
};
WishItemList.setAdapter(firebaseRecyclerAdapter);
}
public static class WishItemViewHolder extends RecyclerView.ViewHolder {
View mView;
public WishItemViewHolder(@NonNull View itemView) {
super(itemView);
mView = itemView;
}
public void setWishitemname(String wishitemname) {
TextView wishitemname1 = (TextView) mView.findViewById(R.id.tv_wishitemname);
wishitemname1.setText(wishitemname);
}
public void setChecked (boolean Checked){
CheckBox checked = (CheckBox)mView.findViewById(R.id.checkBox);
checked.setChecked(Checked);
}
}
private void SendUserToAddWishItemActivity() {
Intent addwishitem = new Intent(WishItemsActivity.this, AddWishItemActivity.class);
addwishitem.putExtra("Key", Key);
startActivity(addwishitem);
}
private void SendUserToMainUserActivity() {
Intent mainuseractivity = new Intent(WishItemsActivity.this, MainUserActivity.class);
startActivity(mainuseractivity);
}
private void SendUserToAddWishListActivity() {
Intent addwishlistactivity = new Intent(WishItemsActivity.this, AddWishItemActivity.class);
startActivity(addwishlistactivity);
回答1:
You are getting currentUserId
from firebase auth. So you can hide/unhide checkbox based on currentUserId
.
if(currentUserId != null){
checked.setVisibility(View.INVISIBLE);
}
来源:https://stackoverflow.com/questions/57933618/hide-a-checkbox-for-the-current-user-that-is-logged-in-the-app