You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android RecyclerView不显示项:游戏玩家输入列表异常求助

Fixing Your Blank RecyclerView for Dynamic Player Inputs

Hey there! Let's figure out why your RecyclerView is showing a blank screen even though the bottom button works. This is a super common pitfall when setting up RecyclerViews for dynamic content, so let's walk through the most likely fixes and provide a working example you can reference.


Common Causes & Fixes

1. You forgot to set a LayoutManager

RecyclerView requires a LayoutManager to know how to arrange its items (linear grid, staggered, etc.). Without one, it won't render any content at all—even if your Adapter is set up correctly.

In your Activity's onCreate method, make sure you add this line after finding your RecyclerView:

recyclerView.setLayoutManager(new LinearLayoutManager(this));

2. Your Adapter's getItemCount() returns 0

If you're not passing the correct player count from your Intent extra to the Adapter, getItemCount() will return 0, resulting in an empty list.

Verify:

  • You're correctly extracting the player count from the Intent (e.g., int playerCount = getIntent().getIntExtra("PLAYER_COUNT", 0);)
  • You're passing this count to your Adapter's constructor
  • Your Adapter's getItemCount() returns this count, not a hardcoded 0.

3. Your Item Layout isn't inflated correctly

If your Adapter's onCreateViewHolder fails to inflate the item layout (wrong ID, incorrect LayoutInflater usage), the items won't render.

Double-check that you're inflating the layout properly:

View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.player_item, parent, false);

Make sure R.layout.player_item matches your actual item layout file name.

4. You didn't attach the Adapter to the RecyclerView

It sounds obvious, but it's easy to forget! After creating your Adapter, you need to bind it to the RecyclerView:

PlayerAdapter adapter = new PlayerAdapter(playerCount);
recyclerView.setAdapter(adapter);

Working Example Code

Here's a complete, tested implementation that matches your requirements:

1. Item Layout (res/layout/player_item.xml)

This defines each player's input row:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="16dp"
    android:gravity="center_vertical">

    <TextView
        android:id="@+id/tv_player_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:text="Player #1"/>

    <EditText
        android:id="@+id/et_player_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter name"
        android:layout_marginStart="16dp"/>
</LinearLayout>

2. RecyclerView Adapter (PlayerAdapter.java)

Handles binding data and tracking player names:

import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class PlayerAdapter extends RecyclerView.Adapter<PlayerAdapter.PlayerViewHolder> {

    private final int playerCount;
    private final List<String> playerNames;

    public PlayerAdapter(int playerCount) {
        this.playerCount = playerCount;
        // Initialize list with empty strings for each player
        playerNames = new ArrayList<>(Collections.nCopies(playerCount, ""));
    }

    @NonNull
    @Override
    public PlayerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.player_item, parent, false);
        return new PlayerViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull PlayerViewHolder holder, int position) {
        // Set player number (1-indexed)
        holder.tvPlayerNumber.setText(String.format("Player #%d", position + 1));
        
        // Sync EditText with stored name and track changes
        holder.etPlayerName.setText(playerNames.get(position));
        holder.etPlayerName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                playerNames.set(position, s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {}
        });
    }

    @Override
    public int getItemCount() {
        return playerCount;
    }

    // Getter to retrieve all player names for the next Activity
    public List<String> getPlayerNames() {
        return playerNames;
    }

    static class PlayerViewHolder extends RecyclerView.ViewHolder {
        TextView tvPlayerNumber;
        EditText etPlayerName;

        public PlayerViewHolder(@NonNull View itemView) {
            super(itemView);
            tvPlayerNumber = itemView.findViewById(R.id.tv_player_number);
            etPlayerName = itemView.findViewById(R.id.et_player_name);
        }
    }
}

3. Main Activity (PlayerInputActivity.java)

Initializes the RecyclerView, handles Intent data, and manages the next button:

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

public class PlayerInputActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player_input);

        // Get player count from Intent extra (default to 0 if missing)
        int playerCount = getIntent().getIntExtra("PLAYER_COUNT", 0);

        // Set up RecyclerView
        RecyclerView recyclerView = findViewById(R.id.recycler_view_players);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        PlayerAdapter adapter = new PlayerAdapter(playerCount);
        recyclerView.setAdapter(adapter);

        // Handle next button click: send names to next Activity
        Button btnNext = findViewById(R.id.btn_next);
        btnNext.setOnClickListener(v -> {
            List<String> playerNames = adapter.getPlayerNames();
            Intent intent = new Intent(this, NextActivity.class);
            intent.putStringArrayListExtra("PLAYER_NAMES", new ArrayList<>(playerNames));
            startActivity(intent);
        });
    }
}

4. Activity Layout (res/layout/activity_player_input.xml)

Hosts the RecyclerView and bottom button:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view_players"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/btn_next"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Next"/>
</LinearLayout>

Compare your code against this example, and you'll likely spot the missing piece (most often a forgotten LayoutManager or Adapter setup). Once fixed, your RecyclerView should show all the player input rows as expected!

内容的提问来源于stack exchange,提问作者olirwin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:39:06