3

我对 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);
}

我把吐司留在了我在 public void done.... 方法中吐出 objectID 的地方。

一如既往,我们将不胜感激任何帮助。

应该提到(可能),不会抛出任何错误,在 toast 消失后列表视图永远不会被填充。

不知道这是否会对任何人有所帮助,但我从下面的两个帖子中获取了一些信息,并提出了这个:

    //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();
            }
        }
    });
4

4 回答 4

2

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

于 2012-11-27T02:41:56.093 回答
2
public void parseRetrieveAll(View view) throws ParseException {
 findViewById(R.id.listView1);
 ParseQuery query = new ParseQuery("TestObject");
 query.orderByDescending("foo");
 query.findInBackground(new FindCallback() {
 public void done(List<ParseObject> categories, ParseException e) {
    if (e == null) {
       for (int i = 0; i < categories.size(); i++) {
       String c = categories.get(i).getString("foo");
       foos.add(c); 
    } 
} else {
     Toast.makeText(getApplicationContext(), "Error",
    Toast.LENGTH_LONG).show();
    }
    } 
});
ListView lv = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, foos);
lv.setAdapter(arrayAdapter);
}
于 2012-12-09T01:44:03.640 回答
1

可能这个教程会帮助你。检查一次..

并检查这个如何从 Json 填充列表视图

于 2012-11-27T03:35:42.277 回答
1
Here is What I did:
* "foo" is the column name in my  "TestObject", which is my ParseObject
1: Inside the For Loop I added each value to a ListArray<String>
String c = categories.get(i).getString("foo");
foos.add(c);

2. Once Completed I Added the String Array to my ListView
ListView lv = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, foos);
lv.setAdapter(arrayAdapter);
3.  After that It worked.  Make sure that you declare the ArrayList<String> in your class.

Example:
ArrayList<String> foos = new ArrayList<String>();
于 2012-12-09T01:48:22.020 回答