首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不能使用android-volley从JSON数组获取数据到自定义列表视图中。

不能使用android-volley从JSON数组获取数据到自定义列表视图中。
EN

Stack Overflow用户
提问于 2019-07-31 10:55:15
回答 3查看 57关注 0票数 1

因此,对于我的最后一个问题:Creating Json file with C# of mysql data,我尝试从创建的JSON /file中获取数据,但是它只是没有加载请求的页面,并且在Android仿真器上没有出现任何错误。这是我的截击码:

代码语言:javascript
复制
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.studentss);
    allstudents=new ArrayList<>();
    student_lists=findViewById(R.id.students_listview);

    postClass=new PostClass(allstudents,this);
    student_lists.setAdapter(postClass);
    mQueue=Volley.newRequestQueue(this);     

    jsonParse()

    private void jsonParse(){
    String url=".......WebForm1.aspx";
    JsonObjectRequest request=new JsonObjectRequest(Request.Method.GET, 
    url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
      JSONArray jsonArray=response.getJSONArray("Students");
                        for(int i=0;i<jsonArray.length();i++){
                            JSONObject stu =jsonArray.getJSONObject(i);
                            String stuName=stu.getString("firstname");

                            allstudents.add(stuName);

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

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
             error.printStackTrace();
        }
    });
    mQueue.add(request);

}

如果你能告诉我我的错,我会非常感激的。我认为这个问题必须用android做一些事情,而不是json,因为,我可以得到数据,但不能显示它!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-07-31 11:32:37

onResponse()内部,获得响应后,需要使用更新的数据设置listView。

onResponse()中添加此代码

代码语言:javascript
复制
postClass=new PostClass(allstudents,this);
student_lists.setAdapter(postClass); or  postClass.notifyDataSetChanged();

就像这样-

代码语言:javascript
复制
    @Override
    public void onResponse(JSONObject response) {
        try {
            JSONArray jsonArray = response . getJSONArray ("Students");
            for (int i = 0;i < jsonArray.length();i++){
                JSONObject stu = jsonArray . getJSONObject (i);
                String stuName = stu . getString ("firstname");

                allstudents.add(stuName);
            }

       postClass=new PostClass(allstudents,this);//THİS LİNE GIVES AN ERROR LIKE THIS BUT ONLY USING THE NEXT LINE WOULD BE EFFICIENT AND ALSO SOLVE THE PROBLEM JUST WANTED TO EDIT

:postClass (ArrayList,android.app.Activity)不能应用于(ArrayList,匿名com.android.volley.Response.Listener)“

代码语言:javascript
复制
        postClass.notifyDataSetChanged();//THIS LINE IS ENOUGH 
        } catch (JSONException e) {
            e.printStackTrace();
        }
票数 1
EN

Stack Overflow用户

发布于 2019-07-31 11:02:56

这里有几点你必须检查: 1-‘学生’是正确的名字在你的Json,而不是‘学生’。JSON结果对象的结构->Array->Object。

我个人更喜欢使用GSON,因为它不需要手动循环就可以自动转换。

票数 0
EN

Stack Overflow用户

发布于 2019-07-31 11:33:04

我解决了这个问题,我补充道:

代码语言:javascript
复制
 allstudents.add(stuName);
 postClass.notifyDataSetChanged(); //THİS LİNE SOLVED İT
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57289138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档