Java 尝试使用Parse中的对象在Android中填充ListView
我对android非常陌生,也非常不熟悉解析。我创建了一个名为StudentInformation的类,其中包括姓名、地址、电话等列。 我想创建一个列表视图,其中包含添加到类中的所有学生的姓名 我该怎么做?我已经做到了,我可以把我所有条目的objectid都去掉,但却不知道如何提取并将名称添加到ListView中 以下是代码片段:Java 尝试使用Parse中的对象在Android中填充ListView,java,android,parse-platform,Java,Android,Parse Platform,我对android非常陌生,也非常不熟悉解析。我创建了一个名为StudentInformation的类,其中包括姓名、地址、电话等列。 我想创建一个列表视图,其中包含添加到类中的所有学生的姓名 我该怎么做?我已经做到了,我可以把我所有条目的objectid都去掉,但却不知道如何提取并将名称添加到ListView中 以下是代码片段: //Set up the listview studentListView = (ListView)findViewById(R.id.listVie
//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);
}
//设置listview
studentListView=(ListView)findViewById(R.id.listViewStudents);
//从parse创建并填充对象的ArrayList
ParseQuery查询=新的ParseQuery(“学生信息”);
最终ArrayList studentList=新ArrayList();
findInBackground(新的FindCallback(){
公共void done(列出对象,parsee异常){
如果(e==null){
Toast.makeText(getApplicationContext(),objects.toString(),Toast.LENGTH_LONG.show();
对于(int i=0;i
我把吐司放在我在公共场合吐司的地方了。。。。方法
我们将一如既往地非常感谢您的帮助
应该提到(可能),没有抛出错误,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();
}
}
});
//设置listview
studentList=newarraylist();
//从parse创建并填充对象的ArrayList
listAdapter=newarrayadapter(这是android.R.layout.simple\u list\u item\u 1);
studentListView=(ListView)findViewById(R.id.listViewStudents);
setAdapter(listAdapter);
最终ParseQuery查询=新ParseQuery(“学生信息”);
findInBackground(新的FindCallback(){
公共void done(列出对象,parsee异常){
如果(e==null){
//Toast.makeText(getApplicationContext(),objects.toString(),Toast.LENGTH_LONG.show();
对于(int i=0;i
这里有两个问题:
1) listAdapter包含列表中显示的数据。您需要在运行查询之前创建它,并让查询直接将值添加到listAdapter。然后需要在完成填充后调用listAdapter.notifyDataSetChanged(),以便使用新数据重新绘制列表。
2) 将值添加到studentList的函数是添加“name”.toString()。您可能想调用studentList.add(objects.get(i)) 也许这本旅游杂志会对你有所帮助。一次 并检查此
公共无效parseRetrieveAll(视图视图)引发ParseException{
findViewById(R.id.listView1);
ParseQuery查询=新的ParseQuery(“TestObject”);
query.orderByDescending(“foo”);
findInBackground(新的FindCallback(){
公共作废已完成(列表类别,e){
如果(e==null){
对于(int i=0;i
以下是我所做的:
*“foo”是我的“TestObject”中的列名,TestObject是我的ParseObject
1:在For循环中,我将每个值添加到ListArray中
字符串c=categories.get(i).getString(“foo”);
添加(c);
2.完成后,我将字符串数组添加到我的ListView中
ListView lv=(ListView)findViewById(R.id.listView1);
ArrayAdapter ArrayAdapter=新的ArrayAdapter(此,
android.R.layout.simple_list_item_1,foos);
低压设置适配器(阵列适配器);
3.在那之后,它起了作用。确保在类中声明ArrayList。
例子:
ArrayList foos=新的ArrayList();
我显然很困惑。。。我只想看到ListView上的学生姓名。那么,在“我给了我所有的对象,包括name和所有其他attr”中添加对象不是吗?是的,如果您按照我说的做,列表将包含从“toString()”返回的任何内容。如果您的对象是一个名为“Student”的类,并且Student有一个名为name的成员变量,那么您需要调用“Student s=(Student)objects.get(i);studentList.add(s.name);当您在最后向listview添加值时,即使它不是异步的,查找Background有什么意义?
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);
}
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>();