Camadas
Camadas

Reputation: 509

Adapter of RecyclerView, not working as intended

I have 2 adapters, they are all most the exact same code, the only difference is one receives one type of object and the other, other object.

This is the one that I'm having problems

public class BuyHelpAdapter extends RecyclerView.Adapter<BuyHelpAdapter.MyViewHolder>
{
    private List<LastPrices> _list;
    public class MyViewHolder extends RecyclerView.ViewHolder
    {
        public TextView data, nome;
        public MyViewHolder(View view)
        {
            super(view);
            data = view.findViewById(R.id.ProdListRowCodBarras);
            nome = view.findViewById(R.id.ProdListRowNome);
        }
    }

    public BuyHelpAdapter(List<LastPrices> list)
    {
        this._list = list;
    }

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

    @Override
    public void onBindViewHolder(MyViewHolder holder, int pos)
    {
        LastPrices lastPrices = this._list.get(pos);
        holder.data.setText(lastPrices.getData());
        holder.nome.setText(lastPrices.getTerceiro());
    }

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

I'm using the same way but when using this one the RecyclerView it will be filled but without any data.

Edit: This what I have inside the onCreate many of this is done in AsyncTaskRunner (use both adapter like this and the other adapter works at 100% like I mention)

_recyclerViewList.setHasFixedSize(true);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        _recyclerViewList.setLayoutManager(mLayoutManager);
        _recyclerViewList.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        _recyclerViewList.setItemAnimator(new DefaultItemAnimator());

This part is inside of the AsyncTaskRunner

_recyclerViewList.removeAllViews();
_recyclerAdapter = new BuyHelpAdapter(_listLastPrices);
_recyclerViewList.setAdapter(_recyclerAdapter);

The xml here the information must go (use this one on both adatpers)

<?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="55dp">

    <TextView
        android:id="@+id/ProdListRowCodBarras"
        android:layout_width="0dp"
        android:layout_height="19dp"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="5dp"
        android:textColor="@android:color/black"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/ProdListRowNome"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="5dp"
        android:textColor="@android:color/black"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ProdListRowCodBarras" />
</android.support.constraint.ConstraintLayout>

I have double checked the _listLastPrices it as 3 objets (the RecyclerView its to be filled with the 3 objects) when running through the MyViewHolder onCreateViewHolder and the void onBindViewHolder the information is there but in the end the recycleView as 3 items inside with no data at all (know this because it fill's the separator)

What is wrong here? The other adapter that just receives a different object works at 100% without any type of problems

Upvotes: 1

Views: 889

Answers (1)

Faysal Ahmed
Faysal Ahmed

Reputation: 7669

TextView width not correctly set. Plase try to replace for both textviews

android:layout_width="0dp"

to

android:layout_width="wrap_content"

This will solve the problem.

Upvotes: 1

Related Questions