0

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>
4
  • Did you check if your adapter recive the information?
    – Tatsuya
    Commented Feb 27, 2018 at 16:20
  • How do I do that ?
    – olirwin
    Commented Feb 27, 2018 at 16:21
  • you need to indent your code not for us but for you .. so that makes code much easier to read and understand
    – Mohammad
    Commented Feb 27, 2018 at 16:42
  • Hi, the code is indented in AS, but reformated when i copy/pasted it here
    – olirwin
    Commented Feb 27, 2018 at 16:48

2 Answers 2

1

The problem is in your custom adapter constructor you are not even passing data in list to populate! try to learn debugging it will make your life more easier.!

 Adapter = new PlayerAdapter(playerNames,context);


private class PlayerAdapter extends             
RecyclerView.Adapter<PlayerAdapter.PlayerHolder> {

ArrayList<String> playerNames;

Context context;

   public PlayerAdapter (ArrayList<String> arrayList, Context context) {
            playerNames= arrayList;
            context = context;
        }


    @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);
    }



}
0
1

Items not passed to Adapter for populating.

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(playerNames,this);
    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> {
private ArrayList<String> playerNames;
private Context context;

  public PlayerAdapter (ArrayList<String> playerNames, Context context) {
    this.playerNames = playerNames;
    this.context = context;
}

    @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(this.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();
        }

    }
}
}

Hope this will help.

1
  • Hi, I don't know if it's related, but I'm getting a E/BoostFramework: BoostFramework() : Exception_1 = java.lang.NoSuchMethodException: perfIOPrefetchStart [int, class java.lang.String] error when I launch the activity, what would it be reated to ? (tried earching on the web, did not find anything)
    – olirwin
    Commented Feb 28, 2018 at 10:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.