ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – 尝试使用Parse中的对象在Android中填充ListView

2019-08-26 02:00:19  阅读:135  来源: 互联网

标签:parse-platform android java


我对android非常了不起,对Parse来说非常新.我创建了一个类StudentInformation,其中包含名称,地址,电话等列.
我想创建一个列表视图,其中包含添加到课程中的所有学生的姓名.

我该怎么做呢?我已经知道我可以干掉所有条目的objectID,但无法弄清楚如何提取和添加名称到ListView.

以下是代码片段:

    //Set up the listview
    studentListView = (ListView)findViewById(R.id.listViewStudents);
    //Create and populate an ArrayList of objects from parse
    ParseQuery query = new ParseQuery("StudentInformation");
    final ArrayList<Object> studentList = new ArrayList<Object>();
    query.findInBackground(new FindCallback() {
        public void done(List<ParseObject> objects, ParseException e) {
            if (e == null) {
                Toast.makeText(getApplicationContext(), objects.toString(), Toast.LENGTH_LONG).show();
                for(int i = 0;i < objects.size(); i++){
                    objects.get(i);
                    studentList.add("name".toString());
                }
            } else {
                Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
            }
        }
    });

    //studentList.addAll(Arrays.asList(students));
    listAdapter = new ArrayAdapter<Object>(this,android.R.layout.simple_list_item_1,studentList);
    studentListView.setAdapter(listAdapter);
}

我已经离开了toast,我在public void done ….方法中删除了objectIDs.

任何帮助将一如既往地非常感谢.

应该提到(可能),没有抛出任何错误,在toast消失后listview只是永远不会被填充.

不知道这是否会对任何人有所帮助,但我从下面的两个帖子中得到了一点,并想出了这个:

    //Set up the listview
    studentList = new ArrayList<String>();
    //Create and populate an ArrayList of objects from parse
    listAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
    studentListView = (ListView)findViewById(R.id.listViewStudents);
    studentListView.setAdapter(listAdapter);
    final ParseQuery query = new ParseQuery("StudentInformation");
    query.findInBackground(new FindCallback() {
        public void done(List<ParseObject> objects, ParseException e) {
            if (e == null) {
                //Toast.makeText(getApplicationContext(), objects.toString(), Toast.LENGTH_LONG).show();
                for (int i = 0; i < objects.size(); i++) {
                    Object object = objects.get(i);
                    String name = ((ParseObject) object).getString("name").toString();
                    listAdapter.add(name);
               }
            } else {
                Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
            }
        }
    });

解决方法:

你有2个问题:
1)listAdapter包含列表中显示的数据.您需要在运行查询之前创建此查询并获取查询以直接将值添加到listAdapter.完成填充后,您需要调用listAdapter.notifyDataSetChanged(),以便使用新数据重新绘制列表.
2)将值添加到studentList的功能是添加“name”.toString().您可能想要调用studentList.add(objects.get(i));

标签:parse-platform,android,java
来源: https://codeday.me/bug/20190826/1725274.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有