RecyclerView onBindViewHolder () ошибка при загрузке данных из firebase - PullRequest
0 голосов
/ 25 апреля 2020

У меня есть данные в Firebase, которые я пытаюсь прочитать в RecyclerView с пользовательским файлом макета. я не знаю, что не так с кодом. (вручную добавленные данные)

Я использую макет ограничения для файла пользовательского макета.

Разрешения на чтение / запись верны, а ключ SHA1 также apply.

это как-то связано с макетом?

activity_shop имеет макет ограничения, в котором я добавил представление переработчика фиксированного размера, а затем создал собственный файл макета с макетом ограничения.

Вот мой код для ShopActivity. java.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shop);
    recyclerView = findViewById(R.id.my_Rv);

    final LinearLayoutManager layoutManager = new LinearLayoutManager(ShopActivity.this);
    recyclerView.setLayoutManager(layoutManager);

    DataRef = FirebaseDatabase.getInstance().getReference().child("Products");

    DataRef.keepSynced(true);
    LoadData();
}

Вот мой метод LoadData ().

private void LoadData() {
        options = new FirebaseRecyclerOptions.Builder<Products>().setQuery(DataRef,Products.class).build();
        adapter = new FirebaseRecyclerAdapter<Products, MyViewHolder>(options) {

            @Override
            protected void onBindViewHolder(@NonNull MyViewHolder holder, int position, @NonNull Products model) {
                holder.product.setText(model.getName());
                holder.net.setText(model.getNetweight());
                holder.price.setText(model.getPrice());
                Picasso.with(getApplicationContext()).load(model.getImage()).into(holder.imageView);


            }

            @NonNull
            @Override
            public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.blog_row,parent, false);
                return new MyViewHolder(v);
            }
        };
        adapter.startListening();
        recyclerView.setAdapter(adapter);
    }

Вот мой файл пользовательского макета.

Это файл blog_row. xml, который надувается.

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="@color/grey"
    android:elevation="90dp"
    android:orientation="vertical"

    app:cardCornerRadius="15dp">

    <Button
        android:id="@+id/add_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="Add2Cart"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.949"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/product_image"
        app:layout_constraintVertical_bias="0.785" />

    <ImageView
        android:id="@+id/product_image"
        android:layout_width="120dp"
        android:layout_height="114dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginTop="8dp"
        android:contentDescription="product_image"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.982"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/common_google_signin_btn_icon_dark_normal" />

    <TextView
        android:id="@+id/product_name"
        android:layout_width="283dp"
        android:layout_height="92dp"
        android:text="ProductName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/product_image"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.19" />

    <TextView
        android:id="@+id/price_text"
        android:layout_width="105dp"
        android:layout_height="40dp"
        android:layout_marginEnd="20dp"
        android:text="Price"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/add_btn"
        app:layout_constraintHorizontal_bias="0.093"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/product_name"
        app:layout_constraintVertical_bias="0.612" />

    <TextView
        android:id="@+id/net_text"
        android:layout_width="82dp"
        android:layout_height="25dp"
        android:layout_marginStart="8dp"
        android:text="Netweight"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/price_text"
        app:layout_constraintTop_toBottomOf="@+id/product_name"
        app:layout_constraintVertical_bias="0.395" />

</androidx.constraintlayout.widget.ConstraintLayout>

Logcat

com.abdul.dailydeck, PID: 24355
android.content.res.Resources$NotFoundException: String resource ID #0xa8
    at android.content.res.Resources.getText(Resources.java:339)
    at android.widget.TextView.setText(TextView.java:5572)
    at com.abdul.dailydeck.ShopActivity$1.onBindViewHolder(ShopActivity.java:47)
    at com.abdul.dailydeck.ShopActivity$1.onBindViewHolder(ShopActivity.java:41)
    at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:149)
    at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
    at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
    at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
    at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
    at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
    at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
    at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
    at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
    at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
    at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
    at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
    at android.view.View.layout(View.java:19682)
    at android.view.ViewGroup.layout(ViewGroup.java:6086)
    at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
    at android.view.View.layout(View.java:19682)
    at android.view.ViewGroup.layout(ViewGroup.java:6086)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:19682)
    at android.view.ViewGroup.layout(ViewGroup.java:6086)
    at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:446)
    at android.view.View.layout(View.java:19682)
    at android.view.ViewGroup.layout(ViewGroup.java:6086)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:19682)
    at android.view.ViewGroup.layout(ViewGroup.java:6086)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
    at android.view.View.layout(View.java:19682)
    at android.view.ViewGroup.layout(ViewGroup.java:6086)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at com.android.internal.policy.DecorView.onLayout(DecorView.java:761)
    at android.view.View.layout(View.java:19682)
    at android.view.ViewGroup.layout(ViewGroup.java:6086)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2557)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2257)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1432)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6826)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
    at android.view.Choreographer.doCallbacks(Choreographer.java:723)
    at android.view.Choreographer.doFrame(Choreographer.java:658)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)

1 Ответ

0 голосов
/ 25 апреля 2020

В вашем onBindViewHolder у вас есть:

holder.product.setText(model.getName());
holder.net.setText(model.getNetweight());
holder.price.setText(model.getPrice());

Кажется, что один из model.getX() (тот, что в строке 47, согласно вашей трассировке стека) возвращает целое число, а затем ваше текстовое представление пытается разрешить его как строковый ресурс, вызывая исключение.

Вам необходимо преобразовать его в String.

...