Home > Blockchain >  RecyclerView show only one item
RecyclerView show only one item

Time:11-26

I have a problem, I want to display a list in my second Activity but it only displays the last item and not the previous ones. I need to display all my items on Activity so.

What do I need to do to display them all?

My Activity:

    mRecyclerView = (RecyclerView)findViewById(R.id.home_recyclerview_pokemonname);

    mPokemonList = new ArrayList<>();

    mPokemonList.add(new MyPokemonBank("Pikachu", "Electrik"));
    mPokemonList.add(new MyPokemonBank("Dracaufeu", "Feu"));
    mPokemonList.add(new MyPokemonBank("Miaouss", "Normal"));

    mAdapter = new MyPokemonAdapter(mPokemonList);

    mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false));
    mRecyclerView.setAdapter(mAdapter);
}

My Adapter:

public class MyPokemonAdapter extends RecyclerView.Adapter<MyPokemonAdapter.MyViewHolder> {

ArrayList<MyPokemonBank> mPokemonList;

MyPokemonAdapter(ArrayList<MyPokemonBank> mPokemonList){
    this.mPokemonList = (ArrayList<MyPokemonBank>) mPokemonList;
}

@NonNull
@Override
public MyPokemonAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
    View view = layoutInflater.inflate(R.layout.pokemon_bank, parent, false);
    return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull MyPokemonAdapter.MyViewHolder holder, int position) {
    holder.display(mPokemonList.get(position));
}

@Override
public int getItemCount() {
    return mPokemonList.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder{

    private TextView mPokemonName;
    private TextView mPokemonType;

    public MyViewHolder(@NonNull View itemView) {
        super(itemView);

        mPokemonName = (TextView)itemView.findViewById(R.id.name);
        mPokemonType = (TextView)itemView.findViewById(R.id.type);
    }

    public void display(MyPokemonBank myPokemonBank) {

        this.mPokemonName.setText(MyPokemonBank.getName());
        this.mPokemonType.setText(MyPokemonBank.getType());

    }
}
}

CodePudding user response:

Do not use match_parent height for your recycler view item view. Because One item fills the whole screen so you do not see another.

CodePudding user response:

As @Mehul Kabaria said, the problem lies in the layout of your item views. The root view in pokemon_bank.xml has the layout_width and layout_height set to match_parent.

So actually all your items are displayed and if you scroll, you will see that they are there. But each item fills the whole screen which makes it look like only one item is displayed.

Change the width and height dimensions of the root view in pokemon_bank.xml to wrap_content or a fixed size.

<androidx.appcompat.widget.LinearLayoutCompat 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
    android:id="@ id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:textSize="18sp"
    android:text="Pikachu">
</TextView>

<TextView
    android:id="@ id/type"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom|end"
    android:padding="10dp"
    android:text="Electrik">
</TextView>
</androidx.appcompat.widget.LinearLayoutCompat>

Depending on whether you use a LinearLayoutManager for a vertical or horizontal list, you can set either the width or the height to match_parent.

Edit:

Also, it looks like you have an error in the binding of your model data to the item view in the RecyclerView Adapter: This here uses static methods to get values from the class MyPokemonBank.

public void display(MyPokemonBank myPokemonBank) {

    this.mPokemonName.setText(MyPokemonBank.getName());
    this.mPokemonType.setText(MyPokemonBank.getType());

}

Instead, they should use the parameter (lowercase myPokemonBank):

public void display(MyPokemonBank myPokemonBank) {

    this.mPokemonName.setText(myPokemonBank.getName());
    this.mPokemonType.setText(myPokemonBank.getType());

}

CodePudding user response:

instead of this line :

mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false));

try this one hope will work for you

mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

CodePudding user response:

That is my second activity XML:

<androidx.recyclerview.widget.RecyclerView
    android:id="@ id/home_recyclerview_pokemonname"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
</androidx.recyclerview.widget.RecyclerView>

and pokemon_bank.xml

<androidx.appcompat.widget.LinearLayoutCompat 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:id="@ id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:textSize="18sp"
    android:text="Pikachu">
</TextView>

<TextView
    android:id="@ id/type"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom|end"
    android:padding="10dp"
    android:text="Electrik">
</TextView>
</androidx.appcompat.widget.LinearLayoutCompat>
  • Related