Chat RecyclerView is blank, Showing nothing but Data is being fetched - android

I am trying to show Recyclerview in chat form, but it is blank can't find the issue here is the code, I don't know where the issue is but it might be in adapter.
MessageListAdapter.java
public class MessageListAdapter extends RecyclerView.Adapter {
private static final int VIEW_TYPE_MESSAGE_SENT = 1;
private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2;
private Context mContext;
private List<ItemMessage> mMessageList;
public MessageListAdapter(Context context, List<ItemMessage> messageList) {
mContext = context;
mMessageList = messageList;
}
#Override
public int getItemCount() {
return mMessageList.size();
}
// Determines the appropriate ViewType according to the sender of the message.
#Override
public int getItemViewType(int position) {
ItemMessage message = mMessageList.get(position);
if (message.getSender().equals(FirebaseAuth.getInstance().getCurrentUser().getUid())) {
// If the current user is the sender of the message
return VIEW_TYPE_MESSAGE_SENT;
} else {
// If some other user sent the message
return VIEW_TYPE_MESSAGE_RECEIVED;
}
}
// Inflates the appropriate layout according to the ViewType.
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
if (viewType == VIEW_TYPE_MESSAGE_SENT) {
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_message_sent, parent, false);
return new SentMessageHolder(view);
} else if (viewType == VIEW_TYPE_MESSAGE_RECEIVED) {
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_message_received, parent, false);
return new ReceivedMessageHolder(view);
}
return null;
}
// Passes the message object to a ViewHolder so that the contents can be bound to UI.
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ItemMessage message = mMessageList.get(position);
switch (holder.getItemViewType()) {
case VIEW_TYPE_MESSAGE_SENT:
((SentMessageHolder) holder).bind(message);
break;
case VIEW_TYPE_MESSAGE_RECEIVED:
((ReceivedMessageHolder) holder).bind(message);
}
}
private class SentMessageHolder extends RecyclerView.ViewHolder {
TextView messageText, timeText;
SentMessageHolder(View itemView) {
super(itemView);
messageText = (TextView) itemView.findViewById(R.id.text_message_body);
timeText = (TextView) itemView.findViewById(R.id.text_message_time);
}
void bind(ItemMessage message) {
messageText.setText(message.getMessgae());
// Format the stored timestamp into a readable String using method.
timeText.setText(message.getTime());
}
}
private class ReceivedMessageHolder extends RecyclerView.ViewHolder {
TextView messageText, timeText, nameText;
ImageView profileImage;
ReceivedMessageHolder(View itemView) {
super(itemView);
messageText = (TextView) itemView.findViewById(R.id.text_message_body);
timeText = (TextView) itemView.findViewById(R.id.text_message_time);
nameText = (TextView) itemView.findViewById(R.id.text_message_name);
profileImage = (ImageView) itemView.findViewById(R.id.image_message_profile);
}
void bind(ItemMessage message) {
messageText.setText(message.getMessgae());
// Format the stored timestamp into a readable String using method.
timeText.setText(message.getTime());
nameText.setText("kdkdk");
// Insert the profile image from the URL into the ImageView.
// Utils.displayRoundImageFromUrl(mContext, message.getSender().getProfileUrl(), profileImage);
}
}
}
ItemMessage.java
public class ItemMessage {
private String sender = null;
private String reciever = null;
private String time = null;
private String messgae = null;
public ItemMessage() {
}
public ItemMessage(String sender, String reciever, String time, String messgae) {
this.sender = sender;
this.reciever = reciever;
this.time = time;
this.messgae = messgae;
}
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public String getReciever() {
return reciever;
}
public void setReciever(String reciever) {
this.reciever = reciever;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getMessgae() {
return messgae;
}
public void setMessgae(String messgae) {
this.messgae = messgae;
}
}
fetching code.
childEventListener = new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
ItemMessage itemMessage = dataSnapshot.getValue(ItemMessage.class);
friendlyMessages.add(itemMessage);
mMessageAdapter.notifyDataSetChanged();
Log.d("SSSSz" , friendlyMessages.size()+"");
Log.d("SSSSz", itemMessage.getMessgae());
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
};
databaseReferencemsg.addChildEventListener(childEventListener);
I am not posting rest of the code because I know issue is here.

Related

RecyclerView with different types and different adapter

and thank you for your attention to my request.
I'm a newbie on Andorid Studio and I'm developing an App, it has to show a list of Events, Courses and News. There is a DrawerLayout which allow navigating into Event's, course's and news's area, each area has each specific RecyclerView, but into the home activity, I have to show all the items (Event, Course and News).
I create an Adapter for each item (because they have different properties) and each section (fragment) works properly but now I'm stuck with the main activity (the home).
Here are the models for explaining the difference between them:
Event
public class Event {
private int id;
private String title;
private String description;
private LocalDateTime startingDate;
private LocalDateTime endingDate;
private String cost;
private String website;
private Category category;
private Venue venue;
private Organizer organizer;
private String status;
private int resImage;
public Event(int id, String title, String description, LocalDateTime startingDate, LocalDateTime endingDate, String cost, String website, Category category, Venue venue, Organizer organizer, int image) {
this.id = id;
this.title = title;
this.description = description;
this.startingDate = startingDate;
this.endingDate = endingDate;
this.cost = cost;
this.website = website;
this.category = category;
this.venue = venue;
this.organizer = organizer;
this.resImage = image;
}
public Event(){}
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public void setTitle(String name) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public LocalDateTime getStartingDate() {
return startingDate;
}
public void setStartingDate(LocalDateTime startingDate) {
this.startingDate = startingDate;
}
public LocalDateTime getEndingDate() {
return endingDate;
}
public void setEndingDate(LocalDateTime endingDate) {
this.endingDate = endingDate;
}
public String getCost() {
return cost;
}
public void setCost(String cost) {
this.cost = cost;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
public Category getCategory() {
return category;
}
public Venue getVenue() {
return venue;
}
public Organizer getOrganizer() {
return organizer;
}
public String getStatus() {
return status;
}
public int getResImage() {
return resImage;
}
}
News:
public class News {
private int id;
private String title;
private String content;
private int resImage;
public News(int id, String title, String content, int image) {
this.id = id;
this.title = title;
this.content = content;
this.resImage = image;
}
public News(){}
public int getId(){
return this.id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setDescription(String description) {
this.content = content;
}
public int getResImage() {
return resImage;
}
public void setResImage(int resImage) {
this.resImage = resImage;
}
}
Courses
public Course(int id, String title, String content, float price, LocalDate startingDate, int availablePlace, Teacher teacher, CourseCategory courseCategory, Location location, Duration duration, Level level, int resImage) {
this.id = id;
this.title = title;
this.content = content;
this.price = price;
this.startingDate = startingDate;
this.availablePlace = availablePlace;
this.teacher = teacher;
this.courseCategory = courseCategory;
this.location = location;
this.duration = duration;
this.level = level;
this.resImage = resImage;
}
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public LocalDate getStartingDate() {
return startingDate;
}
public void setStartingDate(LocalDate startingDate) {
this.startingDate = startingDate;
}
public int getAvailablePlace() {
return availablePlace;
}
public void setAvailablePlace(int availablePlace) {
this.availablePlace = availablePlace;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
public CourseCategory getCourseCategory() {
return courseCategory;
}
public void setCourseCategory(CourseCategory courseCategory) {
this.courseCategory = courseCategory;
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
public Duration getDuration() {
return duration;
}
public void setDuration(Duration duration) {
this.duration = duration;
}
public Level getLevel() {
return level;
}
public int getResImage() {
return resImage;
}
public void setResImage(int resImage) {
this.resImage = resImage;
}
}
Here is the Adapters:
EventAdapter
public class EventViewAdapter extends RecyclerView.Adapter<EventViewAdapter.EventViewHolder> {
ArrayList<Event> eventList;
Context context;
public EventViewAdapter(ArrayList<Event> eventList, Context context) {
this.eventList = eventList;
this.context = context;
}
public EventViewAdapter() {
}
public static class EventViewHolder extends RecyclerView.ViewHolder {
ImageView eventImage;
TextView startingDate, place, title;
LinearLayout cellLayout;
EventViewHolder(View eventView) {
super(eventView);
eventImage = eventView.findViewById(R.id.event_image);
startingDate = eventView.findViewById(R.id.event_starting_date);
place = eventView.findViewById(R.id.event_place);
title = eventView.findViewById(R.id.event_title);
cellLayout = eventView.findViewById(R.id.event_cell_layout);
}
}
#NonNull
#Override
public EventViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.event_cell_def, parent, false);
EventViewHolder evh = new EventViewHolder(view);
return evh;
}
#Override
public void onBindViewHolder(#NonNull EventViewHolder holder, int position) {
final String title = eventList.get(position).getTitle();
final String description = eventList.get(position).getDescription();
final String place = eventList.get(position).getVenue().getCity();
final String address = eventList.get(position).getVenue().getAddress();
final String startDate = String.valueOf(eventList.get(position).getStartingDate().toLocalDate());
final String startTime = String.valueOf(eventList.get(position).getStartingDate().getHour() + ":" + String.valueOf(eventList.get(position).getStartingDate().getMinute()));
final String endDate = String.valueOf(eventList.get(position).getEndingDate().toLocalDate());
final String cost = eventList.get(position).getCost();
final String website = eventList.get(position).getWebsite();
final String category = eventList.get(position).getCategory().getName();
final String organizer = eventList.get(position).getOrganizer().getOrganizer();
final String organizerWebsite = eventList.get(position).getOrganizer().getWebsite();
final int image = eventList.get(position).getResImage();
holder.eventImage.setImageResource(image);
holder.startingDate.setText(startDate);
holder.place.setText(place);
holder.title.setText(title);
holder.cellLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), DetailEventActivity.class);
intent.putExtra("title", title);
intent.putExtra("description", description);
intent.putExtra("place", place);
intent.putExtra("address", address);
intent.putExtra("startDate", startDate);
intent.putExtra("startTime", startTime);
intent.putExtra("endDate", endDate);
intent.putExtra("cost", cost);
intent.putExtra("website", website);
intent.putExtra("category", category);
intent.putExtra("organizer", organizer);
intent.putExtra("orgWebsite", organizerWebsite);
intent.putExtra("image", image);
v.getContext().startActivity(intent);
Toast.makeText(context, "Hai premuto su: " + title, Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return eventList.size();
}
public void eventSetSearchOperation(ArrayList<Event> newList){
eventList = new ArrayList<>();
eventList.addAll(newList);
notifyDataSetChanged();
}
}
CourseAdapter:
public class CourseViewAdapter extends RecyclerView.Adapter<CourseViewAdapter.CourseViewHolder> {
ArrayList<Course> courseList;
Context context;
public CourseViewAdapter(ArrayList<Course> courseList, Context context) {
this.courseList = courseList;
this.context = context;
}
public CourseViewAdapter(){}
public static class CourseViewHolder extends RecyclerView.ViewHolder {
ImageView courseImage;
TextView startingDate, place, title;
LinearLayout courseLinear;
CourseViewHolder(View courseView){
super(courseView);
courseImage = courseView.findViewById(R.id.course_image);
startingDate = courseView.findViewById(R.id.course_starting_date);
place = courseView.findViewById(R.id.course_place);
title = courseView.findViewById(R.id.course_title);
courseLinear = courseView.findViewById(R.id.course_cell_layout);
}
}
#NonNull
#Override
public CourseViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.course_cell_def, parent, false);
CourseViewHolder cvh = new CourseViewHolder(view);
return cvh;
}
#Override
public void onBindViewHolder(#NonNull CourseViewHolder holder, int position) {
final String title = courseList.get(position).getTitle();
final String category = courseList.get(position).getCourseCategory().getName();
final String description = courseList.get(position).getContent();
final String place = courseList.get(position).getLocation().getName();
final String startDate = String.valueOf(courseList.get(position).getStartingDate());
final int availablePlace = courseList.get(position).getAvailablePlace();
final String teacher = courseList.get(position).getTeacher().getName();
final String teacherEmail = courseList.get(position).getTeacher().getEmail();
final String teacherMobile = courseList.get(position).getTeacher().getPhone();
final String duration = courseList.get(position).getDuration().getName();
final String level = courseList.get(position).getLevel().getName();
final String price = String.valueOf(courseList.get(position).getPrice());
final int image = courseList.get(position).getResImage();
holder.courseImage.setImageResource(image);
holder.startingDate.setText(startDate);
holder.place.setText(place);
holder.title.setText(title);
holder.courseLinear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), DetailCourseActivity.class);
intent.putExtra("title", title);
intent.putExtra("category", category);
intent.putExtra("content", description);
intent.putExtra("startDate", startDate);
intent.putExtra("availablePlace", availablePlace);
intent.putExtra("teacher", teacher);
intent.putExtra("teacherEmail", teacherEmail);
intent.putExtra("teacherMobile", teacherMobile);
intent.putExtra("duration", duration);
intent.putExtra("price", price);
intent.putExtra("place", place);
intent.putExtra("level", level);
intent.putExtra("image", image);
v.getContext().startActivity(intent);
Toast.makeText(context, "Hai premuto su: " + title, Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return courseList.size();
}
public void courseSetSearchOperation(ArrayList<Course> newList){
courseList = new ArrayList<>();
courseList.addAll(newList);
notifyDataSetChanged();
}
}
News:
public class NewsViewAdapter extends RecyclerView.Adapter<NewsViewAdapter.NewsViewHolder> {
ArrayList<News> newsList;
Context context;
public NewsViewAdapter(ArrayList<News> newsList, Context context) {
this.newsList = newsList;
this.context = context;
}
public NewsViewAdapter(){}
public static class NewsViewHolder extends RecyclerView.ViewHolder {
ImageView newsImage;
TextView newsTitle, newsContent;
LinearLayout newsLayout;
NewsViewHolder(View newView){
super(newView);
newsImage = newView.findViewById(R.id.news_image);
newsTitle = newView.findViewById(R.id.news_title);
newsContent = newView.findViewById(R.id.news_content);
newsLayout = newView.findViewById(R.id.news_cell_layout);
}
}
#NonNull
#Override
public NewsViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_cell_def, parent, false);
NewsViewHolder nvh = new NewsViewHolder(view);
return nvh;
}
#Override
public void onBindViewHolder(#NonNull NewsViewHolder holder, int position) {
final String title = newsList.get(position).getTitle();
final String content = newsList.get(position).getContent();
final int image = newsList.get(position).getResImage();
holder.newsImage.setImageResource(image);
holder.newsTitle.setText(title);
holder.newsContent.setText(content);
holder.newsLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), DetailNewsActivity.class);
intent.putExtra("title", title);
intent.putExtra("content", content);
intent.putExtra("image", image);
v.getContext().startActivity(intent);
Toast.makeText(context, "Premuto su: " + title, Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return newsList.size();
}
public void newsSetSearchOperation(ArrayList<News> newList){
newsList = new ArrayList<>();
newsList.addAll(newList);
notifyDataSetChanged();
}
}
Each specific list of item is into a fragment, indeed here is the Main Activity (home), where I would like to display a RecyclerView of all the itmes:
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private NavigationView mNavigationView;
private Toolbar toolbar;
private ActionBarDrawerToggle drawerToggle;
private FloatingActionButton fab;
private RecyclerView mRecyclerView;
private ImageView openFilter;
private SearchView searchView;
private DataMock dataMock = new DataMock();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.recycler_view_main);
drawerLayout = findViewById(R.id.drawer_layout);
mNavigationView = findViewById(R.id.nav_view);
toolbar = findViewById(R.id.toolbar);
openFilter = findViewById(R.id.filter_icon);
searchView = findViewById(R.id.action_search);
fab = findViewById(R.id.fab);
fab.show();
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
searchView.setQueryHint(getString(R.string.search_hint));
drawerToggle = setupDrawerToggle();
drawerToggle.setDrawerIndicatorEnabled(true);
drawerToggle.syncState();
drawerLayout.setDrawerListener(drawerToggle);
drawerLayout.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
});
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
Fragment nextFragment;
switch (menuItem.getItemId()){
case R.id.event_list:
nextFragment = new EventFragment();
break;
case R.id.course_list:
nextFragment = new CourseFragment();
break;
case R.id.news_list:
nextFragment = new NewsFragment();
break;
default:
throw new IllegalArgumentException("No Fragment for the given menu item");
}
if (nextFragment != null){
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.anchor_point, nextFragment)
.commit();
menuItem.setChecked(true);
setTitle(menuItem.getTitle());
drawerLayout.closeDrawer(mNavigationView);
}
return false;
}
});
openFilter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BottomSheetDialog filterDialog = new BottomSheetDialog();
filterDialog.show(getSupportFragmentManager(), "filter dialog opened!");
}
});
// mRecyclerView.setHasFixedSize(true);
// LinearLayoutManager layoutManager = new LinearLayoutManager(this);
// mRecyclerView.setLayoutManager(layoutManager);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
switch (item.getItemId()) {
case android.R.id.home:
drawerLayout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
private ActionBarDrawerToggle setupDrawerToggle() {
// NOTE: Make sure you pass in a valid toolbar reference. ActionBarDrawToggle() does not require it
// and will not render the hamburger icon without it.
return new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
}
public void backToHome(View view){
for (Fragment fragment : getSupportFragmentManager().getFragments()){
if (fragment != null && (fragment instanceof CourseFragment || fragment instanceof EventFragment || fragment instanceof NewsFragment)){
getSupportFragmentManager().beginTransaction().remove(fragment).commit();
toolbar.setTitle("WePress");
fab.show();
}
}
Toast.makeText(this, "Premuto sul logo", Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(mNavigationView);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
drawerToggle.onConfigurationChanged(newConfig);
}
public void addItem(View view){
PopupMenu popupMenu = new PopupMenu(MainActivity.this, fab);
popupMenu.getMenuInflater().inflate(R.menu.fab_main_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.add_event:
Toast.makeText(MainActivity.this, "Pronti per aggiungere un evento", Toast.LENGTH_SHORT).show();
Intent intentEvent = new Intent(MainActivity.this, AddItemActivity.class);
intentEvent.putExtra("isFrom", "event");
startActivity(intentEvent);
break;
case R.id.add_course:
Toast.makeText(MainActivity.this, "Pronti per aggiungere un corso", Toast.LENGTH_SHORT).show();
Intent intentCourse = new Intent(MainActivity.this, AddItemActivity.class);
intentCourse.putExtra("isFrom", "course");
startActivity(intentCourse);
break;
case R.id.add_news:
Toast.makeText(MainActivity.this, "Pronti per aggiungere una news", Toast.LENGTH_SHORT).show();
Intent intentNews = new Intent(MainActivity.this, AddItemActivity.class);
intentNews.putExtra("isFrom", "news");
startActivity(intentNews);
break;
}
return true;
}
});
popupMenu.show();
}
}
if it's possible to show the list with differents layouts, can you also suggest a way to improve the quality of my code avoiding to repeat lines of code? If I'm not wrong, I would like to re-use each Adapter or part of them that I can put into a specific class (?).
Thank you so much for your help!
If I'm missing some information, please ask me in order to edit the post.
You must do only one adapter (using view type) with a view model class
that contain either an event or a news or a course
the adapter should be something like that:
public class EventViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int TYPE_EVENT = 10;
private static final int TYPE_COURSE = 11;
private static final int TYPE_NEWS = 12;
ArrayList<EventViewModel> eventList;
Context context;
public EventViewAdapter(ArrayList<EventViewModel> eventList, Context context) {
this.eventList = eventList;
this.context = context;
}
public EventViewAdapter() {
}
#Override
public int getItemViewType(int position) {
EventViewModel event = eventList.get(position);
if (event.isEvent()) {
return TYPE_EVENT ;
} else if(event.isCourse()) {
return TYPE_COURSE;
} else {
return TYPE_NEWS;
}
}
public static class EventViewHolder extends RecyclerView.ViewHolder {
ImageView eventImage;
//....
EventViewHolder(View eventView) {
super(eventView);
eventImage = eventView.findViewById(R.id.event_image);
//.....
}
}
public static class CourseViewHolder extends RecyclerView.ViewHolder {
ImageView courseImage;
//.....
CourseViewHolder(View courseView) {
super(courseView);
courseImage = courseView.findViewById(R.id.course_image);
//.....
}
public static class NewsViewHolder extends RecyclerView.ViewHolder {
ImageView newsImage;
//.....
NewsViewHolder(View newView){
super(newView);
newsImage = newView.findViewById(R.id.news_image);
//.....
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
if (viewType == TYPE_EVENT) {
View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.event_cell_def, parent, false);
return new EventViewHolder (rootView);
} else if (viewType == TYPE_COURSE) {
View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.course_cell_def, parent, false);
return new CourseViewHolder(rootView);
} else {
View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_cell_def, parent, false);
return new NewsViewHolder(rootView);
}
}
#Override
public void onBindViewHolder(#NonNull EventViewHolder holder, int position)
{
final EventViewModel eventViewModel = eventList.get(position);
if (eventViewModel .isEvent()) {
onBindEvent(holder, eventViewModel.getEvent());
} else if(eventViewModel .isCourse()) {
onBindCourse(holder, eventViewModel.getCourse());
} else {
onBindNews(holder, eventViewModel.getNews());
}
}
private void onBindEvent(RecyclerView.ViewHolder holder, Event event) {
EventViewHolder eventHolder= (EventViewHolder ) holder;
final String title = event.getTitle();
final String description = event.getDescription();
// others ...
holder.eventImage.setImageResource(image);
holder.startingDate.setText(startDate);
// others ...
holder.cellLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
// ...
}
#Override
public int getItemCount() {
return eventList.size();
}
public void eventSetSearchOperation(ArrayList<Event> newList){
eventList = new ArrayList<>();
eventList.addAll(newList);
notifyDataSetChanged();
}
}
And a View Model classes like that :
public class EventViewModel() {
private Event mEvent;
private Course mCourse;
private News mNews;
private EventViewModel(Event event, Course course, News news) {
this.mEvent = event;
this.mCourse = course;
this.mNews = news;
}
public boolean isEvent() {
return mEvent != null
}
public boolean isCourse() {
return mCourse != null
}
public boolean isNews() {
return mNews != null
}
public static EventViewModel getEventInstance(Event event) {
return new EventViewModel(event, null, null
}
public static EventViewModel getCourseInstance(Course course) {
return new EventViewModel(null, course, null
}
public static EventViewModel getNewsInstance(News news) {
return new EventViewModel(null, null, news
}
public Event getEvent() {
return mEvent;
}
public Event getCourse() {
return mScore;
}
public Event getNews() {
return mNews;
}
}
You can use this adapter in all of your fragments (event, course, news) and each fragment contaions a list of EventViewModel with only its specific type. The adapter display in the main activity contains EventViewModel of all types to display all events.

How to align messages in a RecyclerView depending on whether the user sent them

I'm making a messaging app for school, I'm trying to make the messages coming from the user align to the right and the messages from others align left.
I've been looking online to try and figure out how to do it, but I don't understand any of them well enough to apply them. I was hoping for some help with applying any option to my app.
SubjectGroupPage.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_subject_group_page);
BottomNavigationView navView = findViewById(R.id.nav_view);
mTextMessage = findViewById(R.id.message);
navView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
rvMessagesSubjectGroupPage.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, true));
final ArrayList<MessageObj> messages = new ArrayList<>();
ArrayList<MessageObj> messagesAdap = readMessages(rvMessagesSubjectGroupPage, messages);
MessageAdapter adapter = new MessageAdapter(messagesAdap);
rvMessagesSubjectGroupPage.setAdapter(adapter);
}
public ArrayList<MessageObj> readMessages(final RecyclerView rv, final ArrayList<MessageObj> messages) {
db.collection("messagesIT")
.orderBy("Timesent", Query.Direction.DESCENDING)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
String message = document.getString("Message");
String senderName = document.getString("SenderName");
String sender = document.getString("Sender");
Timestamp timesent = document.getTimestamp("Timesent");
MessageObj mess;
mess = new MessageObj(message, sender, senderName, timesent);
messages.add(mess);
Log.d(TAG, "mmmmm: " + mess.getMessage());
Log.d(TAG, "sssss: " + mess.getSender());
Log.d(TAG, "ttttt:" + mess.getTimesent());
showToast("b");
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
showToast("There has been an error, please try again later.");
Log.d(TAG, "Error: " + e);
}
});
return messages;
}
MessageAdapter.java
public class MessageAdapter extends
RecyclerView.Adapter<MessageAdapter.ViewHolder> {
private ArrayList<MessageObj> listdata;
private FirebaseAuth currentUser = FirebaseAuth.getInstance();
private boolean fromCurrentUser = true;
public static final String MyPREFERENCES = "MyPrefs";
private static String userObject = "";
private static final String TAG = "MessageAdapter.java";
public MessageAdapter(ArrayList<MessageObj> listdata) {
this.listdata = listdata;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView sender;
public TextView message;
public LinearLayout linearLayout;
public ViewHolder(View itemView) {
super(itemView);
this.sender = (TextView) itemView.findViewById(R.id.sender);
this.message = (TextView) itemView.findViewById(R.id.message);
linearLayout = (LinearLayout) itemView.findViewById(R.id.listLayout);
}
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final MessageObj myListData = listdata.get(position);
holder.sender.setText(listdata.get(position).getSenderName());
holder.message.setText(listdata.get(position).getMessage());
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "click on item: " + myListData.getSender(), Toast.LENGTH_LONG).show();
}
});
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View listItem = layoutInflater.inflate(R.layout.list_item, parent, false);
ViewHolder viewHolder = new ViewHolder(listItem);
TextView sender;
TextView message;
LinearLayout linearLayout;
return viewHolder;
}
#Override
public int getItemCount() {
return listdata.size();
}
}
MessageObj.java
public class MessageObj {
private String message;
private String sender;
private String senderName;
private Timestamp timesent;
private boolean isCurrentUser;
public MessageObj() {}
public MessageObj(String message, String sender, String senderName, Timestamp timesent) {
this.message = message;
this.sender = sender;
this.senderName = senderName;
this.timesent = timesent;
}
public String getMessage() { return message; }
public String getSender() { return sender; }
public String getSenderName() { return senderName; }
public Timestamp getTimesent() { return timesent; }

Firebase RecyclerView not displaying Anything

I have the same code for another database and it works just fine, I have no Idea why it's not working here,
the onCreateViewHolder does not even get called.
(Appearently Stacks Overflow wants me to add more details to compensate for too much code so here's some unuseful Text , Text, Text , TEXT).
public class Course extends AppCompatActivity {
private RecyclerView mLocationView;
private DatabaseReference mLocation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course);
mLocation = FirebaseDatabase.getInstance().getReference("FINISHEDCOURSES");
mLocation.keepSynced(true);
mLocationView = (RecyclerView) findViewById(R.id.my_recycler_view);
mLocationView.setHasFixedSize(true);
mLocationView.setLayoutManager(new LinearLayoutManager(this));
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions<Courses> options = new FirebaseRecyclerOptions.Builder<Courses>().setQuery(mLocation, Courses.class).build();
FirebaseRecyclerAdapter<Courses, CourseViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Courses, CourseViewHolder>
(options) {
#Override
protected void onBindViewHolder(#NonNull CourseViewHolder holder, int position, #NonNull Courses model) {
holder.setText(model.getDate(), model.getDistance(), model.getDriver(), model.getPrice());
}
#NonNull
#Override
public CourseViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.courses_rows, parent, false);
return new CourseViewHolder(view);
}
};
mLocationView.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.startListening();
}
public static class CourseViewHolder extends RecyclerView.ViewHolder{
View mView;
public CourseViewHolder(View itemView){
super(itemView);
mView = itemView;
}
public void setText(String Date, String start, String end, String price){
TextView dateText = mView.findViewById(R.id.dateText);
TextView startText = mView.findViewById(R.id.depart_text);
TextView endText = mView.findViewById(R.id.end_text);
TextView priceText = mView.findViewById(R.id.price);
dateText.setText(Date);
startText.setText(start);
endText.setText(end);
priceText.setText(price);
}
}
The Courses Class
public class Courses {
private String driver, date, distance, preWaitTime, price, waitTime, client;
public Courses() {
}
public Courses(String driver, String date, String distance, String preWaitTime, String price, String waitTime, String client) {
this.driver = driver;
this.date = date;
this.distance = distance;
this.preWaitTime = preWaitTime;
this.price = price;
this.waitTime = waitTime;
this.client = client;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getDistance() {
return distance;
}
public void setDistance(String distance) {
this.distance = distance;
}
public String getPreWaitTime() {
return preWaitTime;
}
public void setPreWaitTime(String preWaitTime) {
this.preWaitTime = preWaitTime;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getWaitTime() {
return waitTime;
}
public void setWaitTime(String waitTime) {
this.waitTime = waitTime;
}
public String getClient() {
return client;
}
public void setClient(String client) {
this.client = client;
}
}
Here's The Database Structure :
Sorry I should have commented but i do not have enough reputation to do so.
I cannot see anything similar to this in your code.
FirebaseRecyclerAdapter<Courses, CourseViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Courses, CourseViewHolder>(
Courses.class,
R.layout.Courses,// should be your layout name
CourseViewHolder.class,
mLocation
) {
#Override
protected void populateViewHolder(CourseViewHolder viewHolder, category model, final int position) {
viewHolder.setName(model.getName());
viewHolder.setImage(getApplicationContext(),model.getImage());

Inter-changing LayoutInflator View using if statements

I am trying to make an App that enables users to interact using text messages and audio messages. I am able to send and retrieve messages but the problem i'm facing is that i am getting an NullPointerException when i try to check whether the received message is a text or an audio. What i want to do is first check the text type, after that choose an appropriate Layout view based on the message type. I used adapter and this is my code:
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MyMessageViewHolder>{
private ArrayList<Messages> arrayListMessages= new ArrayList<>();
private Context mcontext;
FirebaseAuth mAuth;
private String Type = null;
public static class myAudioMessageViewHolder extends RecyclerView.ViewHolder{
TextView rec_start;
TextView rec_end;
ImageButton myPlay;
SeekBar audio_seek;
public myAudioMessageViewHolder(View itemView){
super(itemView);
rec_start = itemView.findViewById(R.id.time_elapsed);
rec_end = itemView.findViewById(R.id.finish_time);
myPlay = itemView.findViewById(R.id.play_btn);
audio_seek = itemView.findViewById(R.id.my_seekbar);
}
}
public static class MyMessageViewHolder extends RecyclerView.ViewHolder{
TextView textViewMessage;
CircleImageView UserProfile;
public MyMessageViewHolder(View itemView) {
super(itemView);
textViewMessage = itemView.findViewById(R.id.messageTextView);
UserProfile = itemView.findViewById(R.id.message_profile_layout);
}
}
public ArrayAdapter adapter;
public MessageAdapter(Context context, ArrayList<Messages> arrayListMessages){
this.arrayListMessages = arrayListMessages;
mcontext = context;
}
#Override
public MyMessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType ) {
if (Type.equals("audio")){ // This should be the determinant on the view being chosen and it should call type from database
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.audio_message_layout, parent, false);
return new MyMessageViewHolder(view);
}else if (Type.equals("text")){
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_single_layout, parent,false);
return new MyMessageViewHolder(view);
}
else {
return null;
}
}
#Override
public void onBindViewHolder(final MyMessageViewHolder holder, final int position) {
mAuth = FirebaseAuth.getInstance();
String mCurrentUser = mAuth.getCurrentUser().getUid();
final Messages messages = arrayListMessages.get(position);
String message_type = messages.getType();
String from_user = messages.getFrom();
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(from_user);
if(mCurrentUser.equals(messages.getFrom())){
holder.textViewMessage.setBackgroundResource(R.drawable.custom_message_bg_primary);
holder.textViewMessage.setTextIsSelectable(true);
holder.textViewMessage.setGravity(Gravity.RIGHT);
holder.textViewMessage.setTextColor(Color.WHITE);
holder.textViewMessage.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view)
{
CharSequence options[] = new CharSequence[]{"Delete", "Copy Text"};
final AlertDialog.Builder builder = new AlertDialog.Builder(mcontext);
builder.setTitle("Select Options");
builder.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//Click Event for each item.
if (i == 0) {
String selectedItem = messages.getMessage();
arrayListMessages.remove(selectedItem);
Toast.makeText(mcontext, "Message deleted", Toast.LENGTH_SHORT).show();
}
if (i == 1)
{
ClipboardManager cm = (ClipboardManager) mcontext.getSystemService(Context.CLIPBOARD_SERVICE);
cm.setText(holder.textViewMessage.getText());
Toast.makeText(mcontext, "Copied to clipboard", Toast.LENGTH_SHORT).show();
}
}
});
builder.show();
return true;
}
});
}else{
holder.textViewMessage.setBackgroundResource(R.drawable.custom_message_bd_white);
holder.textViewMessage.setTextIsSelectable(true);
holder.textViewMessage.setGravity(Gravity.LEFT);
holder.textViewMessage.setTextColor(Color.BLACK);
holder.textViewMessage.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
CharSequence options[] = new CharSequence[]{"Delete", "Copy Text"};
final AlertDialog.Builder builder = new AlertDialog.Builder(mcontext);
builder.setTitle("Select Options");
builder.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//Click Event for each item.
if (i == 0) {
String selectedItem = messages.getMessage();
arrayListMessages.remove(selectedItem);
adapter.notifyDataSetChanged();
Toast.makeText(mcontext, "Message deleted", Toast.LENGTH_SHORT).show();
}
if (i == 1)
{
ClipboardManager cm = (ClipboardManager) mcontext.getSystemService(Context.CLIPBOARD_SERVICE);
cm.setText(holder.textViewMessage.getText());
Toast.makeText(mcontext, "Copied to clipboard", Toast.LENGTH_SHORT).show();
}
}
});
builder.show();
return true;
}
});
}
holder.textViewMessage.setText(messages.getMessage());
if(message_type.equals("text")) {
Type = messages.getType(); // Here i am trying to get the message type and then use it to get Inflator view of a text type
holder.textViewMessage.setText(messages.getMessage());
}
else if(message_type.equals("audio"))
{
Type = messages.getType(); // Here i am trying to get the message type and then use it to get Inflator view of a audio type
}
}
#Override
public int getItemCount() {
return arrayListMessages.size();
}
}
This is where i get the Messages from :
public class Messages {
String message,type,from;
long time;
boolean seen;
public Messages(){
// for datashnap shot
}
public Messages(String message, String type, long time, boolean seen, String from) {
this.message = message;
this.type = type;
this.time = time;
this.seen = seen;
this.from=from;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public boolean isSeen() {
return seen;
}
public void setSeen(boolean seen) {
this.seen = seen;
}
}
After Running the App on a device, i got the following error:
FATAL EXCEPTION: main
Process: com.rescuex_za.rescuex, PID: 22285
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at com.rescuex_za.rescuex.MessageAdapter.onCreateViewHolder(MessageAdapter.java:92)
at com.rescuex_za.rescuex.MessageAdapter.onCreateViewHolder(MessageAdapter.java:37)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6519)
I am getting and checking the messages from my database. How can i read the message type? Please provide full details or suggestions as i am still new to android development and want to understand what i am doing. Thank you.
after Following the approach suggested by AL. I am not getting the view i am looking for this is the updates of the classes i posted when i asked the question.
1.Messages class
public class Messages {
public static final int TEXT_TYPE=0;
public static final int AUDIO_TYPE=1;
String message,type,from;
long time;
boolean seen;
int type_text;
public Messages(){
// for datashnap shot
}
public Messages(String message, String type, long time, boolean seen, String from) {
this.message = message;
this.type = type;
this.time = time;
this.seen = seen;
this.from=from;
if (type.equals("text")){
this.type_text=TEXT_TYPE;
} else if (type.equals("recording")){
this.type_text=AUDIO_TYPE;
}
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public boolean isSeen() {
return seen;
}
public void setSeen(boolean seen) {
this.seen = seen;
}
}
This is my Adapter class
public class MessageAdapter extends
RecyclerView.Adapter<MessageAdapter.MyMessageViewHolder>{
private ArrayList<Messages> arrayListMessages= new ArrayList<>();
private Context mcontext;
FirebaseAuth mAuth;
private DatabaseReference mUserDatabase;
private ViewGroup parent;
public static class myAudioMessageViewHolder extends MyMessageViewHolder{
TextView rec_start;
TextView rec_end;
ImageButton myPlay;
SeekBar audio_seek;
public myAudioMessageViewHolder(View itemView){
super(itemView);
rec_start = itemView.findViewById(R.id.time_elapsed);
rec_end = itemView.findViewById(R.id.finish_time);
myPlay = itemView.findViewById(R.id.play_btn);
audio_seek = itemView.findViewById(R.id.my_seekbar);
}
}
public static class MyMessageViewHolder extends RecyclerView.ViewHolder{
TextView textViewMessage;
CircleImageView UserProfile;
public MyMessageViewHolder(View itemView) {
super(itemView);
textViewMessage = itemView.findViewById(R.id.messageTextView);
UserProfile = itemView.findViewById(R.id.message_profile_layout);
}
}
public ArrayAdapter adapter;
public MessageAdapter(Context context, ArrayList<Messages> arrayListMessages){
this.arrayListMessages = arrayListMessages;
mcontext = context;
}
#Override
public MyMessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType ) {
View view;
switch (viewType) {
case Messages.TEXT_TYPE:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_single_layout, parent, false);
return new myAudioMessageViewHolder(view);
case Messages.AUDIO_TYPE:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.audio_message_layout, parent, false);
return new MyMessageViewHolder(view);
}
return null;
}
#Override
public int getItemViewType(int position) {
switch (arrayListMessages.get(position).type_text) {
case 0:
return Messages.TEXT_TYPE;
case 1:
return Messages.AUDIO_TYPE;
default:
return -1;
}
}
#Override
public void onBindViewHolder(final MyMessageViewHolder holder, final int position) {
mAuth = FirebaseAuth.getInstance();
String mCurrentUser = mAuth.getCurrentUser().getUid();
final Messages messages = arrayListMessages.get(position);
String message_type = messages.getType();
String from_user = messages.getFrom();
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(from_user);
if(mCurrentUser.equals(messages.getFrom())){
holder.textViewMessage.setBackgroundResource(R.drawable.custom_message_bg_primary);
holder.textViewMessage.setTextIsSelectable(true);
holder.textViewMessage.setGravity(Gravity.RIGHT);
holder.textViewMessage.setTextColor(Color.WHITE);
holder.textViewMessage.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view)
{
CharSequence options[] = new CharSequence[]{"Delete", "Copy Text"};
final AlertDialog.Builder builder = new AlertDialog.Builder(mcontext);
builder.setTitle("Select Options");
builder.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//Click Event for each item.
if (i == 0) {
String selectedItem = messages.getMessage();
arrayListMessages.remove(selectedItem);
Toast.makeText(mcontext, "Message deleted", Toast.LENGTH_SHORT).show();
}
if (i == 1)
{
ClipboardManager cm = (ClipboardManager) mcontext.getSystemService(Context.CLIPBOARD_SERVICE);
cm.setText(holder.textViewMessage.getText());
Toast.makeText(mcontext, "Copied to clipboard", Toast.LENGTH_SHORT).show();
}
}
});
builder.show();
return true;
}
});
}else{
holder.textViewMessage.setBackgroundResource(R.drawable.custom_message_bd_white);
holder.textViewMessage.setTextIsSelectable(true);
holder.textViewMessage.setGravity(Gravity.LEFT);
holder.textViewMessage.setTextColor(Color.BLACK);
holder.textViewMessage.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
CharSequence options[] = new CharSequence[]{"Delete", "Copy Text"};
final AlertDialog.Builder builder = new AlertDialog.Builder(mcontext);
builder.setTitle("Select Options");
builder.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//Click Event for each item.
if (i == 0) {
String selectedItem = messages.getMessage();
arrayListMessages.remove(selectedItem);
adapter.notifyDataSetChanged();
Toast.makeText(mcontext, "Message deleted", Toast.LENGTH_SHORT).show();
}
if (i == 1)
{
ClipboardManager cm = (ClipboardManager) mcontext.getSystemService(Context.CLIPBOARD_SERVICE);
cm.setText(holder.textViewMessage.getText());
Toast.makeText(mcontext, "Copied to clipboard", Toast.LENGTH_SHORT).show();
}
}
});
builder.show();
return true;
}
});
}
holder.textViewMessage.setText(messages.getMessage());
if(message_type.equals("text")) {
// Here i am trying to get the message type and then use it to get Inflator view of a text type
holder.textViewMessage.setText(messages.getMessage());
}
else if(message_type.equals("recording"))
{
// Here i am trying to get the message type and then use it to get Inflator view of a audio type
}
}
#Override
public int getItemCount() {
return arrayListMessages.size();
}
}

Android recyclerView updata all list data still merge with old data

public RecyclerView.Adapter mAdapter;
public RecyclerView mMessagesView;
this is my list
public List<MessageList> Message_List = new ArrayList<MessageList>();
and this is my adapter
mAdapter = new MessageAdapter1(getApplicationContext(), Message_List);
mMessagesView = (RecyclerView) findViewById(R.id.messages);
mMessagesView.setLayoutManager(new LinearLayoutManager(this));
mMessagesView.setAdapter(mAdapter);
now i add some data on it
Message_List.add(messageList);
mAdapter.notifyItemInserted(Message_List.size() - 1);
its work without problems
now i want to change all Message_List data and show in in recyvlerView
i do this
public void swap(List<MessageList> datas){
Message_List.clear();
Message_List.addAll(datas);
mAdapter.notifyDataSetChanged();
}
here i replace all data in Message_List and notifyDataSetChanged now its shown new data of Message_List
empty Message_List show blank
but when i try add new item to list and notify adapter its shown to me some of old item i removed it from Message_List and in LOG i watch it no old data there
now this is image before update Message_List data
now change Message_List data and notify
like we see blank because there are no data on Message_List
now try add new item to Message_List and notify adapter
Message_List.add(balabala data);
mAdapter.notifyItemInserted(Message_List.size() - 1);
and result must be without old data
for more information this is my MessageList class
public class MessageList {
public static final int TYPE_MESSAGE_RIGHT = 0;
public static final int TYPE_MESSAGE_LEFT = 1;
public static final int TYPE_ACTION = 2;
public static final String TYPING = "typing";
private String message, thumbnailUrl;
private String date;
private int user_id;
private boolean status = false;
private String fname;
private String direction;
private ArrayList<String> messageList;
public MessageList() {
setMessageStatus(status);
}
public MessageList(String name, String thumbnailUrl, String date, String direction,
ArrayList<String> messageList, String fname, int user_id) {
this.message = name;
this.thumbnailUrl = thumbnailUrl;
this.date = date;
this.direction = direction;
this.messageList = messageList;
this.fname = fname;
this.user_id = user_id;
}
public String getMessage() {
return message;
}
public String getFname() {
return fname;
}
public void setMessage(String message) {
this.message = message;
}
public String getThumbnailUrl() {
return thumbnailUrl;
}
public void setThumbnailUrl(String thumbnailUrl) {
this.thumbnailUrl = thumbnailUrl;
}
public String getDate() {
return date;
}
public boolean getMessageStatus() {
return status;
}
public void setMessageStatus(boolean status) {
this.status = status;
}
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public void setDate(String date) {
this.date = date;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getDir() {
return direction;
}
public void setDir(String direction) {
this.direction = direction;
}
public ArrayList<String> getMessageList() {
return messageList;
}
public void setMessageList(ArrayList<String> messageList) {
this.messageList = messageList;
}
}
and this is my adapter
public class MessageAdapter1 extends RecyclerView.Adapter<MessageAdapter1.ViewHolder> {
private List<MessageList> mMessages;
private int[] mUsernameColors;
private Context context;
public MessageAdapter1(Context context, List<MessageList> messages) {
mMessages = messages;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
int layout = -1;
int type = -1;
if(mMessages.get(viewType).getDir().equals("left")) type = 1;
else if(mMessages.get(viewType).getDir().equals("right")) type = 0;
else if(mMessages.get(viewType).getDir().equals("typing")) type = 2;
switch (type) {
case MessageList.TYPE_MESSAGE_RIGHT:
layout = R.layout.right_message;
break;
case MessageList.TYPE_MESSAGE_LEFT:
layout = R.layout.left_message;
break;
case MessageList.TYPE_ACTION:
layout = R.layout.message_left;
break;
}
View v = LayoutInflater.from(parent.getContext()).inflate(layout, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
MessageList message = mMessages.get(position);
viewHolder.setGroupMessage(message);
}
#Override
public int getItemCount() {
return mMessages.size();
}
#Override
public int getItemViewType(int position) {
return position;
}
public class ViewHolder extends RecyclerView.ViewHolder {
private LinearLayout groupMessage;
//private ImageView typing;
public ViewHolder(View itemView) {
super(itemView);
groupMessage = (LinearLayout)itemView.findViewById(R.id.messages);
}
public void setGroupMessage(MessageList m) {
if (null == groupMessage) return;
int i = 0;
if(m.getMessageStatus() == false){
m.setMessageStatus(true);
for (String message : m.getMessageList()) {
//TextView text = new TextView(activity);
TextView text = new MyTextView(context);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
p.setMargins(0, 0, 0, 2);
if (m.getDir().equals("left")) {
text.setTextColor(Color.BLACK);
p.gravity = Gravity.LEFT;
if(m.getMessageList().size() == 1){
text.setBackgroundResource(R.drawable.message_left_default);
}
else if (i == 0) {
text.setBackgroundResource(R.drawable.message_left_first);
} else if (i + 1 == m.getMessageList().size()) {
text.setBackgroundResource(R.drawable.message_left_last);
} else {
text.setBackgroundResource(R.drawable.message_left);
}
} else{
p.gravity = Gravity.RIGHT;
text.setTextColor(Color.WHITE);
if(m.getMessageList().size() == 1){
text.setBackgroundResource(R.drawable.message_right_default);
}
else if (i == 0) {
text.setBackgroundResource(R.drawable.message_right_first);
} else if (i + 1 == m.getMessageList().size()) {
text.setBackgroundResource(R.drawable.message_right_last);
} else {
text.setBackgroundResource(R.drawable.message_right);
}
}
text.setLayoutParams(p);
text.setText(message);
text.setPadding(8, 8, 8, 8);
text.setTextSize(18f);
//text.setTextAppearance(context, android.R.style.TextAppearance_Large);
groupMessage.addView(text);
i++;
}
}
}
}
}
the problem is there is no connection between mMessages and Message_List. To achieve this, put swap() in the adapter class and call it from the activity or fragment.
public class MessageAdapter1 extends RecyclerView.Adapter<MessageAdapter1.ViewHolder> {
...
public void swap(List<MessageList> datas){
mMessages.clear();
mMessages.addAll(datas);
notifyDataSetChanged();
}
public void add(MessageList data){
mMessages.add(data);
notifyItemInserted(mMessages.size() - 1);
}
}
and from fragment or activity:
((MessageAdapter1) rv.getAdapter).add(data);
((MessageAdapter1) rv.getAdapter).swap(Message_List);

Categories

Resources