Android ListView和截击

Android ListView和截击,android,listview,android-volley,Android,Listview,Android Volley,android和volley非常新(双重麻烦:) 在使用字符串数组尝试listview(它工作了,我能够看到结果)并截取其中一个在线测试api(它工作了,我看到结果)之后 当我尝试将这两种方法结合起来时(截取的响应,解析它并将其传递给适配器),结果什么也没有显示。有没有人能给我指点一些结合截击和listview的坚实教程,或者帮助我演示如何让适配器在listview中正确显示响应 希望有人有时间帮忙 My Mainactivity.java package com.example.web.lis

android和volley非常新(双重麻烦:)

在使用字符串数组尝试listview(它工作了,我能够看到结果)并截取其中一个在线测试api(它工作了,我看到结果)之后

当我尝试将这两种方法结合起来时(截取的响应,解析它并将其传递给适配器),结果什么也没有显示。有没有人能给我指点一些结合截击和listview的坚实教程,或者帮助我演示如何让适配器在listview中正确显示响应

希望有人有时间帮忙

My 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);
        }
    }
package com.example.web.listviewexample;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入android.widget.TextView;
导入com.android.volley.Request;
导入com.android.volley.RequestQueue;
导入com.android.volley.Response;
导入com.android.volley.VolleyError;
导入com.android.volley.toolbox.JsonObjectRequest;
导入com.android.volley.toolbox.volley;
导入org.json.JSONException;
导入org.json.JSONObject;
公共类MainActivity扩展了AppCompatActivity{
//字符串数组。。。
字符串[]mobileArray={“Android”、“Iphone”、“Windows”、“WebOs”、“BlackBerry”、“Max OS x”};
文本查看结果;
字符串JsonURL=”https://reqres.in/api/users/2";
字符串数据=”;
//定义截击请求队列。它处理请求
请求队列请求队列;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取将与适配器通信的listview
ListView listexample=(ListView)findViewById(R.id.mobile_列表);
//准备适配器
最终的ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1);
//连接适配器
listexample.setAdapter(适配器);
//凌空抽射的东西——cricket_007 Stackoverflow.com回答
final JsonObjectRequest obj=新的JsonObjectRequest(Request.Method.GET,JsonURL,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
//解析响应
response=response.getJSONObject(“数据”);
//向适配器添加内容
add(response.toString());
}
捕获(JSONException e){
e、 printStackTrace();
}
}
},new Response.ErrorListener(){
@凌驾
public void onErrorResponse(截击错误){error.printStackTrace();
}
});
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

如何将适配器与截击结合起来

首先,我认为您需要修复您的URL,然后您只需添加一个适配器,如Arraylist

其次,ArrayAdapter的第二个参数需要是一个布局,其中包含ID
android:ID/text1
,如果您不打算提供任何额外的参数(很好,您不知道,它隐藏在文档中)。也就是说,一个文本视图有一个内置的布局

    //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());
//获取列表视图
ListView ListView=(ListView)findViewById(R.id.mobile\u列表);
//制作适配器并设置它
最终的ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1);
setAdapter(适配器);
//截击
final JsonObjectRequest obj=新的JsonObjectRequest(Request.Method.GET,JsonUrl,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
//解析响应
response=response.getJSONObject(“数据”);
//向适配器添加内容
add(response.toString());

您的JsonUrl无效。它有一个分号,您有两个
Volley.newRequestQueue(此)
,其中只有一个needed@Marco在您的代码中,您正在截取请求下面创建适配器。截取请求默认是异步的,因此您希望在
onResponse
@Enzokie中创建适配器或分配值,不一定在内部,但在此之前,这是一个副本,感谢您和我的时间感谢你的这一行“adapter.add(response.toString())”…这是所有教程中缺少的部分。这是一个漫长的8小时阅读和尝试。我将返回upvote lol。再次需要你的帮助。我添加了你的代码(我更新了上面的activity_main.java)并得到了错误(为上面的错误附加了屏幕)…不管警告如何,它都应该能工作,但我忘了左边板球的
ArrayAdapter
。\u 007……你说得对。它工作了。非常感谢。你帮了我大忙。我很感激,周末过得很愉快。
    //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());