recyclerview根本不显示。只显示文本视图。我查看了其他问题,并使用了以前问题的所有答案。其他的答案建议,回收器的宽度不能设置为wrap content,或者在设置layout manager和我满足这些条件之后应该调用setadapter。
主要活动
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.main_recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
final MainAdapter adapter = new MainAdapter();
recyclerView.setAdapter(adapter);
List<FirebaseProduct> firebaseProductList = new ArrayList<>();
firebaseProductList = getData(); //Here is my Firebase code
adapter.setList(firebaseProductList);
}
}
活动\u main.xml
<RelativeLayout 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=".MainActivity">
<TextView
android:id="@+id/main_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/aktualne_produkty"
android:gravity="center"
android:background="@color/colorPrimary"
android:textAppearance="@style/TextAppearance.AppCompat.Large">
</TextView>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/main_recycler"
android:layout_height="wrap_content"
android:layout_width="match_parent"
tools:listitem="@layout/card_main"
android:layout_below="@id/main_text"/>
</RelativeLayout>
主适配器
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MainHolder> {
private List<FirebaseProduct> productList = new ArrayList<>();
@NonNull
@Override
public MainHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_main, parent, false);
return new MainHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull MainHolder holder, int position) {
FirebaseProduct product = productList.get(position);
Log.d("Prod holder", "name - " + product.getProductName());
Log.d("Prod holder", "num - " + product.getProductNumber());
holder.nameView.setText(product.getProductName());
holder.numView.setText(product.getProductNumber());
}
@Override
public int getItemCount() {
return productList.size();
}
public void setProductList(List<FirebaseProduct> productList1){
this.productList = productList1;
notifyDataSetChanged();
}
public List<FirebaseProduct> getList(){
return productList;
}
public static class MainHolder extends RecyclerView.ViewHolder{
private TextView nameView;
private TextView numView;
public MainHolder(View itemView){
super(itemView);
nameView = itemView.findViewById(R.id.product_name);
numView = itemView.findViewById(R.id.product_count);
}
}
}
主卡
<androidx.cardview.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:layout_weight="3">
<TextView
android:id="@+id/product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
</TextView>
<TextView
android:id="@+id/product_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/product_name">
</TextView>
</RelativeLayout>
<ImageView
android:id="@+id/product_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:contentDescription="@string/product_description">
</ImageView>
</LinearLayout>
</androidx.cardview.widget.CardView>
1条答案
按热度按时间wn9m85ua1#
你在打电话吗
adapter.setList(firebaseProductList);
在MainActivity
但是函数名MainAdapter
是setProductList()
. 所以把它改成adapter. setProductList(firebaseProductList)
另外,你要确定getData();
在MainActivity
应返回非空列表。您可以使用调试器来验证它,或者只需在之前添加一个log语句adapter.setList(firebaseProductList)
. 这样地: