2017-04-08 105 views
0

很新的Android和截击(双重麻烦:))Android的ListView和排球

与字符串数组试图列表视图后(它的工作,我能看到的结果),并抽射从一个检索那些在线测试API(它的工作,我看到结果)

当我试图将两个(来自volley的响应,解析它,并将其传递到适配器)并没有显示。有人会友好地指向我一些结合了排列和列表视图的可靠教程,或者帮助向我展示如何让适配器在列表视图中正确显示响应。

希望有人在那里有时间来帮忙。

我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> 

收到此错误 enter image description here

+1

您的JsonUrl无效。它有一个分号 –

+1

你有两个'Volley.newRequestQueue(this)',其中只需要一个 –

+0

@Marco在你的代码中创建适配器在Volley请求下面。 Volley请求默认为Asynchronous,因此您需要创建Adapter或在'onResponse'中分配值。 – Enzokie

回答

1

如何将适配器与Volley结合?

首先,我认为你需要修复您的网址,然后你只需要添加一个适配器像一个ArrayList

其次,你要一个ArrayAdapter第二个参数必须包含的android:id/text1的ID布局如果你不打算提供任何额外的参数(没关系,那你就不知道了,它被埋在了文档中)。也就是说,有一个单一的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()); 
+0

谢谢你们俩的时间。我非常感谢cricket_007到你的这一行“adapter.add(response.toString())”...这是所有这些教程中缺少的部分。这是一个漫长的8小时阅读和尝试。我会回来upvote大声笑。 – Marco

+0

需要你的帮助。我添加了你的代码(我更新了上面的activity_main.java)并获取错误(附上上面的错误的屏幕)... – Marco

+0

它应该工作,不管那个警告,但我忘记了'ArrayAdapter '在左边 –