When sending messages to firebase recycler view reading them but recycler view not updating instantly....i have tried notifyDataSetChanged() but not working.................................................................................................................................................................
MessagingActivity.java
public class MessagingActivity extends AppCompatActivity {
RecyclerView recyclerView;
ImageView ivBack, ivSend;
EditText etTypeMessage;
TextView tvName;
String Receiverphone, image, name;
CircleImageView civProfile;
DatabaseReference reference;
MessagingAdapter adapter;
ArrayList<MessagingBinding> list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_messaging);
recyclerView = findViewById(R.id.rv_messagingActivity);
ivBack = findViewById(R.id.ivBack_messagingActivity);
etTypeMessage = findViewById(R.id.etTypeMessage_activityMessaging);
tvName = findViewById(R.id.tvName_activityMessaging);
civProfile = findViewById(R.id.civProfile_messagingActivity);
ivSend = findViewById(R.id.ivSend_activityMessaging);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
image = getIntent().getStringExtra("image");
name = getIntent().getStringExtra("name");
Receiverphone = getIntent().getStringExtra("phone");
tvName.setText(name);
Picasso.get().load(image).into(civProfile);
reference = FirebaseDatabase.getInstance().getReference().child("Users");
ivSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Messages messages = new Messages(etTypeMessage.getText().toString(), name, image, Singleton.obj.phone, Receiverphone);
reference.child("messages").push().setValue(messages);
etTypeMessage.setText("");
adapter.notifyDataSetChanged();
}
});
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MessagingActivity.this);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(linearLayoutManager);
list = new ArrayList<>();
reference.child("messages").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
Messages messages = dataSnapshot.getValue(Messages.class);
if (messages.getSenderphone().equals(Singleton.obj.phone) && messages.getReceiverPhone().equals(Receiverphone)) {
list.add(messages);
}
}
adapter = new MessagingAdapter(list);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
ivBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MessagingActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
}
MessagingAdapter.javastrong text
public class MessagingAdapter extends RecyclerView.Adapter {
private ArrayList<MessagingBinding> bindingList;
public MessagingAdapter(ArrayList<MessagingBinding> bindingList) {
this.bindingList = bindingList;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view;
switch (viewType) {
case (MessagingBinding.TYPE_SENDER):
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_sendermessaging, parent, false);
return new SenderViewHolder(view);
default:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_receivermessaging, parent, false);
return new ReceiverViewHodler(view);
}
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
switch (getItemViewType(position)) {
case (MessagingBinding.TYPE_SENDER):
((SenderViewHolder) holder).bindView(position);
break;
case (MessagingBinding.TYPE_RECEIVER):
((ReceiverViewHodler)holder).bindView(position);
break;
}
}
#Override
public int getItemCount() {
if (bindingList == null) {
return 0;
} else {
return bindingList.size();
}
}
#Override
public int getItemViewType(int position) {
return bindingList.get(position).getType();
}
class SenderViewHolder extends RecyclerView.ViewHolder {
TextView tvSender;
ImageView ivSender;
public SenderViewHolder(#NonNull View itemView) {
super(itemView);
tvSender = itemView.findViewById(R.id.tvSender_listItemsMessaging);
ivSender = itemView.findViewById(R.id.ivSender_listItemMessaging);
}
void bindView(int position) {
SenderMessaging sender = (SenderMessaging) bindingList.get(position);
tvSender.setText(sender.getTvSenderMsg());
ivSender.setImageResource(sender.getImg());
}
}
//-------------------------------ReceiverViewHolder----------------------------------
class ReceiverViewHodler extends RecyclerView.ViewHolder {
TextView tvReceiver;
public ReceiverViewHodler(#NonNull View itemView) {
super(itemView);
tvReceiver = itemView.findViewById(R.id.tvReceiver_listItemsMessaging);
}
void bindView(int position) {
Messages receiver = (Messages) bindingList.get(position);
tvReceiver.setText(receiver.getMessage());
}
}
}
From your code, your messages should be visible when firebase notify you with changes in your db.
Please, try next code:
MessagingActivity
public class MessagingActivity extends AppCompatActivity {
RecyclerView recyclerView;
ImageView ivBack, ivSend;
EditText etTypeMessage;
TextView tvName;
String Receiverphone, image, name;
CircleImageView civProfile;
DatabaseReference reference;
MessagingAdapter adapter;
ArrayList<MessagingBinding> list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_messaging);
recyclerView = findViewById(R.id.rv_messagingActivity);
ivBack = findViewById(R.id.ivBack_messagingActivity);
etTypeMessage = findViewById(R.id.etTypeMessage_activityMessaging);
tvName = findViewById(R.id.tvName_activityMessaging);
civProfile = findViewById(R.id.civProfile_messagingActivity);
ivSend = findViewById(R.id.ivSend_activityMessaging);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
image = getIntent().getStringExtra("image");
name = getIntent().getStringExtra("name");
Receiverphone = getIntent().getStringExtra("phone");
tvName.setText(name);
Picasso.get().load(image).into(civProfile);
reference = FirebaseDatabase.getInstance().getReference().child("Users");
ivSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Messages messages = new Messages(etTypeMessage.getText().toString(), name, image, Singleton.obj.phone, Receiverphone);
reference.child("messages").push().setValue(messages);
etTypeMessage.setText("");
}
});
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MessagingActivity.this);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(linearLayoutManager);
list = new ArrayList<>();
reference.child("messages").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
Messages messages = dataSnapshot.getValue(Messages.class);
if (messages.getSenderphone().equals(Singleton.obj.phone) && messages.getReceiverPhone().equals(Receiverphone)) {
list.add(messages);
}
}
adapter = new MessagingAdapter(list);
recyclerView.setAdapter(adapter);
adapter.updateList(list);
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
ivBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MessagingActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
And the Adapter:
public class MessagingAdapter extends RecyclerView.Adapter {
private ArrayList<MessagingBinding> bindingList;
public MessagingAdapter(ArrayList<MessagingBinding> bindingList) {
this.bindingList = bindingList;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view;
switch (viewType) {
case (MessagingBinding.TYPE_SENDER):
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_sendermessaging, parent, false);
return new SenderViewHolder(view);
default:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_receivermessaging, parent, false);
return new ReceiverViewHodler(view);
}
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
switch (getItemViewType(position)) {
case (MessagingBinding.TYPE_SENDER):
((SenderViewHolder) holder).bindView(position);
break;
case (MessagingBinding.TYPE_RECEIVER):
((ReceiverViewHodler)holder).bindView(position);
break;
}
}
#Override
public int getItemCount() {
if (bindingList == null) {
return 0;
} else {
return bindingList.size();
}
}
public void updateList(ArrayList<MessagingBinding> messages){
this.bindingList = messages;
this.notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) {
return bindingList.get(position).getType();
}
class SenderViewHolder extends RecyclerView.ViewHolder {
TextView tvSender;
ImageView ivSender;
public SenderViewHolder(#NonNull View itemView) {
super(itemView);
tvSender = itemView.findViewById(R.id.tvSender_listItemsMessaging);
ivSender = itemView.findViewById(R.id.ivSender_listItemMessaging);
}
void bindView(int position) {
SenderMessaging sender = (SenderMessaging) bindingList.get(position);
tvSender.setText(sender.getTvSenderMsg());
ivSender.setImageResource(sender.getImg());
}
}
//-------------------------------ReceiverViewHolder----------------------------------
class ReceiverViewHodler extends RecyclerView.ViewHolder {
TextView tvReceiver;
public ReceiverViewHodler(#NonNull View itemView) {
super(itemView);
tvReceiver = itemView.findViewById(R.id.tvReceiver_listItemsMessaging);
}
void bindView(int position) {
Messages receiver = (Messages) bindingList.get(position);
tvReceiver.setText(receiver.getMessage());
}
}
}
Related
I'm trying to print the position of RecyclerView in logcat when I click on the list of recyclerView, but it's not printing anything. Actually in this app, if I click on a position of the recyclerview, that should take me to new activity and grab all the details of that position on which I clicked.
Here is my ImageAdapter.java class
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {
private Context mContext;
private List<UserDetails> mUploads;
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}
public ImageAdapter(Context context, List<UserDetails> uploads) {
mContext = context;
mUploads = uploads;
}
#NonNull
#Override
public ImageViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(mContext).inflate(R.layout.user_display, viewGroup, false);
return new ImageViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull ImageViewHolder imageViewHolder, int i) {
UserDetails currentItem = mUploads.get(i);
UserDetails userDetails = mUploads.get(i);
imageViewHolder.requestId.setText("Request Id : " + userDetails.getMobileNumber());
imageViewHolder.customerName.setText(userDetails.getName());
imageViewHolder.customerMobile.setText("Mobile : " + userDetails.getMobileNumber());
imageViewHolder.customerAddress.setText(userDetails.getAddress());
}
#Override
public int getItemCount() {
return mUploads.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder {
public TextView requestId, customerName, customerAddress, customerMobile, serviceType, date;
public ImageViewHolder(#NonNull View itemView) {
super(itemView);
requestId = itemView.findViewById(R.id.requestId);
customerName = itemView.findViewById(R.id.customerName);
customerAddress = itemView.findViewById(R.id.customerAddress);
customerMobile = itemView.findViewById(R.id.customerMobile);
serviceType = itemView.findViewById(R.id.serviceType);
date = itemView.findViewById(R.id.date);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mListener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
mListener.onItemClick(position);
}
}
}
});
}
}
}
and here is the recyclerView class
public class requestList extends AppCompatActivity implements ImageAdapter.OnItemClickListener {
RecyclerView recyclerView;
DatabaseReference databaseReference;
List<UserDetails> downloadDataArray;
ImageAdapter adapter;
ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_request_list);
progressBar = findViewById(R.id.progressBar);
Toolbar toolbar = findViewById(R.id.roServiceToolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Requests");
toolbar.setNavigationIcon(R.drawable.back);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(requestList.this, customerRequests.class));
}
});
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
downloadDataArray = new ArrayList<>();
databaseReference = FirebaseDatabase.getInstance().getReference("Service Request/RO Service");
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
downloadDataArray.clear();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
UserDetails userDetails = postSnapshot.getValue(UserDetails.class);
downloadDataArray.add(userDetails);
}
adapter = new ImageAdapter(requestList.this, downloadDataArray);
getSupportActionBar().setTitle("Requests" + "(" +adapter.getItemCount()+ ")");
recyclerView.setAdapter(adapter);
progressBar.setVisibility(View.INVISIBLE);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
#Override
public void onItemClick(int position) {
Log.i("Position", String.valueOf(position));
}
}
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mListener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
//print log message
}
}
}
});
Because you are not assigning the value of mListener. Add this line after initialize adapter object.
adapter.setOnItemClickListener(this)
I am using a recyclerview and inside its adapter I have another recyclerview.
How to prevent item change and item refresh when an item is updated.
This is creating visual glitches and wrong positioning of the items
I have tried to read directly from a database reference on the current position of the holder.
Result: https://media.giphy.com/media/j6TpXZRE6e44cMZVIP/giphy.gif
public class CommentsAdapter extends RecyclerView.Adapter<CommentsAdapter.ViewHolder>{
private Context mContext;
private List<Comments> mComments;
private List<Replies> allReplies;
public CommentsAdapter(Context mContext, List<Comments> mComments, List<Replies> allReplies )
{
this.mContext = mContext;
this.mComments = mComments;
this.allReplies = allReplies;
}
#NonNull
#Override
public CommentsAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(mContext).inflate(R.layout.all_comments_layout, viewGroup, false);
return new CommentsAdapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, final int position)
{
final Comments comments = mComments.get(position);
holder.like.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CommentUps.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
{
if (dataSnapshot.child(PostKey).child(com_uid).hasChild(currentUserID)) {
CommentUps.child(PostKey).child(com_uid).child(currentUserID).removeValue();
CommentsRef.child(PostKey).child(com_uid).child("likes").setValue(comments.getLikes() - 1);
} else {
CommentUps.child(PostKey).child(com_uid).child(currentUserID).setValue(true);
CommentsRef.child(PostKey).child(com_uid).child("likes").setValue(comments.getLikes() + 1);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
});
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Replies");
//reference.orderByChild("pLikes")
reference.orderByChild(com_uid).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot)
{
holder.repliesList.clear();
for (DataSnapshot snapshot : dataSnapshot.child(PostKey).child(com_uid).getChildren())
{
Replies replies = snapshot.getValue(Replies.class);
//final String com = comment.toString();
// Log.d(new String("Comments"), com);
//for (String id : followingList)
if (replies.getComment_uid().equals(com_uid))
{
holder.repliesList.add(replies);
}
}
holder.repliesAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
#Override
public int getItemCount() {
return mComments.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageButton like
List<Replies> repliesList;
RepliesAdapter repliesAdapter;
public ViewHolder(View itemView) {
super(itemView);
like = itemView.findViewById(R.id.comment_like_button);
repliesRecycler = itemView.findViewById(R.id.all_replies_recycler);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
repliesRecycler.setLayoutManager(linearLayoutManager);
repliesRecycler.setHasFixedSize(true);
repliesList = new ArrayList<>();
repliesAdapter = new RepliesAdapter(mContext, repliesList);
repliesRecycler.setAdapter(repliesAdapter);
repliesAdapter.notifyDataSetChanged();
}
// Second Method - Same Result
private void displayReplies(final String com_uid ,final String PostKey, final boolean locked) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Replies");
reference.child(PostKey).child(com_uid).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
repliesList.clear();
if (dataSnapshot.exists()) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
final Replies replies = snapshot.getValue(Replies.class);
if (replies.getComment_uid().equals(com_uid)) {
repliesList.add(replies);
repliesAdapter.notifyDataSetChanged();
}
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
My replies adapter:
public class RepliesAdapter extends RecyclerView.Adapter<RepliesAdapter.ViewHolder>
{
public Context mContext;
public List<Replies> mReplies;
public RepliesAdapter(Context mContext, List<Replies> mReplies)
{
this.mContext = mContext;
this.mReplies = mReplies;
}
#NonNull
#Override
public RepliesAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(mContext).inflate(R.layout.all_replies_layout, viewGroup, false);
mAuth = FirebaseAuth.getInstance();
return new RepliesAdapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, final int position)
{
final Replies replies = mReplies.get(position);
holder.myUserName.setText(replies.getUsername());
holder.myComment.setText(replies.getReply());
holder.myTime.setText(replies.getTime());
}
#Override
public int getItemCount() {
return mReplies.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView myUserName, myComment,myTime;
public ViewHolder(View itemView) {
super(itemView);
myUserName = itemView.findViewById(R.id.reply_username);
myComment = itemView.findViewById(R.id.reply_text);
myTime = itemView.findViewById(R.id.reply_time);
}
}
I have implement a Listener for my database. The listener is being called but my menuAdapter is not receiving my values.
I don't know why this is happening.
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("Menu/思慕雪");
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
listStr.add(String.valueOf(dataSnapshot.getKey()));
picUrl.add(dataSnapshot.child("pic").getValue().toString().trim());
m_price.add(dataSnapshot.child("M").getValue().toString().trim());
l_price.add(dataSnapshot.child("L").getValue().toString().trim());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
menuAdapter menuAdapter = new menuAdapter(listStr,picUrl,m_price,l_price);
recyclerView.setAdapter(menuAdapter);
This is my menuAdapter codes.
public class menuAdapter extends RecyclerView.Adapter {
private List<String> mListString;
private List<String> mPicUrl;
private List<String> mMPrice;
private List<String> mLPrice;
public class ViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
ImageView mImgView;
TextView mTxt;
TextView m_MPrice;
TextView m_LPrice;
ViewHolder(View itemView) {
super(itemView);
mImgView = (ImageView) itemView.findViewById(R.id.drink_pic);
mTxt = (TextView) itemView.findViewById(R.id.drink_name);
m_MPrice = (TextView) itemView.findViewById(R.id.drink_m_price);
m_LPrice = (TextView) itemView.findViewById(R.id.drink_l_price);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
Toast.makeText(view.getContext(),
mListString.get(getAdapterPosition()), Toast.LENGTH_LONG)
.show();
}
}
public menuAdapter(List<String> listString,List<String> picUrl,List<String> m_price,List<String> l_price) {
mListString = listString;
mPicUrl=picUrl;
mMPrice=m_price;
mLPrice=l_price;
}
#Override
public menuAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(
R.layout.menu_card, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
new downloadImageTask(holder.mImgView)
.execute(mPicUrl.get(position));
holder.mTxt.setText(mListString.get(position));
if(!mMPrice.get(position).equals("0")){
holder.m_MPrice.setText("M: $"+mMPrice.get(position));
}else{
holder.m_MPrice.setText("--");
}
if(!mLPrice.get(position).equals("0")){
holder.m_LPrice.setText("L: $"+mLPrice.get(position));
}else{
holder.m_LPrice.setText("--");
}
}
#Override
public int getItemCount() {
return mListString.size();
}
}
I am currently working on a chat app. In chat activity, I wanted to show profile picture only on the first item and when I used Runnable (onItemChanged) it slowed down my recycler view very much and now it looks like I added motion blur to my recycler view and it is very very lazy.
Here is a gif of my chat activity:
https://i.gyazo.com/f0489ca7fe9ad28519061c0ff8963fb4.gif
Im not actually sure that it is Runnable that slows down my recycler view but when I remove :
if (i > 0)
if (mMessageList.get(i - 0).getFrom().equals(mMessageList.get(i).getFrom())) {
makeDPInvisible(i - 0);
}
if (invisibleIds.contains(i)) {
viewHolder.displayName.setVisibility(View.GONE);
viewHolder.profileImage.setVisibility(View.INVISIBLE);
}
and
private void makeDPInvisible(final int i) {
invisibleIds.add(i);
recyclerView.post(new Runnable(){
#Override
public void run() {
notifyItemRangeChanged(i);
}
});
}
my recycler is very smooth and without any problems...
Here is my MessaageAdapter.java
/**
* Created by Aryojj on 24/07/17.
*/
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder>{
private FirebaseAuth mAuth;
private ArrayList<Integer> invisibleIds = new ArrayList<>();
private RecyclerView recyclerView;
private List<Messages> mMessageList;
private DatabaseReference mUserDatabase;
public MessageAdapter(List<Messages> mMessageList) {
this.mMessageList = mMessageList;
}
#Override
public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.message_single_layout ,parent, false);
mAuth = FirebaseAuth.getInstance();
return new MessageViewHolder(v);
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
this.recyclerView = recyclerView;
}
public class MessageViewHolder extends RecyclerView.ViewHolder {
public TextView messageText;
public CircleImageView profileImage;
public TextView displayName;
public ImageView messageImage;
public MessageViewHolder(View view) {
super(view);
messageText = (TextView) view.findViewById(R.id.message_text_layout);
profileImage = (CircleImageView) view.findViewById(R.id.message_profile_layout);
displayName = (TextView) view.findViewById(R.id.name_text_layout);
messageImage = (ImageView) view.findViewById(R.id.message_image_layout);
}
}
#Override
public void onBindViewHolder(final MessageViewHolder viewHolder, int i) {
String message_sender_id = mAuth.getCurrentUser().getUid();
Messages c = mMessageList.get(i);
String from_user = c.getFrom();
String message_type = c.getType();
if (i > 0)
if (mMessageList.get(i - 0).getFrom().equals(mMessageList.get(i).getFrom())) {
makeDPInvisible(i - 0);
}
if (invisibleIds.contains(i)) {
viewHolder.displayName.setVisibility(View.GONE);
viewHolder.profileImage.setVisibility(View.INVISIBLE);
}
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(from_user);
mUserDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("name").getValue().toString();
String image = dataSnapshot.child("thumb_image").getValue().toString();
viewHolder.displayName.setText(name);
Picasso.with(viewHolder.profileImage.getContext()).load(image)
.placeholder(R.drawable.default_avatar).into(viewHolder.profileImage);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
if (message_type.equals("text")) {
viewHolder.messageText.setText(c.getMessage());
viewHolder.messageImage.setVisibility(View.INVISIBLE);
} else {
viewHolder.messageText.setVisibility(View.INVISIBLE);
Picasso.with(viewHolder.profileImage.getContext()).load(c.getMessage())
.networkPolicy(NetworkPolicy.OFFLINE).placeholder(R.drawable.default_avatar).into(viewHolder.messageImage);
}
}
private void makeDPInvisible(final int i) {
invisibleIds.add(i);
recyclerView.post(new Runnable(){
#Override
public void run() {
notifyItemRangeChanged(i);
}
});
}
#Override
public int getItemCount() {
return mMessageList.size();
}
}
I show list of data in card view successfully but now i want to open another activity and show details of clicked data.
I have three class MainActivity, ContactAdapter, Contact.
Main Activity Code:
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
String[] name,email;
ArrayList<Contact> list = new ArrayList<Contact>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
name = getResources().getStringArray(R.array.person_name);
email = getResources().getStringArray(R.array.person_email);
int count = 0;
for (String Name : name)
{
Contact contact = new Contact(Name,email[count]);
count++;
list.add(contact);
}
recyclerView= (RecyclerView) findViewById(R.id.recycler_view);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
adapter=new ContactAdapter(list);
recyclerView.setAdapter(adapter);
i tried but it's not working.
Adapter Code:
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactViewHolder> {
ArrayList<Contact> contacts = new ArrayList<Contact>();
public ContactAdapter(ArrayList<Contact> contacts) {
this.contacts = contacts;
}
#Override
public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_layout, parent, false);
ContactViewHolder contactViewHolder = new ContactViewHolder(view);
return contactViewHolder;
}
#Override
public void onBindViewHolder(ContactViewHolder holder, int position) {
Contact CON = contacts.get(position);
holder.person_name.setText(CON.getName());
holder.person_email.setText(CON.getEmail());
}
#Override
public int getItemCount() {
return contacts.size();
}
public static class ContactViewHolder extends RecyclerView.ViewHolder {
TextView person_name, person_email;
public ContactViewHolder(View view) {
super(view);
person_name = (TextView) view.findViewById(R.id.person_name);
person_email = (TextView) view.findViewById(R.id.person_email);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "OnClick Version :",
Toast.LENGTH_SHORT).show();
}
});
view.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Toast.makeText(v.getContext(), "OnLongClick Version :",
Toast.LENGTH_SHORT).show();
return true;
}
});
}
}
}
In your adapter do this:
public class ProdutoAdapter extends RecyclerView.Adapter<ProdutoAdapter.ProdutoViewHolder>{
private static ProdutoAdapter.ClickListener clickListener; //this is important
private List<Produto> produtos; //this is my list of beans
public ProdutoAdapter(List<Produto> produtos) {
this.produtos = produtos;
}
#Override
public ProdutoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_produtos, parent, false);
ProdutoViewHolder mvh = new ProdutoViewHolder(v);
return mvh;
}
#Override
public void onBindViewHolder(ProdutoViewHolder holder, int position) {
//holder.nome.setText(this.produtos.get(position).getNome());
holder.img.setImageDrawable(produtos.get(position).getImgDrawable());
holder.nome.setText(produtos.get(position).getNome());
holder.preco.setText(String.valueOf(produtos.get(position).getPreco()));
}
#Override
public int getItemCount() {
return this.produtos.size();
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public static class ProdutoViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{
Produto produto;
CardView cardView;
TextView nome;
TextView preco;
ImageView img;
ProdutoViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
cardView = (CardView) itemView.findViewById(R.id.cardviewProduto);
nome = (TextView) itemView.findViewById(R.id.nome);
preco = (TextView) itemView.findViewById(R.id.preco);
img = (ImageView) itemView.findViewById(R.id.foto);
}
public void setItem(Produto produto){
this.produto = produto;
}
public Produto getItem(){
return this.produto;
}
//this is what you'll need in your activity
#Override
public void onClick(View v) {
clickListener.onItemClick(getAdapterPosition(), v);
}
#Override
public boolean onLongClick(View v) {
clickListener.onItemLongClick(getAdapterPosition(), v);
return false;
}
}
public void setOnItemClickListener(ClickListener clickListener){
ProdutoAdapter.clickListener = clickListener;
}
public interface ClickListener {
void onItemClick(int position, View v);
void onItemLongClick(int position, View v);
}
}
and in your activity:
public static ProdutoAdapter mAdapter;
myActivity.mAdapter.setOnItemClickListener(new ProdutoAdapter.ClickListener() {
#Override
public void onItemClick(int position, View v) {
//do what you want
}
#Override
public void onItemLongClick(int position, View v) {
}
});
myActivity.mRecyclerView.setAdapter(mAdapter);