【发布时间】:2017-09-27 05:10:08
【问题描述】:
我无法弄清楚如何使用适配器(myadapter)将凌空响应传递给列表视图。我是否使用 myadapter.add() 并传递它什么?我阅读了教程,经过数周的尝试,我认为是时候寻求帮助了。因此,如果您有时间,如果您能帮助我,我将不胜感激。谢谢。我能够在文本视图中显示响应。
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?
【问题讨论】:
-
您究竟想在
ListView中显示什么?没有通用的解决方案可以将任意 JSON 放入ListView。该 URL 中的 JSON 最终是两个具有不同数据的不同数组 - 一个数组是具有名称和十六进制值的颜色,另一个是仅具有名称的形状。你想怎么处理? -
我只是想学习如何显示任何数组,所以我将使用哪个数组没有区别。让我们选择“带有名称和十六进制值的颜色”。该怎么做?
-
对于特定的 JSON,您已经将它作为
JSONArray获取,因此您将获取第一个对象 -JSONObject jo = response.getJSONObject(0)- 然后获取它的数组 -JSONArray colorArray = jo.getJSONArray("colorArray")- 然后循环得到你想要的。但是,你又想在ListView中显示什么,以及如何显示?你只想要名字吗?还是名称和十六进制值?两者都是Strings?一个TextView,还是两个?或者也许将名称的文本颜色设置为十六进制值?这就是我所说的“没有通用解决方案”的意思。我们不知道您希望列表是什么样子。 -
嗨,谢谢。我实际上是带着你的解释到达那里,但仍然需要最后一步。仅在列表视图的一个 textView 中显示名称。
标签: android listview android-volley android-adapter