我试图使用RecyclerView在游戏中拥有可变数量的玩家。玩家的数量是通过加载此活动的额外传递来设置的。目标是让玩家#X: EditText "为每个玩家插入他的名字,抓住这些名字并将其发送到新的活动中。
我面临的问题是RecyclerView似乎不起作用。加载活动时,没有文本视图或编辑区域,请选择白色屏幕。底部按钮工作正常。我可以看到"区"通过向上和向下拉动为RecyclerView创建,它显示蓝色"页面末尾"标记。
这是代码,如果你可以帮我弄清楚发生了什么:
活动(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();
}
}
}
}
布局
<?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>
答案 0 :(得分:3)
问题在于您的自定义适配器constructor
,您甚至没有在列表中传递数据来填充!
尝试学习调试它会让你的生活更轻松。!
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);
}
}
答案 1 :(得分:1)
未传递给适配器以进行填充的项目。
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();
}
}
}
}
希望这会有所帮助。