Android ListView and Volley -
very new android , volley (double trouble :))
after trying listview array of strings(it worked , able see result) , volley retrieve 1 of online test api (it worked , see result)
when attempted combine 2 (the response volley, parse , pass adapter) , nothing shows. kindly point me solid tutorial combining volley , listview or show me how adapter display response correctly in listview.
hope out there time help.
my mainactivity.java
package com.example.web.listviewexample; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.widget.arrayadapter; import android.widget.listview; import android.widget.textview; 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.jsonobjectrequest; import com.android.volley.toolbox.volley; import org.json.jsonexception; import org.json.jsonobject; public class mainactivity extends appcompatactivity { //array of strings... string[] mobilearray = {"android", "iphone", "windows", "webos", "blackberry", "max os x"}; textview results; string jsonurl ="https://reqres.in/api/users/2"; string data=""; //define volley request queue. handles requests requestqueue requestqueue; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //get listview communicate adapter listview listexample = (listview) findviewbyid(r.id.mobile_list); //prepare adapter final arrayadapter adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1); //attach adapter listexample.setadapter(adapter); //volley stuff --cricket_007 stackoverflow.com answer final jsonobjectrequest obj= new jsonobjectrequest(request.method.get, jsonurl, new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { try { // parse response response = response.getjsonobject("data"); // add things adapter adapter.add(response.tostring()); } catch (jsonexception e) { e.printstacktrace(); } } }, new response.errorlistener(){ @override public void onerrorresponse(volleyerror error){error.printstacktrace(); } }); volley.newrequestqueue(this).add(obj); } } activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity" > <listview android:id="@+id/mobile_list" android:layout_width="match_parent" android:layout_height="wrap_content" > </listview> </linearlayout activity_listview.xml
<?xml version="1.0" encoding="utf-8"?> <textview xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/label" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textsize="25sp" android:padding="10dp" android:textstyle="bold"> </textview>
how incorporate adapter volley?
first, think need fix url, add adapter arraylist
second, second parameter arrayadapter needs layout contains id of android:id/text1 if aren't going provide additional arguments (it's fine didn't know that, buried in documentation). being said, there's built-in layout single textview
//get listview listview listview = (listview) findviewbyid(r.id.mobile_list); // make adapter , set final arrayadapter<string> adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1); listview.setadapter(adapter); // volley things final jsonobjectrequest obj= new jsonobjectrequest(request.method.get, jsonurl, new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { try { // parse response response = response.getjsonobject("data"); // add things adapter adapter.add(response.tostring()); 
Comments
Post a Comment