Trying to populate ListView in Android using objects from Parse

Posted by mrwienerdog on Stack Overflow See other posts from Stack Overflow or by mrwienerdog
Published on 2012-11-26T21:57:01Z Indexed on 2012/11/27 5:04 UTC
Read the original article Hit count: 333

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about java

Related posts about android