Reputation: 1205
I'm unable to figure out how to pass the volley response to the listview using the adapter(myadapter). Do I use myadapter.add() and pass it what? I read tutorials and after weeks trying, I think it's time to seek help. So if you have time, I'm grateful if you can help me out. Thanks. I'm able to show response in the textview.
package hfad.com.adapters;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends Activity {
//thorntech.com parsing jsonandroid using colley library
TextView results;
// URL of object to be parsed
String JsonURL = "https://raw.githubusercontent.com/ianbar20/JSON-Volley-Tutorial/master/Example-JSON-Files/Example-Array.JSON";
// This string will hold the results
String data = "";
// Defining the Volley request queue that handles the URL request concurrently
ListView myList;
RequestQueue requestQueue;
//Adding adapter and assign it -set- to a listview
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creates the Volley request queue
requestQueue = Volley.newRequestQueue(this);
// Casts results into the TextView found within the main layout XML with id jsonData
results = (TextView) findViewById(R.id.textView);
myList = (ListView) findViewById(R.id.listv);
final ArrayAdapter<String> myAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1);
ListView myList = (ListView) findViewById(R.id.listv);
myList.setAdapter(myAdapter);
// Creating the JsonArrayRequest class called arrayreq, passing the required parameters
//JsonURL is the URL to be fetched from
JsonArrayRequest arrayreq = new JsonArrayRequest(JsonURL,
// The second parameter Listener overrides the method onResponse() and passes
//JSONArray as a parameter
new Response.Listener<JSONArray>() {
// Takes the response from the JSON request
@Override
public void onResponse(JSONArray response) {
//Juan suggestion:
ArrayList<String> myArraylist
myArraylist = new ArrayList<String>();
}}
My mainactivity.xml: includes a textview and listview.
<?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"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity"
android:weightSum="1">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<ListView
android:id="@+id/listv"
android:layout_width="match_parent"
android:layout_height="89dp"/>
</LinearLayout>
My question is how to pass the response from volley to the listview using an arrayadapter?
Upvotes: 0
Views: 3249
Reputation: 5589
In here:
public void onResponse(JSONArray response) {
}
You should go through the JSONArray and extract each String and put into an ArrayList<String>
.
ArrayList<String> stringsFromJson = new ArrayList<>();
And then, also in the same place, you set the values to your adapter:
myAdapter.clear();
myAdapter.addAll(stringsFromJson);
myAdapter.notifyDataSetChanged();
Also, your adapter should be defined as ArrayAdapter<String>
.
EDIT 1
Some things you need to consider:
To provide an example I am choosing the simple alternative of populating the listview with name of the shapes. You can experiment and change for other alternatives.
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.apackagename.so_43691098">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name="SO_43691098">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Main Activity
package com.apackagename.so_43691098;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class SO_43691098 extends AppCompatActivity {
private static final String TAG = SO_43691098.class.getSimpleName();
//thorntech.com parsing jsonandroid using colley library
TextView results;
// URL of object to be parsed
String JsonURL = "https://raw.githubusercontent.com/ianbar20/JSON-Volley-Tutorial/master/Example-JSON-Files/Example-Array.JSON";
// This string will hold the results
String data = "";
// Defining the Volley request queue that handles the URL request concurrently
ListView myList;
RequestQueue requestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
// Creates the Volley request queue
requestQueue = Volley.newRequestQueue(this);
// Casts results into the TextView found within the main layout XML with id jsonData
results = (TextView) findViewById(R.id.textView);
myList = (ListView) findViewById(R.id.listv);
final ArrayAdapter<String> myAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1);
ListView myList = (ListView) findViewById(R.id.listv);
myList.setAdapter(myAdapter);
// Creating the JsonArrayRequest class called arrayreq, passing the required parameters
//JsonURL is the URL to be fetched from
JsonArrayRequest arrayRequest = new JsonArrayRequest(JsonURL,
new Response.Listener<JSONArray>(){
@Override
public void onResponse(JSONArray response) {
//Juan suggestion:
ArrayList<String> myArrayList = new ArrayList<>();
try {
JSONArray shapes = response.getJSONObject(1).getJSONArray("shapeArray");
for(int i=0; i<shapes.length(); i++) {
myArrayList.add(shapes.getJSONObject(i).getString("shapeName"));
}
} catch (JSONException e){
Log.e(TAG, "Getting shape name", e);
}
myAdapter.clear();
myAdapter.addAll(myArrayList);
myAdapter.notifyDataSetChanged();
}
},
new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, error.getMessage(), error);
}
}
);
requestQueue.add(arrayRequest);
}
}
I made no changes to the layout.
Upvotes: 1