将数据从firebase加载到android recyclerview
我一直在尝试将firebase数据库中的数据提取到android studio中的recyclerview中,但每次尝试,应用程序都会不断崩溃。我尝试过调试和所有这些,但似乎没有任何帮助。 以下是我的代码: 错误 } 流行适配器 } 主要活动布局 流行项目布局 我在Firebase上的数据库 我真的很期待找到这个问题的解决方案,因为这个项目是我需要在年底前完成的:提前感谢这是-您正在ImageViewHolder中将ImageView转换为TextView prod_img的类型为TextView,id prodImag是ImageView的id:将数据从firebase加载到android recyclerview,android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,我一直在尝试将firebase数据库中的数据提取到android studio中的recyclerview中,但每次尝试,应用程序都会不断崩溃。我尝试过调试和所有这些,但似乎没有任何帮助。 以下是我的代码: 错误 } 流行适配器 } 主要活动布局 流行项目布局 我在Firebase上的数据库 我真的很期待找到这个问题的解决方案,因为这个项目是我需要在年底前完成的:提前感谢这是-您正在ImageViewHolder中将ImageView转换为TextView prod_img的类型为TextVie
因此,将产品类型更改为ImageView我已经更改了该类型,但似乎无法解决问题:我现在在进行更正后得到此错误:2019-12-19 14:39:51.499 6421-6428/?E/art:向调试器发送回复失败:断管2019-12-19 14:39:51.850 6421-6421/?E/RecyclerView:未连接适配器;跳过布局现在可以了,先生。我完全按照你们的建议做了:我做了更改,但什么都没起作用:你们做了什么更改,它现在说了什么?好消息:我做了你们推荐的,就是,将textView更改为imageView,它成功了。我真高兴,伙计们。谢谢:
--------- beginning of crash
2019-12-18 08:43:24.107 5409-5409/com.example.shoptest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.shoptest, PID: 5409
java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to
android.widget.TextView
at com.example.shoptest.PopularAdapter$ImageViewHolder.<init>(PopularAdapter.java:68)
at com.example.shoptest.PopularAdapter.onCreateViewHolder(PopularAdapter.java:33)
at com.example.shoptest.PopularAdapter.onCreateViewHolder(PopularAdapter.java:17)
at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6794)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline
(RecyclerView.java:5975)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
at
androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
at androidx.recyclerview.widget.LinearLayoutManager
.onLayoutChildren(LinearLayoutManager.java:612)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
at androidx.recyclerview.widget.RecyclerView.onMeasure(RecyclerView.java:3336)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1117)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:642)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at androidx.appcompat.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:403)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:687)
at android.view.View.measure(View.java:19734)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2271)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1358)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1607)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
at android.view.Choreographer.doCallbacks(Choreographer.java:683)
at android.view.Choreographer.doFrame(Choreographer.java:619)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
2019-12-18 08:43:24.107 5409-5409/com.example.shoptest E/AndroidRuntime: at
android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
package com.example.shoptest;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
public PopularAdapter adapter;
private DatabaseReference mDatabaseReference;
private List<Popular> mPopular;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
showPopularProducts();
}
public void showPopularProducts() {
mPopular = new ArrayList<>();
mDatabaseReference = FirebaseDatabase.getInstance().getReference("popular");
mDatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapShot: dataSnapshot.getChildren())
{
Popular popular = postSnapShot.getValue(Popular.class);
mPopular.add(popular);
}
adapter = new PopularAdapter(MainActivity.this, mPopular);
recyclerView.setAdapter(adapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(MainActivity.this,databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
public class Popular {
private String product_image;
private String product_name;
private String product_price;
public Popular(){}
public Popular(String product_image, String product_name, String product_price) {
this.product_image = product_image;
this.product_name = product_name;
this.product_price = product_price;
}
public String getProduct_image() {
return product_image;
}
public void setProduct_image(String product_image) {
this.product_image = product_image;
}
public String getProduct_name() {
return product_name;
}
public void setProduct_name(String product_name) {
this.product_name = product_name;
}
public String getProduct_price() {
return product_price;
}
public void setProduct_price(String product_price) {
this.product_price = product_price;
}
public class PopularAdapter extends RecyclerView.Adapter<PopularAdapter.ImageViewHolder> {
private Context mContext;
private List<Popular> mPopular;
public PopularAdapter(Context mContext, List<Popular> mPopular) {
this.mContext = mContext;
this.mPopular = mPopular;
}
@NonNull
@Override
public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.popular_item, parent, false);
return new ImageViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
Popular popularCur = mPopular.get(position);
holder.prod_name.setText(popularCur.getProduct_name());
holder.prod_price.setText(popularCur.getProduct_price());
Picasso.get()
.load(popularCur.getProduct_image())
.placeholder(R.drawable.ic_image)
.fit()
.centerCrop()
.into((Target) holder.prod_img);
}
@Override
public int getItemCount() {
return mPopular.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder {
public TextView prod_name, prod_price, prod_img;
public ImageViewHolder(@NonNull View itemView) {
super(itemView);
prod_name = itemView.findViewById(R.id.prodName);
prod_price = itemView.findViewById(R.id.prodPrice);
prod_img = itemView.findViewById(R.id.prodImag);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:paddingLeft="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:textSize="24dp"
android:text="Popular Products"
android:textColor="@color/colorPrimary"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="10dp"/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerView"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp">
<androidx.cardview.widget.CardView
android:layout_width="120dp"
android:layout_height="160dp"
android:layout_marginBottom="15dp"
android:elevation="6dp"
app:cardCornerRadius="6dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/prodImag"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:src="@drawable/ic_image" />
<TextView
android:textColor="@color/colorPrimary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:id="@+id/prodName"
android:text="Product"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="0dp"
android:layout_height="18dp"
android:layout_weight="1"
android:src="@drawable/ic_favorite"
android:tint="@color/colorPrimary"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_weight="3"
android:paddingRight="10dp"
android:text="$$200"
android:id="@+id/prodPrice"
android:textColor="@color/colorPrimary"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to
android.widget.TextView
<ImageView
android:id="@+id/prodImag"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:src="@drawable/ic_image" />