Quantcast
Viewing latest article 28
Browse Latest Browse All 601

Passing volley response to listview using an ArrayAdapter

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?


Viewing latest article 28
Browse Latest Browse All 601

Trending Articles