JSONArray到Listview我做错了什么?

时间:2018-04-17 10:02:54

标签: java android json listview

大家好,我现在已经搜索了错误或者如何纠正错误一段时间,这里有很多问题让我更接近我的想法。

但我有一个API,我得到一个JSONArray,我想在列表视图中显示这些数据。到目前为止,我在我的日志中获取数据,现在我只是尝试显示每个对象的名称开始(然后我可以在以后添加其余的数据)但我不确定我是否在正确的位置也许有人可以帮助我在这里,并表明我做错了什么?

非常感谢

Listview名称:AllBirds

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

    mAuth = FirebaseAuth.getInstance();

    String URL="myAPI";

    RequestQueue requestQueue= Volley.newRequestQueue(this);


    JsonArrayRequest objectRequest = new JsonArrayRequest (Request.Method.GET, URL, null, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            Log.e("Rest Response",response.toString());

            try{
                for(int i=0; i<response.length();i++){
                    JSONObject jresponse = response.getJSONObject(i);
                    String NameDanish = jresponse.getString("NameDanish");

                    Log.d("NameDanish", NameDanish);


                }
            }catch(JSONException e){
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("Rest Response",error.toString());
        }
    });

    requestQueue.add(objectRequest);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_all_observation, NameDanish);

    ListView list = (ListView) findViewById(R.id.AllBirds);
    list.setAdapter(adapter);
}

5 个答案:

答案 0 :(得分:2)

在班级中创建ArrayList

ArrayList arrayList = new ArrayList&lt;&gt;();

使用

更改上述代码
  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_observation);

        mAuth = FirebaseAuth.getInstance();

        String URL = "myAPI";

        RequestQueue requestQueue = Volley.newRequestQueue(this);


        JsonArrayRequest objectRequest = new JsonArrayRequest(Request.Method.GET, URL, null, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                Log.e("Rest Response", response.toString());

                try {
                    for (int i = 0; i < response.length(); i++) {
                        JSONObject jresponse = response.getJSONObject(i);
                        String NameDanish = jresponse.getString("NameDanish");
                        arrayList.add(NameDanish);
                        Log.d("NameDanish", NameDanish);


                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("Rest Response", error.toString());
            }
        });

        requestQueue.add(objectRequest);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_all_observation, arrayList);

        ListView list = (ListView) findViewById(R.id.AllBirds);
        list.setAdapter(adapter);
    }

答案 1 :(得分:1)

您应该在成功时将适配器设置为

xrdb ~/.Xdefaults

并且不要忘记将arraylist声明为

 JsonArrayRequest objectRequest = new JsonArrayRequest (Request.Method.GET, URL, null, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            Log.e("Rest Response",response.toString());

            try{
                for(int i=0; i<response.length();i++){
                    JSONObject jresponse = response.getJSONObject(i);
                    String NameDanish = jresponse.getString("NameDanish");

                    Log.d("NameDanish", NameDanish);
                    name.add(NameDanish);


                }

                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_all_observation, name);

                ListView list = (ListView) findViewById(R.id.AllBirds);
                list.setAdapter(adapter);
            }catch(JSONException e){
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("Rest Response",error.toString());
        }
    });

答案 2 :(得分:1)

像这样

全局创建一个字符串arraylist
ArrayList<String> name_array=new ArrayList<>();

然后添加

         try{
            for(int i=0; i<response.length();i++){
                JSONObject jresponse = response.getJSONObject(i);
                String NameDanish = jresponse.getString("NameDanish");
                name_array.add(NameDanish);
                Log.d("NameDanish", NameDanish);
            }

然后将此arraylist传递给您的适配器

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_all_observation, name_array);

答案 3 :(得分:0)

创建listView,adapter,array作为成员变量,并使用来自responseHandler的notifyDataUpdate来更新数据。

    ListView mList;
    ArrayAdapter<String> mAdapter
    ArrayList<String> mData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_observation);
        mData=new ArrayList<>();

        mList = (ListView) findViewById(R.id.AllBirds);
        mAdapter = new ArrayAdapter<String>(this, R.layout.activity_all_observation, mData);
        list.setAdapter(adapter);

        mAuth = FirebaseAuth.getInstance();
        String URL="myAPI";

        RequestQueue requestQueue= Volley.newRequestQueue(this);


        JsonArrayRequest objectRequest = new JsonArrayRequest (Request.Method.GET, URL, null, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                Log.e("Rest Response",response.toString());

                try{
                    for(int i=0; i<response.length();i++){
                        JSONObject jresponse = response.getJSONObject(i);
                        String NameDanish = jresponse.getString("NameDanish");
                        mData.add(NameDanish);
                        Log.d("NameDanish", NameDanish);
                    }
                    mAdapter.notifyDataSetChanged();
                }catch(JSONException e){
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("Rest Response",error.toString());
            }
        });

        requestQueue.add(objectRequest);
    }

答案 4 :(得分:0)

我现在已将代码更改为此,因为Toyj说,我已将我的xml发布到。

我没有得到任何数据,只有日志中显示的所有名称?

<div id="parent">
  <div id="child"></div>
</div>

JSON数据看起来像这样我只需要从NameDanish开始

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

    mAuth = FirebaseAuth.getInstance();
    final ArrayList arrayList = new ArrayList<>()

    String URL="MYAPI";

    RequestQueue requestQueue= Volley.newRequestQueue(this);


    JsonArrayRequest objectRequest = new JsonArrayRequest (Request.Method.GET, URL, null, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            Log.e("Rest Response",response.toString());

            try{
                //JSONArray jsonArray = new JSONArray(response);
                for(int i=0; i<response.length();i++){
                    JSONObject jresponse = response.getJSONObject(i);
                    String NameDanish = jresponse.getString("NameDanish");
                    arrayList.add(NameDanish);
                    Log.d("NameDanish", NameDanish);


                }
            }catch(JSONException e){
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("Rest Response",error.toString());
        }
    });

    requestQueue.add(objectRequest);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_all_observation, arrayList);

    ListView list = (ListView) findViewById(R.id.AllBirds);
    list.setAdapter(adapter);
}

XML文件

    {
    "BirdId": 1,
    "Comment": null,
    "Created": "/Date(1523836800000+0000)/",
    "Id": 132,
    "Latitude": 55.6886037,
    "Longitude": 12.5310135,
    "Placename": "K",
    "Population": 0,
    "UserId": "user1",
    "NameDanish": "solsort",
    "NameEnglish": "blackbird"
}