I'm trying to use a RecyclerView to have a variable number of players in a game. The number of players is set by an extra passed with the intent on loading this activity. The objective is to have "Player #X : EditText" for every player to insert his name, to catch those names and send them onto a new activity.
The problem I am facing is that the RecyclerView does not seem to work. When loading the activity, there are no textviews or edit zones, juste a white screen. The bottom Button is working fine though. I can see the "zone" created for the RecyclerView by pulling up and down, it shows the blue "end of page" markers.
Here is the code, if you could help me figure out what happened :
Activity (java)
package com.olirwin.spartacus.deutsch;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class players extends AppCompatActivity {
ArrayList<String> playerNames;
Button send;
PlayerAdapter Adapter;
RecyclerView playerList;
String[] names;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_players);
playerNames = new ArrayList<String>();
final ArrayList<Player> players = new ArrayList<Player>();
send = findViewById(R.id.b_send);
Bundle extra = new Bundle();
final int numPlayers = extra.getInt("num");
for (int i = 0; i < numPlayers; i++)
{
playerNames.add("Joueur " + (i+1) + " : ");
}
playerList = findViewById(R.id.player_list);
playerList.setLayoutManager(new LinearLayoutManager(this));
Adapter = new PlayerAdapter();
playerList.setAdapter(Adapter);
names = new String[numPlayers];
Adapter.notifyDataSetChanged();
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i = 0; i < numPlayers; i++) {
players.add(new Player(names[i]));
}
Intent intent = new Intent(view.getContext(), Score.class);
intent.putExtra("players", players);
view.getContext().startActivity(intent);
}
});
}
private class PlayerAdapter extends
RecyclerView.Adapter<PlayerAdapter.PlayerHolder> {
@Override
public PlayerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v =
LayoutInflater.from(players.this).inflate(R.layout.list_cell, parent,
false);
return new PlayerHolder(v);
}
@Override
public void onBindViewHolder(PlayerHolder holder, final int position) {
holder.bind(playerNames.get(position));
holder.mPlayerName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int
start, int before,
int count) {
names[position] = charSequence.toString();
}
@Override
public void onTextChanged(CharSequence charSequence, int start,
int before,
int count){
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
@Override
public int getItemCount() {
return playerNames.size();
}
public class PlayerHolder extends RecyclerView.ViewHolder {
TextView mPlayerLabel;
EditText mPlayerName;
public PlayerHolder(View itemView) {
super(itemView);
mPlayerLabel = itemView.findViewById(R.id.player_label);
mPlayerName = itemView.findViewById(R.id.player_name);
}
public void bind(String playerName) {
mPlayerLabel.setText(playerName);
mPlayerName.setHint("Nom de " + playerName);
}
public String getData(){
return mPlayerName.getText().toString();
}
}
}
}
Layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.olirwin.spartacus.deutsch.players">
<android.support.v7.widget.RecyclerView
android:id="@+id/player_list"
android:layout_width="0dp"
android:layout_height="476dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/b_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Valider"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/player_list" />
</android.support.constraint.ConstraintLayout>