I am following this tutorial and adapting things to my project on the go: https://codelabs.developers.google.com/codelabs/android-training-livedata-viewmodel/index.html?index=..%2F..android-training#0
I have a database with conversation lists which I want to show in an activity, and clicking them should get me to conversation screen. For example 3 conversations should appear exactly like the whatsapp main screen, the problem is that even tough there are more entries of conversations history that I can see in the conversations variable, only one is shown in the recycle view.
Activity code:
public class activity_conversations_list extends AppCompatActivity {
private ChatViewModel chatViewModel;
public void startConversation(View view) {
//click on conversation
Intent intent = new Intent(this, activity_conversation.class);
startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_conversations_list);
RecyclerView recyclerView = findViewById(R.id.recyclerViewConversations);
final ConversationListAdapter adapter = new ConversationListAdapter(this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
chatViewModel = ViewModelProviders.of(this).get(ChatViewModel.class);
chatViewModel.getConversations().observe(this, new Observer<List<Conversation>>() {
@Override
public void onChanged(@Nullable final List<Conversation> conversations) {
// Update the cached copy of the words in the adapter.
adapter.setConversations(conversations);
}
});
}
}
xml for conversation list activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity_conversations_list">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewConversations"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</LinearLayout>
List adapter class:
public class ConversationListAdapter
extends RecyclerView.Adapter<ConversationListAdapter.ConversationItemViewHolder> {
//conversations list
private List<Conversation> conversations;
private final LayoutInflater layoutInflater;
public ConversationListAdapter(Context context) { layoutInflater = LayoutInflater.from(context); }
@Override
public ConversationItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = layoutInflater.inflate(R.layout.view_conversation_list_item, parent, false);
return new ConversationItemViewHolder(itemView);
}
@Override
public void onBindViewHolder(ConversationItemViewHolder holder, int position) {
if (conversations != null) {
Conversation current = conversations.get(position);
holder.textViewConversationName.setText(current.getConversation_name());
} else {
// Covers the case of data not being ready yet.
holder.textViewConversationName.setText("No conversations");
}
}
public void setConversations(List<Conversation> conversations){
this.conversations = conversations;
notifyDataSetChanged();
}
@Override
public int getItemCount() {
if (conversations != null)
return conversations.size();
else return 0;
}
class ConversationItemViewHolder extends RecyclerView.ViewHolder {
private final TextView textViewConversationName;
private ConversationItemViewHolder(View itemView) {
super(itemView);
textViewConversationName = itemView.findViewById(R.id.textViewConversationName);
}
}
}
xml for list item:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/linearLayoutConversationListItem"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textViewConversationName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:clickable="true"
android:focusable="auto"
android:onClick="startConversation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
change your xml for list item to the one below
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/linearLayoutConversationListItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textViewConversationName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:clickable="true"
android:focusable="auto"
android:onClick="startConversation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Your items are there, just your item fills 1 full screen, if you start scrolling you will see it is there.