我无法弄清楚如何使用适配器(myadapter)将凌空响应传递给listview。我是否使用myadapter.add()并将其传递给它?我阅读了教程,经过几周的尝试,我认为是时候寻求帮助了。所以如果你有时间,我很感激你能不能帮助我。谢谢。我能够在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>();
}}
我的mainactivity.xml:包括textview和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>
我的问题是如何使用arrayadapter将响应从volley传递到listview?
答案 0 :(得分:1)
在这里:
public void onResponse(JSONArray response) {
}
您应该浏览JSONArray并提取每个字符串并放入ArrayList<String>
。
ArrayList<String> stringsFromJson = new ArrayList<>();
然后,在同一个地方,您将值设置为适配器:
myAdapter.clear();
myAdapter.addAll(stringsFromJson);
myAdapter.notifyDataSetChanged();
此外,您的适配器应定义为ArrayAdapter<String>
。
编辑1
您需要考虑的一些事项:
为了举例,我选择了使用形状名称填充listview的简单替代方法。您可以尝试和更改其他替代方案。
清单
<?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>
主要活动
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);
}
}
我没有对布局进行任何更改。