-1

I am working in a recyclerView i am trying to make a recyclerView that has 2 layout but the problem is that when i run the emulator and click the button to show items in the recyclerview the application closes suddenly here's the code of the adapter

package com.example.abdelmagied.myapplication;

import android.content.Context;
import android.database.Cursor;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by AbdELMagied on 7/25/2017.
 */
public class recycleradapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    public Context context;
    public Cursor mycursor;
    public ArrayList<String> mydata;
    private static final int VIEW_TYPE_DARK = 0;
    private static final int VIEW_TYPE_LIGHT = 1;

    public recycleradapter(Context context, ArrayList<String> mydata) {
        this.context = context;
        this.mydata = mydata;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        int viewid;
        switch (viewType) {
            case VIEW_TYPE_DARK:
                viewid = R.layout.recycler_row_dark;
                break;
            case VIEW_TYPE_LIGHT:
                viewid = R.layout.recycler_row_light;
                break;
            default:
                throw new IllegalArgumentException("Invaild Value type : " + viewType);
        }
        View view = LayoutInflater.from(context).inflate(viewid, parent, false);

       if(viewid == VIEW_TYPE_DARK){
           return new ViewHolder0(view);
       }else{
           return new ViewHolder1(view);
       }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
         int typeid = getItemViewType(position);
         String xx  = mydata.get(position);
         String[] x   = xx.split(",");

          switch (holder.getItemViewType())
          {
              case VIEW_TYPE_DARK:{
                  ViewHolder0 viewholder = (ViewHolder0) holder;
                  viewholder.txt6.setText(x[1]);
                  viewholder.txt7.setText(x[2]);
                  break;
              }
              case VIEW_TYPE_LIGHT:{
                  ViewHolder1 viewholder = (ViewHolder1) holder;
                  viewholder.txt8.setText(x[1]);
                  viewholder.txt9.setText(x[2]);
                  break;
              }
          }
    }


    @Override
    public int getItemViewType(int position) {
        if (position % 2 == 0)
            return VIEW_TYPE_DARK;
        return VIEW_TYPE_LIGHT;
    }


    @Override
    public int getItemCount() {
        return mydata.size();
    }


    // for the recycler_row_light layout..
   class ViewHolder0 extends RecyclerView.ViewHolder
   {
      public TextView txt6;
       public TextView txt7;
       public ViewHolder0(View itemView) {
           super(itemView);
           txt6 = (TextView) itemView.findViewById(R.id.textView6);
           txt7 = (TextView) itemView.findViewById(R.id.textView7);
       }
   }

    // for the recycler_row_dark layout...
    class ViewHolder1 extends RecyclerView.ViewHolder{
        public TextView txt9;
        public TextView txt8;
            public ViewHolder1(View itemView){
                super(itemView);
                txt8 = (TextView) itemView.findViewById(R.id.textView8);
                txt9 = (TextView) itemView.findViewById(R.id.textView9);
            }
    }
}

here' the show activity at which i adjust the recyclerview

package com.example.abdelmagied.myapplication;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;

public class show extends AppCompatActivity{

    public RecyclerView mrecyclerview;
    public RecyclerView.LayoutManager mymanager;
    public RecyclerView.Adapter myadapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);


        // fetch all data from the database and put it in an arraylist
        mydatabase database = new mydatabase(this);
        Cursor alldatabase  = database.getData();
        String data = "";
        ArrayList<String> mydata = new ArrayList<String>();
        while(alldatabase.moveToNext()){
            data += alldatabase.getString(1);
            data += ",";
            data += alldatabase.getString(2);
            data += ",";
            data += alldatabase.getString(3);
            data += ",";
            data += alldatabase.getString(4);
            data += ",";
            data += alldatabase.getString(5);
            mydata.add(data);
            data = "";

        }
        mrecyclerview = (RecyclerView) findViewById(R.id.RecyclerId);
        mymanager     = new LinearLayoutManager(this);
        myadapter     = new recycleradapter(this , mydata);
        mrecyclerview.setLayoutManager(mymanager);
        mrecyclerview.setAdapter(myadapter);

    }

}

here's the activity show which contain the recyclerview

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_show"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.abdelmagied.myapplication.show">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/RecyclerId" />

</RelativeLayout>

here's the first layout which called recycler_row_dark

<?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"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@android:color/holo_blue_bright"
        android:id="@+id/textView8">

        <ImageView
            android:layout_width="90dp"
            android:layout_height="90dp"
            app:srcCompat="@android:drawable/stat_notify_missed_call"
            android:id="@+id/imageView" />

        <TextView
            android:text="NAme"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView8"
            android:textSize="35sp" />

        <TextView
            android:text=": Number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView9"
            android:layout_weight="1"
            android:textSize="25sp" />

    </LinearLayout>

</LinearLayout>

here's the second layout which called recycler_row_light

<?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"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
        android:background="@android:color/darker_gray">

        <ImageView
            android:layout_width="90dp"
            android:layout_height="90dp"
            app:srcCompat="@android:drawable/stat_notify_missed_call"
            android:id="@+id/imageView" />

        <TextView
            android:text="NAme"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView6"
            android:textSize="35sp" />

        <TextView
            android:text=": Number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView7"
            android:layout_weight="1"
            android:textSize="25sp" />

    </LinearLayout>

</LinearLayout>

here's a photo of my logcat enter image description here

and this photo when trying to make debugging enter image description here

Abd EL Magied
  • 141
  • 2
  • 9

1 Answers1

1

Change your code to like this

Cursor alldatabase  = database.getData();
String data = "";
ArrayList<String> mydata = new ArrayList<String>();

if (alldatabase.moveToFirst()) {
  while(!alldatabase.isAfterLast()) { 
  // If you use alldatabase.moveToNext() here, you will bypass the first row, which is WRONG
    ...
    alldatabase.moveToNext();
  } 
}

The problem is you are not checking if cursor returns null value set or not. so please check first and then do operation on that.

And this log entry also not helpful as it doesn't display any app issue related logs.

Update

One more problem is in your dark layout as you have given same id to linear layout and textview both so make that correction also.

Wasim K. Memon
  • 5,979
  • 4
  • 40
  • 55