Recycleview weird behaviour with firebase
I have a very simple recycle view with firebase adapter.
Everythig works fine except one weird thing:
The logic is - when user upload post without any picture - the relevant firebase node updated accordingly to "none", and so - the logic is - in this case, the relevant post will get only 100dp of width and height.
When i open my app - everything works fine, until i get to the bottom of the recycleview and then when i up - the post with pictures also getting smaller to 100dp... why???
Relevant code is below:
public class MainFeedActivity extends AppCompatActivity {
private static final String TAG = "MainFeedActivity";
private RecyclerView postList;
private FloatingActionButton AddNewPostButton, AddNewTextPostButton;
private DatabaseReference UsersRef, PostsRef;
private FirebaseAuth mAuth;
String currentUserID, post_user_uid_for_comment;
private String post_user_uid;
private long PostCount;
private Context mContext;
private int Post_Key_from_anonymus, PostIntKeyFromComment;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_feed);
Log.d(TAG, "onCreate: started.");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
mContext = MainFeedActivity.this;
mAuth = FirebaseAuth.getInstance();
currentUserID = mAuth.getCurrentUser().getUid();
UsersRef = FirebaseDatabase.getInstance().getReference().child("users");
PostsRef = FirebaseDatabase.getInstance().getReference().child("Posts");
AddNewPostButton = (FloatingActionButton) findViewById(R.id.add_new_post_button);
AddNewTextPostButton = (FloatingActionButton) findViewById(R.id.add_new_textpost_button);
//The next code is working on displaying all users posts
postList = (RecyclerView) findViewById(R.id.all_users_post_list);
postList.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainFeedActivity.this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
postList.setLayoutManager(linearLayoutManager);
AddNewPostButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SendUserToPostActivity();
}
});
AddNewTextPostButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SendUserToTextPostActivity();
}
});
DisplayAllUsersPosts();
}
@Override
protected void onPause() {
super.onPause();
finish();
}
private void DisplayAllUsersPosts() {
Query SortPostsInDecendingOrder = PostsRef.orderByChild("userRank");
FirebaseRecyclerAdapter<Posts, PostsViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Posts, PostsViewHolder>
(
Posts.class,
R.layout.all_post_layout,
PostsViewHolder.class,
SortPostsInDecendingOrder
) {
@Override
protected void populateViewHolder(final PostsViewHolder viewHolder, Posts model, int position) {
final String PostKey = getRef(position).getKey();
final int PostIntKey = position; //var for store post position to return back the user here. Should be declare only here!!!!!!
viewHolder.setFullname(model.getFullname());
viewHolder.setTime(model.getTime());
viewHolder.setDate(model.getDate());
viewHolder.setDescription(model.getDescription());
viewHolder.setPostimage(model.getPostimage());
PostsRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild(PostKey)) {
// String fullname=dataSnapshot.child(PostKey).child("fullname").getValue().toString();
// Toast.makeText(MainFeedActivity.this, fullname+"'s post", Toast.LENGTH_SHORT).show();
String degel = "none"; //"https://firebasestorage.googleapis.com/v0/b/bluesky-ec76e.appspot.com/o/country%20icons%2Floading.png?alt=media&token=c69f2f90-0c36-44c7-a8b8-a3e4fce336df";
String deleted = "This message was deleted by user";
if (dataSnapshot.child(PostKey).child("postimage").getValue().toString().equals(degel)
&& !dataSnapshot.child(PostKey).child("description").getValue().toString().equals(deleted)) {
// Toast.makeText(MainFeedActivity.this, "+"+PostKey+"+", Toast.LENGTH_SHORT).show();
int newHeight = 100; // New height in pixels
int newWidth = 100; // New width in pixels
// Apply the new height for ImageView programmatically
viewHolder.PostImage.getLayoutParams().height = newHeight;
// Apply the new width for ImageView programmatically
viewHolder.PostImage.getLayoutParams().width = newWidth;
// Set the scale type for ImageView image scaling
viewHolder.PostImage.setScaleType(ImageView.ScaleType.FIT_XY);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(30, 30);
viewHolder.PostDesc.setTextColor(Color.parseColor("#3700B3"));
viewHolder.PostDesc.setTextSize(35);
viewHolder.PostDesc.setMaxLines(5);
} else if (dataSnapshot.child(PostKey).child("postimage").getValue().toString().equals(degel)
&& dataSnapshot.child(PostKey).child("description").getValue().toString().equals(deleted))
{
//Toast.makeText(MainFeedActivity.this, "+"+PostKey+"+", Toast.LENGTH_SHORT).show();
int newHeight = 100; // New height in pixels
int newWidth = 100; // New width in pixels
// Apply the new height for ImageView programmatically
viewHolder.PostImage.getLayoutParams().height = newHeight;
// Apply the new width for ImageView programmatically
viewHolder.PostImage.getLayoutParams().width = newWidth;
// Set the scale type for ImageView image scaling
viewHolder.PostImage.setScaleType(ImageView.ScaleType.FIT_XY);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(30, 30);
viewHolder.PostDesc.setTextColor(Color.GRAY);
viewHolder.PostDesc.setTextSize(10);
// viewHolder.PostDesc.setMaxLines(5);
} else if (!dataSnapshot.child(PostKey).child("postimage").getValue().toString().equals(degel)
&& !dataSnapshot.child(PostKey).child("description").getValue().toString().equals(deleted))
{
if (viewHolder.PostDesc.getLineCount() == 1) {
viewHolder.PostDesc.setTextColor(Color.BLUE);
viewHolder.PostDesc.setTextSize(35);
} else if (viewHolder.PostDesc.getLineCount() == 2) {
viewHolder.PostDesc.setTextColor(Color.BLUE);
viewHolder.PostDesc.setTextSize(30);
} else if (viewHolder.PostDesc.getLineCount() == 3) {
viewHolder.PostDesc.setTextColor(Color.BLUE);
viewHolder.PostDesc.setTextSize(25);
} else if (viewHolder.PostDesc.getLineCount() == 4) {
viewHolder.PostDesc.setTextColor(Color.BLUE);
viewHolder.PostDesc.setTextSize(20);
} else if (viewHolder.PostDesc.getLineCount() > 4) {
viewHolder.PostDesc.setTextColor(Color.BLUE);
viewHolder.PostDesc.setTextSize(15);
}
}
// post_user_uid_for_comment = dataSnapshot.child(PostKey).child("uid").getValue().toString();
} else {
Toast.makeText(MainFeedActivity.this, "Not Exist", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
PostsRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild(PostKey)) {
post_user_uid_for_comment = dataSnapshot.child(PostKey).child("uid").getValue().toString();
} else {
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
viewHolder.CommentPostButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent commentsIntent = new Intent(MainFeedActivity.this, CommentsActivity.class);
commentsIntent.putExtra("Post_Key", PostKey);
startActivity(commentsIntent);
}
});
}
};
postList.setAdapter(firebaseRecyclerAdapter);
}
public static class PostsViewHolder extends RecyclerView.ViewHolder {
View mView;
ImageView CommentPostButton;
TextView PostDesc;
String currentUserID;
SquareImageView PostImage;
public PostsViewHolder(View itemView) {
super(itemView);
mView = itemView;
CommentPostButton = (ImageView) mView.findViewById(R.id.speech_bubble);
PostImage = (SquareImageView) mView.findViewById(R.id.post_image);
PostDesc = (TextView) mView.findViewById(R.id.post_description);
try {
currentUserID = FirebaseAuth.getInstance().getCurrentUser().getUid();
} catch (Exception e) {
/* This is a generic Exception handler which means it can handle
* all the exceptions. This will execute if the exception is not
* handled by previous catch blocks.
*/
Toast.makeText(mView.getContext(), "User is not exist", Toast.LENGTH_SHORT).show();
}
}
}
public void setFullname(String fullname) {
TextView username = (TextView) mView.findViewById(R.id.post_user_name);
username.setText(fullname);
}
public void setTime(String time) {
TextView PostTime = (TextView) mView.findViewById(R.id.post_time);
PostTime.setText(" " + time);
}
public void setDate(String date) {
TextView PostDate = (TextView) mView.findViewById(R.id.post_date);
PostDate.setText(" " + date);
}
public void setDescription(String description) {
TextView PostDescription = (TextView) mView.findViewById(R.id.post_description);
PostDescription.setText(description);
}
public void setPostimage(String postimage) {
ImageView PostImage = (ImageView) mView.findViewById(R.id.post_image);
Picasso.get().load(postimage).fit().into(PostImage);
}
}
}
firebase android-recyclerview
add a comment |
I have a very simple recycle view with firebase adapter.
Everythig works fine except one weird thing:
The logic is - when user upload post without any picture - the relevant firebase node updated accordingly to "none", and so - the logic is - in this case, the relevant post will get only 100dp of width and height.
When i open my app - everything works fine, until i get to the bottom of the recycleview and then when i up - the post with pictures also getting smaller to 100dp... why???
Relevant code is below:
public class MainFeedActivity extends AppCompatActivity {
private static final String TAG = "MainFeedActivity";
private RecyclerView postList;
private FloatingActionButton AddNewPostButton, AddNewTextPostButton;
private DatabaseReference UsersRef, PostsRef;
private FirebaseAuth mAuth;
String currentUserID, post_user_uid_for_comment;
private String post_user_uid;
private long PostCount;
private Context mContext;
private int Post_Key_from_anonymus, PostIntKeyFromComment;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_feed);
Log.d(TAG, "onCreate: started.");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
mContext = MainFeedActivity.this;
mAuth = FirebaseAuth.getInstance();
currentUserID = mAuth.getCurrentUser().getUid();
UsersRef = FirebaseDatabase.getInstance().getReference().child("users");
PostsRef = FirebaseDatabase.getInstance().getReference().child("Posts");
AddNewPostButton = (FloatingActionButton) findViewById(R.id.add_new_post_button);
AddNewTextPostButton = (FloatingActionButton) findViewById(R.id.add_new_textpost_button);
//The next code is working on displaying all users posts
postList = (RecyclerView) findViewById(R.id.all_users_post_list);
postList.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainFeedActivity.this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
postList.setLayoutManager(linearLayoutManager);
AddNewPostButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SendUserToPostActivity();
}
});
AddNewTextPostButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SendUserToTextPostActivity();
}
});
DisplayAllUsersPosts();
}
@Override
protected void onPause() {
super.onPause();
finish();
}
private void DisplayAllUsersPosts() {
Query SortPostsInDecendingOrder = PostsRef.orderByChild("userRank");
FirebaseRecyclerAdapter<Posts, PostsViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Posts, PostsViewHolder>
(
Posts.class,
R.layout.all_post_layout,
PostsViewHolder.class,
SortPostsInDecendingOrder
) {
@Override
protected void populateViewHolder(final PostsViewHolder viewHolder, Posts model, int position) {
final String PostKey = getRef(position).getKey();
final int PostIntKey = position; //var for store post position to return back the user here. Should be declare only here!!!!!!
viewHolder.setFullname(model.getFullname());
viewHolder.setTime(model.getTime());
viewHolder.setDate(model.getDate());
viewHolder.setDescription(model.getDescription());
viewHolder.setPostimage(model.getPostimage());
PostsRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild(PostKey)) {
// String fullname=dataSnapshot.child(PostKey).child("fullname").getValue().toString();
// Toast.makeText(MainFeedActivity.this, fullname+"'s post", Toast.LENGTH_SHORT).show();
String degel = "none"; //"https://firebasestorage.googleapis.com/v0/b/bluesky-ec76e.appspot.com/o/country%20icons%2Floading.png?alt=media&token=c69f2f90-0c36-44c7-a8b8-a3e4fce336df";
String deleted = "This message was deleted by user";
if (dataSnapshot.child(PostKey).child("postimage").getValue().toString().equals(degel)
&& !dataSnapshot.child(PostKey).child("description").getValue().toString().equals(deleted)) {
// Toast.makeText(MainFeedActivity.this, "+"+PostKey+"+", Toast.LENGTH_SHORT).show();
int newHeight = 100; // New height in pixels
int newWidth = 100; // New width in pixels
// Apply the new height for ImageView programmatically
viewHolder.PostImage.getLayoutParams().height = newHeight;
// Apply the new width for ImageView programmatically
viewHolder.PostImage.getLayoutParams().width = newWidth;
// Set the scale type for ImageView image scaling
viewHolder.PostImage.setScaleType(ImageView.ScaleType.FIT_XY);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(30, 30);
viewHolder.PostDesc.setTextColor(Color.parseColor("#3700B3"));
viewHolder.PostDesc.setTextSize(35);
viewHolder.PostDesc.setMaxLines(5);
} else if (dataSnapshot.child(PostKey).child("postimage").getValue().toString().equals(degel)
&& dataSnapshot.child(PostKey).child("description").getValue().toString().equals(deleted))
{
//Toast.makeText(MainFeedActivity.this, "+"+PostKey+"+", Toast.LENGTH_SHORT).show();
int newHeight = 100; // New height in pixels
int newWidth = 100; // New width in pixels
// Apply the new height for ImageView programmatically
viewHolder.PostImage.getLayoutParams().height = newHeight;
// Apply the new width for ImageView programmatically
viewHolder.PostImage.getLayoutParams().width = newWidth;
// Set the scale type for ImageView image scaling
viewHolder.PostImage.setScaleType(ImageView.ScaleType.FIT_XY);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(30, 30);
viewHolder.PostDesc.setTextColor(Color.GRAY);
viewHolder.PostDesc.setTextSize(10);
// viewHolder.PostDesc.setMaxLines(5);
} else if (!dataSnapshot.child(PostKey).child("postimage").getValue().toString().equals(degel)
&& !dataSnapshot.child(PostKey).child("description").getValue().toString().equals(deleted))
{
if (viewHolder.PostDesc.getLineCount() == 1) {
viewHolder.PostDesc.setTextColor(Color.BLUE);
viewHolder.PostDesc.setTextSize(35);
} else if (viewHolder.PostDesc.getLineCount() == 2) {
viewHolder.PostDesc.setTextColor(Color.BLUE);
viewHolder.PostDesc.setTextSize(30);
} else if (viewHolder.PostDesc.getLineCount() == 3) {
viewHolder.PostDesc.setTextColor(Color.BLUE);
viewHolder.PostDesc.setTextSize(25);
} else if (viewHolder.PostDesc.getLineCount() == 4) {
viewHolder.PostDesc.setTextColor(Color.BLUE);
viewHolder.PostDesc.setTextSize(20);
} else if (viewHolder.PostDesc.getLineCount() > 4) {
viewHolder.PostDesc.setTextColor(Color.BLUE);
viewHolder.PostDesc.setTextSize(15);
}
}
// post_user_uid_for_comment = dataSnapshot.child(PostKey).child("uid").getValue().toString();
} else {
Toast.makeText(MainFeedActivity.this, "Not Exist", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
PostsRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild(PostKey)) {
post_user_uid_for_comment = dataSnapshot.child(PostKey).child("uid").getValue().toString();
} else {
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
viewHolder.CommentPostButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent commentsIntent = new Intent(MainFeedActivity.this, CommentsActivity.class);
commentsIntent.putExtra("Post_Key", PostKey);
startActivity(commentsIntent);
}
});
}
};
postList.setAdapter(firebaseRecyclerAdapter);
}
public static class PostsViewHolder extends RecyclerView.ViewHolder {
View mView;
ImageView CommentPostButton;
TextView PostDesc;
String currentUserID;
SquareImageView PostImage;
public PostsViewHolder(View itemView) {
super(itemView);
mView = itemView;
CommentPostButton = (ImageView) mView.findViewById(R.id.speech_bubble);
PostImage = (SquareImageView) mView.findViewById(R.id.post_image);
PostDesc = (TextView) mView.findViewById(R.id.post_description);
try {
currentUserID = FirebaseAuth.getInstance().getCurrentUser().getUid();
} catch (Exception e) {
/* This is a generic Exception handler which means it can handle
* all the exceptions. This will execute if the exception is not
* handled by previous catch blocks.
*/
Toast.makeText(mView.getContext(), "User is not exist", Toast.LENGTH_SHORT).show();
}
}
}
public void setFullname(String fullname) {
TextView username = (TextView) mView.findViewById(R.id.post_user_name);
username.setText(fullname);
}
public void setTime(String time) {
TextView PostTime = (TextView) mView.findViewById(R.id.post_time);
PostTime.setText(" " + time);
}
public void setDate(String date) {
TextView PostDate = (TextView) mView.findViewById(R.id.post_date);
PostDate.setText(" " + date);
}
public void setDescription(String description) {
TextView PostDescription = (TextView) mView.findViewById(R.id.post_description);
PostDescription.setText(description);
}
public void setPostimage(String postimage) {
ImageView PostImage = (ImageView) mView.findViewById(R.id.post_image);
Picasso.get().load(postimage).fit().into(PostImage);
}
}
}
firebase android-recyclerview
add a comment |
I have a very simple recycle view with firebase adapter.
Everythig works fine except one weird thing:
The logic is - when user upload post without any picture - the relevant firebase node updated accordingly to "none", and so - the logic is - in this case, the relevant post will get only 100dp of width and height.
When i open my app - everything works fine, until i get to the bottom of the recycleview and then when i up - the post with pictures also getting smaller to 100dp... why???
Relevant code is below:
public class MainFeedActivity extends AppCompatActivity {
private static final String TAG = "MainFeedActivity";
private RecyclerView postList;
private FloatingActionButton AddNewPostButton, AddNewTextPostButton;
private DatabaseReference UsersRef, PostsRef;
private FirebaseAuth mAuth;
String currentUserID, post_user_uid_for_comment;
private String post_user_uid;
private long PostCount;
private Context mContext;
private int Post_Key_from_anonymus, PostIntKeyFromComment;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_feed);
Log.d(TAG, "onCreate: started.");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
mContext = MainFeedActivity.this;
mAuth = FirebaseAuth.getInstance();
currentUserID = mAuth.getCurrentUser().getUid();
UsersRef = FirebaseDatabase.getInstance().getReference().child("users");
PostsRef = FirebaseDatabase.getInstance().getReference().child("Posts");
AddNewPostButton = (FloatingActionButton) findViewById(R.id.add_new_post_button);
AddNewTextPostButton = (FloatingActionButton) findViewById(R.id.add_new_textpost_button);
//The next code is working on displaying all users posts
postList = (RecyclerView) findViewById(R.id.all_users_post_list);
postList.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainFeedActivity.this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
postList.setLayoutManager(linearLayoutManager);
AddNewPostButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SendUserToPostActivity();
}
});
AddNewTextPostButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SendUserToTextPostActivity();
}
});
DisplayAllUsersPosts();
}
@Override
protected void onPause() {
super.onPause();
finish();
}
private void DisplayAllUsersPosts() {
Query SortPostsInDecendingOrder = PostsRef.orderByChild("userRank");
FirebaseRecyclerAdapter<Posts, PostsViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Posts, PostsViewHolder>
(
Posts.class,
R.layout.all_post_layout,
PostsViewHolder.class,
SortPostsInDecendingOrder
) {
@Override
protected void populateViewHolder(final PostsViewHolder viewHolder, Posts model, int position) {
final String PostKey = getRef(position).getKey();
final int PostIntKey = position; //var for store post position to return back the user here. Should be declare only here!!!!!!
viewHolder.setFullname(model.getFullname());
viewHolder.setTime(model.getTime());
viewHolder.setDate(model.getDate());
viewHolder.setDescription(model.getDescription());
viewHolder.setPostimage(model.getPostimage());
PostsRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild(PostKey)) {
// String fullname=dataSnapshot.child(PostKey).child("fullname").getValue().toString();
// Toast.makeText(MainFeedActivity.this, fullname+"'s post", Toast.LENGTH_SHORT).show();
String degel = "none"; //"https://firebasestorage.googleapis.com/v0/b/bluesky-ec76e.appspot.com/o/country%20icons%2Floading.png?alt=media&token=c69f2f90-0c36-44c7-a8b8-a3e4fce336df";
String deleted = "This message was deleted by user";
if (dataSnapshot.child(PostKey).child("postimage").getValue().toString().equals(degel)
&& !dataSnapshot.child(PostKey).child("description").getValue().toString().equals(deleted)) {
// Toast.makeText(MainFeedActivity.this, "+"+PostKey+"+", Toast.LENGTH_SHORT).show();
int newHeight = 100; // New height in pixels
int newWidth = 100; // New width in pixels
// Apply the new height for ImageView programmatically
viewHolder.PostImage.getLayoutParams().height = newHeight;
// Apply the new width for ImageView programmatically
viewHolder.PostImage.getLayoutParams().width = newWidth;
// Set the scale type for ImageView image scaling
viewHolder.PostImage.setScaleType(ImageView.ScaleType.FIT_XY);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(30, 30);
viewHolder.PostDesc.setTextColor(Color.parseColor("#3700B3"));
viewHolder.PostDesc.setTextSize(35);
viewHolder.PostDesc.setMaxLines(5);
} else if (dataSnapshot.child(PostKey).child("postimage").getValue().toString().equals(degel)
&& dataSnapshot.child(PostKey).child("description").getValue().toString().equals(deleted))
{
//Toast.makeText(MainFeedActivity.this, "+"+PostKey+"+", Toast.LENGTH_SHORT).show();
int newHeight = 100; // New height in pixels
int newWidth = 100; // New width in pixels
// Apply the new height for ImageView programmatically
viewHolder.PostImage.getLayoutParams().height = newHeight;
// Apply the new width for ImageView programmatically
viewHolder.PostImage.getLayoutParams().width = newWidth;
// Set the scale type for ImageView image scaling
viewHolder.PostImage.setScaleType(ImageView.ScaleType.FIT_XY);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(30, 30);
viewHolder.PostDesc.setTextColor(Color.GRAY);
viewHolder.PostDesc.setTextSize(10);
// viewHolder.PostDesc.setMaxLines(5);
} else if (!dataSnapshot.child(PostKey).child("postimage").getValue().toString().equals(degel)
&& !dataSnapshot.child(PostKey).child("description").getValue().toString().equals(deleted))
{
if (viewHolder.PostDesc.getLineCount() == 1) {
viewHolder.PostDesc.setTextColor(Color.BLUE);
viewHolder.PostDesc.setTextSize(35);
} else if (viewHolder.PostDesc.getLineCount() == 2) {
viewHolder.PostDesc.setTextColor(Color.BLUE);
viewHolder.PostDesc.setTextSize(30);
} else if (viewHolder.PostDesc.getLineCount() == 3) {
viewHolder.PostDesc.setTextColor(Color.BLUE);
viewHolder.PostDesc.setTextSize(25);
} else if (viewHolder.PostDesc.getLineCount() == 4) {
viewHolder.PostDesc.setTextColor(Color.BLUE);
viewHolder.PostDesc.setTextSize(20);
} else if (viewHolder.PostDesc.getLineCount() > 4) {
viewHolder.PostDesc.setTextColor(Color.BLUE);
viewHolder.PostDesc.setTextSize(15);
}
}
// post_user_uid_for_comment = dataSnapshot.child(PostKey).child("uid").getValue().toString();
} else {
Toast.makeText(MainFeedActivity.this, "Not Exist", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
PostsRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild(PostKey)) {
post_user_uid_for_comment = dataSnapshot.child(PostKey).child("uid").getValue().toString();
} else {
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
viewHolder.CommentPostButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent commentsIntent = new Intent(MainFeedActivity.this, CommentsActivity.class);
commentsIntent.putExtra("Post_Key", PostKey);
startActivity(commentsIntent);
}
});
}
};
postList.setAdapter(firebaseRecyclerAdapter);
}
public static class PostsViewHolder extends RecyclerView.ViewHolder {
View mView;
ImageView CommentPostButton;
TextView PostDesc;
String currentUserID;
SquareImageView PostImage;
public PostsViewHolder(View itemView) {
super(itemView);
mView = itemView;
CommentPostButton = (ImageView) mView.findViewById(R.id.speech_bubble);
PostImage = (SquareImageView) mView.findViewById(R.id.post_image);
PostDesc = (TextView) mView.findViewById(R.id.post_description);
try {
currentUserID = FirebaseAuth.getInstance().getCurrentUser().getUid();
} catch (Exception e) {
/* This is a generic Exception handler which means it can handle
* all the exceptions. This will execute if the exception is not
* handled by previous catch blocks.
*/
Toast.makeText(mView.getContext(), "User is not exist", Toast.LENGTH_SHORT).show();
}
}
}
public void setFullname(String fullname) {
TextView username = (TextView) mView.findViewById(R.id.post_user_name);
username.setText(fullname);
}
public void setTime(String time) {
TextView PostTime = (TextView) mView.findViewById(R.id.post_time);
PostTime.setText(" " + time);
}
public void setDate(String date) {
TextView PostDate = (TextView) mView.findViewById(R.id.post_date);
PostDate.setText(" " + date);
}
public void setDescription(String description) {
TextView PostDescription = (TextView) mView.findViewById(R.id.post_description);
PostDescription.setText(description);
}
public void setPostimage(String postimage) {
ImageView PostImage = (ImageView) mView.findViewById(R.id.post_image);
Picasso.get().load(postimage).fit().into(PostImage);
}
}
}
firebase android-recyclerview
I have a very simple recycle view with firebase adapter.
Everythig works fine except one weird thing:
The logic is - when user upload post without any picture - the relevant firebase node updated accordingly to "none", and so - the logic is - in this case, the relevant post will get only 100dp of width and height.
When i open my app - everything works fine, until i get to the bottom of the recycleview and then when i up - the post with pictures also getting smaller to 100dp... why???
Relevant code is below:
public class MainFeedActivity extends AppCompatActivity {
private static final String TAG = "MainFeedActivity";
private RecyclerView postList;
private FloatingActionButton AddNewPostButton, AddNewTextPostButton;
private DatabaseReference UsersRef, PostsRef;
private FirebaseAuth mAuth;
String currentUserID, post_user_uid_for_comment;
private String post_user_uid;
private long PostCount;
private Context mContext;
private int Post_Key_from_anonymus, PostIntKeyFromComment;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_feed);
Log.d(TAG, "onCreate: started.");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
mContext = MainFeedActivity.this;
mAuth = FirebaseAuth.getInstance();
currentUserID = mAuth.getCurrentUser().getUid();
UsersRef = FirebaseDatabase.getInstance().getReference().child("users");
PostsRef = FirebaseDatabase.getInstance().getReference().child("Posts");
AddNewPostButton = (FloatingActionButton) findViewById(R.id.add_new_post_button);
AddNewTextPostButton = (FloatingActionButton) findViewById(R.id.add_new_textpost_button);
//The next code is working on displaying all users posts
postList = (RecyclerView) findViewById(R.id.all_users_post_list);
postList.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainFeedActivity.this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
postList.setLayoutManager(linearLayoutManager);
AddNewPostButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SendUserToPostActivity();
}
});
AddNewTextPostButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SendUserToTextPostActivity();
}
});
DisplayAllUsersPosts();
}
@Override
protected void onPause() {
super.onPause();
finish();
}
private void DisplayAllUsersPosts() {
Query SortPostsInDecendingOrder = PostsRef.orderByChild("userRank");
FirebaseRecyclerAdapter<Posts, PostsViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Posts, PostsViewHolder>
(
Posts.class,
R.layout.all_post_layout,
PostsViewHolder.class,
SortPostsInDecendingOrder
) {
@Override
protected void populateViewHolder(final PostsViewHolder viewHolder, Posts model, int position) {
final String PostKey = getRef(position).getKey();
final int PostIntKey = position; //var for store post position to return back the user here. Should be declare only here!!!!!!
viewHolder.setFullname(model.getFullname());
viewHolder.setTime(model.getTime());
viewHolder.setDate(model.getDate());
viewHolder.setDescription(model.getDescription());
viewHolder.setPostimage(model.getPostimage());
PostsRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild(PostKey)) {
// String fullname=dataSnapshot.child(PostKey).child("fullname").getValue().toString();
// Toast.makeText(MainFeedActivity.this, fullname+"'s post", Toast.LENGTH_SHORT).show();
String degel = "none"; //"https://firebasestorage.googleapis.com/v0/b/bluesky-ec76e.appspot.com/o/country%20icons%2Floading.png?alt=media&token=c69f2f90-0c36-44c7-a8b8-a3e4fce336df";
String deleted = "This message was deleted by user";
if (dataSnapshot.child(PostKey).child("postimage").getValue().toString().equals(degel)
&& !dataSnapshot.child(PostKey).child("description").getValue().toString().equals(deleted)) {
// Toast.makeText(MainFeedActivity.this, "+"+PostKey+"+", Toast.LENGTH_SHORT).show();
int newHeight = 100; // New height in pixels
int newWidth = 100; // New width in pixels
// Apply the new height for ImageView programmatically
viewHolder.PostImage.getLayoutParams().height = newHeight;
// Apply the new width for ImageView programmatically
viewHolder.PostImage.getLayoutParams().width = newWidth;
// Set the scale type for ImageView image scaling
viewHolder.PostImage.setScaleType(ImageView.ScaleType.FIT_XY);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(30, 30);
viewHolder.PostDesc.setTextColor(Color.parseColor("#3700B3"));
viewHolder.PostDesc.setTextSize(35);
viewHolder.PostDesc.setMaxLines(5);
} else if (dataSnapshot.child(PostKey).child("postimage").getValue().toString().equals(degel)
&& dataSnapshot.child(PostKey).child("description").getValue().toString().equals(deleted))
{
//Toast.makeText(MainFeedActivity.this, "+"+PostKey+"+", Toast.LENGTH_SHORT).show();
int newHeight = 100; // New height in pixels
int newWidth = 100; // New width in pixels
// Apply the new height for ImageView programmatically
viewHolder.PostImage.getLayoutParams().height = newHeight;
// Apply the new width for ImageView programmatically
viewHolder.PostImage.getLayoutParams().width = newWidth;
// Set the scale type for ImageView image scaling
viewHolder.PostImage.setScaleType(ImageView.ScaleType.FIT_XY);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(30, 30);
viewHolder.PostDesc.setTextColor(Color.GRAY);
viewHolder.PostDesc.setTextSize(10);
// viewHolder.PostDesc.setMaxLines(5);
} else if (!dataSnapshot.child(PostKey).child("postimage").getValue().toString().equals(degel)
&& !dataSnapshot.child(PostKey).child("description").getValue().toString().equals(deleted))
{
if (viewHolder.PostDesc.getLineCount() == 1) {
viewHolder.PostDesc.setTextColor(Color.BLUE);
viewHolder.PostDesc.setTextSize(35);
} else if (viewHolder.PostDesc.getLineCount() == 2) {
viewHolder.PostDesc.setTextColor(Color.BLUE);
viewHolder.PostDesc.setTextSize(30);
} else if (viewHolder.PostDesc.getLineCount() == 3) {
viewHolder.PostDesc.setTextColor(Color.BLUE);
viewHolder.PostDesc.setTextSize(25);
} else if (viewHolder.PostDesc.getLineCount() == 4) {
viewHolder.PostDesc.setTextColor(Color.BLUE);
viewHolder.PostDesc.setTextSize(20);
} else if (viewHolder.PostDesc.getLineCount() > 4) {
viewHolder.PostDesc.setTextColor(Color.BLUE);
viewHolder.PostDesc.setTextSize(15);
}
}
// post_user_uid_for_comment = dataSnapshot.child(PostKey).child("uid").getValue().toString();
} else {
Toast.makeText(MainFeedActivity.this, "Not Exist", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
PostsRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild(PostKey)) {
post_user_uid_for_comment = dataSnapshot.child(PostKey).child("uid").getValue().toString();
} else {
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
viewHolder.CommentPostButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent commentsIntent = new Intent(MainFeedActivity.this, CommentsActivity.class);
commentsIntent.putExtra("Post_Key", PostKey);
startActivity(commentsIntent);
}
});
}
};
postList.setAdapter(firebaseRecyclerAdapter);
}
public static class PostsViewHolder extends RecyclerView.ViewHolder {
View mView;
ImageView CommentPostButton;
TextView PostDesc;
String currentUserID;
SquareImageView PostImage;
public PostsViewHolder(View itemView) {
super(itemView);
mView = itemView;
CommentPostButton = (ImageView) mView.findViewById(R.id.speech_bubble);
PostImage = (SquareImageView) mView.findViewById(R.id.post_image);
PostDesc = (TextView) mView.findViewById(R.id.post_description);
try {
currentUserID = FirebaseAuth.getInstance().getCurrentUser().getUid();
} catch (Exception e) {
/* This is a generic Exception handler which means it can handle
* all the exceptions. This will execute if the exception is not
* handled by previous catch blocks.
*/
Toast.makeText(mView.getContext(), "User is not exist", Toast.LENGTH_SHORT).show();
}
}
}
public void setFullname(String fullname) {
TextView username = (TextView) mView.findViewById(R.id.post_user_name);
username.setText(fullname);
}
public void setTime(String time) {
TextView PostTime = (TextView) mView.findViewById(R.id.post_time);
PostTime.setText(" " + time);
}
public void setDate(String date) {
TextView PostDate = (TextView) mView.findViewById(R.id.post_date);
PostDate.setText(" " + date);
}
public void setDescription(String description) {
TextView PostDescription = (TextView) mView.findViewById(R.id.post_description);
PostDescription.setText(description);
}
public void setPostimage(String postimage) {
ImageView PostImage = (ImageView) mView.findViewById(R.id.post_image);
Picasso.get().load(postimage).fit().into(PostImage);
}
}
}
firebase android-recyclerview
firebase android-recyclerview
asked Nov 22 '18 at 6:33


Dima BokovDima Bokov
368
368
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425095%2frecycleview-weird-behaviour-with-firebase%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425095%2frecycleview-weird-behaviour-with-firebase%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown