我有2个适配器,它们都是完全相同的代码,唯一的区别是一个接收一种类型的对象而另一个接收另一个对象。
这是我遇到问题的那个
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();
}
}
我使用相同的方法,但在使用这个时,RecyclerView将填充但没有任何数据。
编辑:我在onCreate
里面的内容很多是在AsyncTaskRunner中完成的(使用这两个适配器,其他适配器就像我提到的那样100%工作)
_recyclerViewList.setHasFixedSize(true);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
_recyclerViewList.setLayoutManager(mLayoutManager);
_recyclerViewList.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
_recyclerViewList.setItemAnimator(new DefaultItemAnimator());
此部分位于AsyncTaskRunner
内_recyclerViewList.removeAllViews();
_recyclerAdapter = new BuyHelpAdapter(_listLastPrices);
_recyclerViewList.setAdapter(_recyclerAdapter);
这里的信息必须是xml(在两个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>
我在_listLastPrices
和MyViewHolder onCreateViewHolder
信息存在时,已将void onBindViewHolder
双重检查为3个objets(RecyclerView将填充3个对象)但是最后,recycleView作为3个项目,根本没有数据(知道这是因为它填充了分隔符)
这里有什么问题?另一个只接收不同对象的适配器工作在100%而没有任何类型的问题
答案 0 :(得分:1)
TextView宽度未正确设置。 Plase尝试替换两个textviews
android:layout_width="0dp"
到
android:layout_width="wrap_content"
这将解决问题。