Passing volley response to listview using an ArrayAdapter
我不知道如何使用适配器(myadapter)将volley响应传递给listview。我是否使用myadapter.add()并传递它什么?我读了教程,经过几周的努力,我认为是时候寻求帮助了。所以如果你有时间,我很感激你能帮我。谢谢。我可以在文本视图中显示响应。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | 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:包括一个textview和listview。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?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> |
我的问题是如何使用arrayadapter将截击的响应传递给listview?
在这里:
1 2 3 | public void onResponse(JSONArray response) { } |
您应该遍历jsonarray,提取每个字符串并放入一个
1 | ArrayList<String> stringsFromJson = new ArrayList<>(); |
然后,在同一个位置,将值设置为适配器:
1 2 3 | myAdapter.clear(); myAdapter.addAll(stringsFromJson); myAdapter.notifyDataSetChanged(); |
另外,您的适配器应该定义为
编辑1
你需要考虑的一些事情:
为了提供一个示例,我选择了用形状名称填充ListView的简单替代方法。你可以尝试和改变其他的选择。
显示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?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"> <intent-filter> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
主要活动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | 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); } } |
我没有更改布局。