我对 android 非常陌生,对 Parse 也非常陌生。我创建了一个类 StudentInformation,其中包含姓名、地址、电话等列。 我想创建一个 ListView ,其中包含添加到类(class)的所有学生的姓名。
我该怎么做?我已经到了可以吐出所有条目的 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.... 方法中我 toatst 出 objectID 的地方。
一如既往,我们将不胜感激。
应该提到(可能),没有错误被抛出, ListView 在 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();
}
}
});
最佳答案
这里有 2 个问题: 1)您的listAdapter 包含列表中显示的数据。您需要在运行查询之前创建它,并让您的查询直接将值添加到 listAdapter。然后,您需要在完成填充后调用 listAdapter.notifyDataSetChanged(),以便使用新数据重新绘制列表。 2) 将值添加到 studentList 的函数是添加“name”.toString()。您可能想调用 studentList.add(objects.get(i));
关于java - 尝试使用 Parse 中的对象在 Android 中填充 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13573847/