【问题标题】:Android ListView and VolleyAndroid ListView 和 Volley
【发布时间】:2017-04-08 00:42:04
【问题描述】:

对 android 和 volley 非常陌生(双重麻烦 :))

在尝试了带有字符串数组的列表视图(它有效,我能够看到结果)并从其中一个在线测试 api 中检索(它有效,我看到了结果)

当我尝试将两者结合起来时(来自 volley 的响应,解析它并将其传递给适配器)并且没有显示任何内容。有人会向我指出一些结合 volley 和 listview 的可靠教程,或者帮助我向我展示如何让适配器在 listview 中正确显示响应。

希望有人有时间提供帮助。

我的 Mainactivity.java

package com.example.web.listviewexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {
    //Array of strings...
    String[] mobileArray = {"Android", "Iphone", "Windows", "WebOs", "BlackBerry", "Max OS x"};

    TextView results;
    String JsonURL ="https://reqres.in/api/users/2";
    String data="";

    //define the volley request queue. It handles requests
    RequestQueue requestQueue;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //get the listview that will communicate with the adapter
        ListView listexample = (ListView) findViewById(R.id.mobile_list);
        //prepare the adapter
        final ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
        //attach the adapter
        listexample.setAdapter(adapter);

        //Volley stuff --cricket_007 Stackoverflow.com answer
        final JsonObjectRequest obj= new JsonObjectRequest(Request.Method.GET, JsonURL, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    // parse the response
                    response = response.getJSONObject("data");

                    // add things to the adapter
                    adapter.add(response.toString());
                }
                    catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener(){
                @Override
                public  void onErrorResponse(VolleyError error){error.printStackTrace();
                }

            });

       Volley.newRequestQueue(this).add(obj);
        }
    }

Activity_Main.xml

<?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"
    tools:context=".MainActivity"
    >


    <ListView
    android:id="@+id/mobile_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    </ListView>

</LinearLayout

activity_listview.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textSize="25sp"
    android:padding="10dp"
    android:textStyle="bold">

</TextView>

收到此错误

【问题讨论】:

  • 您的 JsonUrl 无效。它有一个分号
  • 你有两个Volley.newRequestQueue(this),这里只需要一个
  • @Marco 在您的代码中,您正在 Volley 请求下创建适配器。 Volley 请求默认是异步的,因此您要创建适配器或在 onResponse 中分配值。
  • @Enzokie 不一定在里面,但肯定在之前

标签: android listview android-volley


【解决方案1】:

如何将适配器与 Volley 结合起来?

首先,我认为您需要修复您的 URL,然后您只需添加到 Arraylist 之类的适配器

其次,ArrayAdapter 的第二个参数必须是一个包含 ID 为 android:id/text1 的布局,如果你不打算提供任何额外的参数(你不知道这很好,它隐藏在文档中)。话虽如此,单个 TextView 有一个内置布局

    //Get the listview 
    ListView listView = (ListView) findViewById(R.id.mobile_list);
    // make adapter and set it
    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
    listView.setAdapter(adapter);

    // Do Volley things 
    final JsonObjectRequest obj= new JsonObjectRequest(Request.Method.GET, JsonUrl, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                // parse the response 
                response = response.getJSONObject("data");

                // add things to the adapter 
                adapter.add(response.toString());

【讨论】:

  • 感谢您的宝贵时间。感谢 cricket_007 对您的这一行“adapter.add(response.toString())”...这是所有这些教程中缺少的部分。这是一个漫长的 8 小时阅读和尝试。我会回来投票的,哈哈。
  • 再次需要您的帮助。我添加了你的代码(我更新了上面的activity_main.java)并得到了错误(附加了上面那个错误的屏幕)......
  • 不管该警告如何,它都应该可以工作,但我忘记了左侧的ArrayAdapter&lt;String&gt;
  • cricket_007 ......你说得对。有效。非常感谢。你帮了大忙。我很感激,周末愉快。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-27
  • 1970-01-01
  • 2013-12-08
  • 1970-01-01
  • 2014-07-07
  • 2023-03-24
  • 1970-01-01
相关资源
最近更新 更多