I am getting error while displaying SQLite data in GridLayout using RecyclerViewAdapter

0

I am displaying the data in CardView from SQLite database. But I get error while displaying the data, I checked my code and it seems perfect, there might be some silly mistake. I am newbie to android. Any help is appreciated.

Here is RecyclerViewAdapter.java file

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

    private Context mContext;
    private List<Card> mData;
    public RecyclerViewAdapter(Context mContext, List<Card> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view;
        LayoutInflater mInflator = LayoutInflater.from(mContext);
        view = mInflator.inflate(R.layout.home_view, viewGroup, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
        Card mcard = mData.get(i);
        myViewHolder.title.setText(mcard.getTitle());
    }
    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{
        public TextView title;


        public MyViewHolder(View itemView){
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.title);
        }
    }
}

This is home_view.xml which contains a TextView inside CardView.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_rowWeight="1"
    android:layout_columnWeight="1"
    android:layout_margin="12dp"
    android:onClick="showDetails">
    <RelativeLayout
        android:id="@+id/c1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="16dp">
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dp"
            android:textColor="#6f6f6f"
            android:textSize="18sp"/>
    </RelativeLayout>
</android.support.v7.widget.CardView>

I am getting this error

2019-06-17 20:03:37.316 32343-32361/? E/.ne.cardmanage: Failed to send DDMS packet REAQ to debugger (-1 of 20): Broken pipe
2019-06-17 20:03:45.059 32343-32343/com.ne.cardmanager E/.ne.cardmanage: Invalid ID 0x0000000a.
2019-06-17 20:03:45.074 32343-32343/com.ne.cardmanager E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.ne.cardmanager, PID: 32343
    android.content.res.Resources$NotFoundException: String resource ID #0xa
        at android.content.res.Resources.getText(Resources.java:348)
        at android.widget.TextView.setText(TextView.java:5831)
        at com.ne.cardmanager.RecyclerViewAdapter.onBindViewHolder(RecyclerViewAdapter.java:38)
        at com.ne.cardmanager.RecyclerViewAdapter.onBindViewHolder(RecyclerViewAdapter.java:15)
        at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
        at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
        at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
        at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
        at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
        at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
        at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
        at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
        at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3336)
        at android.view.View.measure(View.java:23181)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
        at android.view.View.measure(View.java:23181)
        at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1308)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.widget.ScrollView.onMeasure(ScrollView.java:350)
        at android.view.View.measure(View.java:23181)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
        at android.view.View.measure(View.java:23181)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
        at android.view.View.measure(View.java:23181)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.view.View.measure(View.java:23181)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
        at android.view.View.measure(View.java:23181)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:716)
        at android.view.View.measure(View.java:23181)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2727)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1580)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1864)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1468)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7208)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1004)
2019-06-17 20:03:45.075 32343-32343/com.ne.cardmanager E/AndroidRuntime:     at android.view.Choreographer.doCallbacks(Choreographer.java:816)
        at android.view.Choreographer.doFrame(Choreographer.java:751)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:990)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6692)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
android
android-recyclerview
asked on Stack Overflow Jun 17, 2019 by Rajat Bhardwaj • edited Jun 17, 2019 by Rajat Bhardwaj

2 Answers

0

Quit this:

myViewHolder.title.setText(mcard.getTitle());

From the onBind() and put it into the constructor of MyViewHolder

Is the problem.

answered on Stack Overflow Jun 17, 2019 by Juan Sancho
0

I solved it by myself.

I forget to cast the value of mcard.getTitle() to String

myViewHolder.title.setText(String.valueOf(mcard.getTitle()));
answered on Stack Overflow Jun 18, 2019 by Rajat Bhardwaj

User contributions licensed under CC BY-SA 3.0