Reputation: 825
I am pretty darned new to android, and VERY new to Parse. I have created a class, StudentInformation, that includes columns for name, address, phone, etc. I would like to create a listview that contains the names of all students added to the class.
How do I do this? I have got it to the point that I can Toast out the objectIDs of all of my entries, but can't figure out how to extract and add just the names to the ListView.
Here is a snippet of the code:
//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);
}
I have left the toast in where I toatst out the objectIDs in the public void done.... method.
Any help would be, as always, greatly appreciated.
Should be mentioned (possibly), no errors are thrown, the listview just never gets populated after the toast disappears.
Don't know if this will help anyone, but I took a bit from both posts below, and came up with this:
//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();
}
}
});
Upvotes: 3
Views: 6320
Reputation: 31
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>();
Upvotes: 1
Reputation: 31
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);
}
Upvotes: 2
Reputation: 21181
May be this turorial will help you. check once..
and also check this How to populate listview from Json
Upvotes: 1
Reputation: 679
You have 2 issues here: 1) Your listAdapter contains the data that is displayed in the list. You need to create this before your run your query and get your query to add the values to the listAdapter directly. You will then need to call listAdapter.notifyDataSetChanged() when you have finished populating it, in order for the list to repaint using the new data. 2) Your function for adding the value to the studentList is adding "name".toString(). You probably are wanting to call studentList.add(objects.get(i));
Upvotes: 2