android - My application closes suddenly before showing RecyclerView -
i working in recyclerview trying make recyclerview has 2 layout problem when run emulator , click button show items in recyclerview application closes here's code of 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 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(); } // 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); } } // 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' show activity @ adjust 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 data database , put in 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 activity show contain 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 first layout 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 second layout 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>
change code this
cursor alldatabase = database.getdata(); string data = ""; arraylist<string> mydata = new arraylist<string>(); if (alldatabase.movetofirst()) { while(!alldatabase.isafterlast()) { // if use alldatabase.movetonext() here, bypass first row, wrong ... alldatabase.movetonext(); } }
the problem not checking if cursor returns null value set or not. please check first , operation on that.
and log entry not helpful doesn't display app issue related logs.
update
one more problem in dark layout have given same id linear layout , textview both make correction also.
Comments
Post a Comment