android - My RecyclerView activity displays blank activity -
i have developed application displays list made using recyclerview , card view viewing tutorials(simplified coding).the cardview has 2 text views , 1 image view.i want data mysql databse , image url loaded image view using picasso. application running not displaying , comes error response.
my app gradle file is
apply plugin: 'com.android.application' android { compilesdkversion 24 buildtoolsversion "26.0.0" defaultconfig { applicationid "com.example.selvam.listapp" minsdkversion 14 targetsdkversion 24 versioncode 1 versionname "1.0" testinstrumentationrunner "android.support.test.runner.androidjunitrunner" } buildtypes { release { minifyenabled false proguardfiles getdefaultproguardfile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile filetree(dir: 'libs', include: ['*.jar']) androidtestcompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:24.2.1' compile 'com.android.support.constraint:constraint-layout:1.0.2' compile 'com.android.support:recyclerview-v7:24.2.1' compile 'com.android.support:cardview-v7:24.2.1' compile 'com.squareup.picasso:picasso:2.5.2' compile 'com.android.volley:volley:1.0.0' testcompile 'junit:junit:4.12' }
my recycler_view xml file is
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.constraintlayout 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="com.example.selvam.listapp.mainactivity"> <android.support.v7.widget.recyclerview android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/myrecyclerview" tools:layout_constrainttop_creator="1" tools:layout_constraintright_creator="1" android:layout_marginstart="8dp" android:layout_marginend="8dp" app:layout_constraintright_torightof="parent" tools:layout_constraintleft_creator="1" app:layout_constraintleft_toleftof="parent" app:layout_constrainttop_totopof="parent" android:layout_marginleft="8dp" android:layout_marginright="8dp"></android.support.v7.widget.recyclerview> </android.support.constraint.constraintlayout>
my card_view xml file is
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:weightsum="1"> <android.support.v7.widget.cardview android:layout_width="match_parent" android:layout_height="219dp" android:layout_weight="0.16"> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:textsize="40sp" android:id="@+id/tvname" /> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:textsize="40sp" android:id="@+id/tvprice" /> <imageview android:id="@+id/imageview" android:layout_width="309dp" android:layout_height="148dp" android:layout_marginleft="30dp" android:layout_marginstart="30dp"/> </linearlayout> </android.support.v7.widget.cardview> </linearlayout>
'my items listed' in class
package com.example.selvam.listapp; /** * created selvam on 11-09-2017. */ public class listitems { private string name; private string price; private string image; public listitems(string name,string price,string image) { this.image=image; this.price=price; this.name=name; } public string getname() { return name; } public string getprice() { return price; } public string getimage() { return image; } }
my recycler adapter is
package com.example.selvam.listapp; import android.content.context; import android.support.v7.widget.recyclerview; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.imageview; import android.widget.textview; import com.squareup.picasso.picasso; import java.util.list; /** * created selvam on 11-09-2017. */ public class recycleradapter extends recyclerview.adapter<recycleradapter.viewholder> { private list<listitems> listitems; private context context; public recycleradapter(list<listitems> listitems, context context) { this.listitems = listitems; this.context = context; } @override public viewholder oncreateviewholder(viewgroup parent, int viewtype) { view view= layoutinflater.from(parent.getcontext()).inflate(r.layout.card_view,parent); return new viewholder(view); } @override public void onbindviewholder(viewholder holder, int position) { listitems item=listitems.get(position); holder.tvname.settext(item.getname()); holder.tvprice.settext(item.getprice()); picasso.with(context).load(item.getimage()).into(holder.imageview); } @override public int getitemcount() { return listitems.size(); } public class viewholder extends recyclerview.viewholder { public textview tvname; public textview tvprice; public imageview imageview; public viewholder(view itemview) { super(itemview); tvname=(textview)itemview.findviewbyid(r.id.tvname); tvprice=(textview)itemview.findviewbyid(r.id.tvprice); imageview=(imageview)itemview.findviewbyid(r.id.imageview); } } }
my main activity json parsing using volley is
package com.example.selvam.listapp; import android.app.progressdialog; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.widget.toast; import com.android.volley.request; import com.android.volley.requestqueue; import com.android.volley.response; import com.android.volley.volleyerror; import com.android.volley.toolbox.stringrequest; import com.android.volley.toolbox.volley; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import java.util.arraylist; import java.util.list; public class mainactivity extends appcompatactivity { private static final string url_list="http://192.168.28.2/signup/activities/getdatatolistview.php"; private recyclerview recyclerview; private recyclerview.adapter adapter; private list<listitems> listitems; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); recyclerview=(recyclerview)findviewbyid(r.id.myrecyclerview); recyclerview.sethasfixedsize(true); recyclerview.setlayoutmanager(new linearlayoutmanager(this)); listitems=new arraylist<>(); loadrecyclerview(); } public void loadrecyclerview(){ final progressdialog progressdialog=new progressdialog(this); progressdialog.setmessage("loading lists..."); progressdialog.show(); stringrequest stringrequest=new stringrequest(request.method.get, url_list, new response.listener<string>() { @override public void onresponse(string response) { progressdialog.dismiss(); try { jsonobject jsonobject=new jsonobject(response); jsonarray array=jsonobject.getjsonarray("list"); for(int i=0;i<array.length();i++){ jsonobject js=array.getjsonobject(i); listitems item=new listitems( js.getstring("name"),js.getstring("price"),js.getstring("image") ); listitems.add(item); } adapter=new recycleradapter(listitems,getapplicationcontext()); recyclerview.setadapter(adapter); } catch (jsonexception e) { e.printstacktrace(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { progressdialog.dismiss(); toast.maketext(getapplicationcontext(),"lists cannot displayed",toast.length_short).show(); } } ); requestqueue requestqueue= volley.newrequestqueue(this); requestqueue.add(stringrequest); } }
my json output mysql database is
{"list":[{"name":"messi","price":"hundred","image":"http:\/\/192.168.28.2\/img\/messi.jpeg"},{"name":"goat","price":"twohundred","image":"http:\/\/192.168.28.2\/img\/goat.jpg"},{"name":"cristiano","price":"threehundred","image":"http:\/\/192.168.28.2\/img\/cr7.jpg"}]}
i searched , tried lot. please me mistake. in advance
try it!
step 1: check width of recyclerview = 0dp. try change = "match_parent". if not working, try step2
step 2: try code below:
public void loadrecyclerview(){ final progressdialog progressdialog=new progressdialog(this); progressdialog.setmessage("loading lists..."); progressdialog.show(); adapter=new recycleradapter(listitems,getapplicationcontext()); recyclerview.setadapter(adapter); jsonobjectrequest getrequest = new jsonobjectrequest(request.method.get, url, null, new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { // display response log.d("response", response.tostring()); try { jsonarray array=response.getjsonarray("list"); for(int i=0;i<array.length();i++){ jsonobject js=array.getjsonobject(i); listitems item=new listitems( js.getstring("name"),js.getstring("price"),js.getstring("image") ); listitems.add(item); } } catch (jsonexception e) { e.printstacktrace(); } adapter.notifydatasetchanged(); } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { log.d("error.response", response); } } ); requestqueue requestqueue= volley.newrequestqueue(this); requestqueue.add(getrequest); }
Comments
Post a Comment