Frank Eno
Frank Eno

Reputation: 2649

Android ListView adapter not working with Parse SDK

I'm an absolute beginner with Android, I can successfully retrieve data from my Parse database but I can't display them into a custom ListView, the wired thing is that the app stops but neither the Android Monitor or Message consoles in Android Studio give me any error, the Message console actually says Information:BUILD SUCCESSFUL, the Logcat is totally empty but the app stops with an Alert like Unfortunately, YourApp has stopped

Here's my code in the MainActivity.java:

 // MARK: - QUERY EVENTS
public  void queryEvents() {

    final ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,
            R.layout.test_cell);
    final ListView eventsListView = (ListView) findViewById(R.id.eventsListView);
    eventsListView.setAdapter(listAdapter);

    // Query
    ParseQuery<ParseObject> query = ParseQuery.getQuery("EventsClass");
    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> objects, ParseException error) {
            if (error == null) {
                eventsArray = objects;
                progDialog.dismiss();

                for (int i = 0; i < eventsArray.size(); i++) {
                    ParseObject eventClass = eventsArray.get(i);
                    String title = eventClass.getString("title");
                    Toast.makeText(getApplicationContext(), eventClass.getString("title"), Toast.LENGTH_LONG).show();
                    listAdapter.add(title);
                }

            } else {
                Toast.makeText(getApplicationContext(), error.getMessage().toString(), Toast.LENGTH_LONG).show();
            }

        }
    });

Note: If i comment listAdapter.add(title); out, the Toast message is fine, it fires all the "title" records from my Parse database, one after the other, and the app doesn't crash. Otherwise, I get no data and the app stops as mentioned above.

My R.layout.test_cell is a simple xml file and it's correct, it hosts a LinearLayout with a TextView inside.

What am I doing wrong here? I haven't created any custom ListAdapter.java file, don't know if i really need it with the code above...

Thanks!

Upvotes: 0

Views: 122

Answers (1)

Carlos J
Carlos J

Reputation: 3045

I think your problem in based on how you init listAdapter, I'd recommend for you to do two things:

1) Create an ArrayList<String> to save all the records that you get from parse

2) To init listAdapter make sure to use the constructor that takes as parameters Context, layout, id and ArrayList. The first two parameters you already have, just add the third which would be the id of the TextView in R.layout.test_cell and for the fourth parameter add the ArrayList that you create. Make sure to initialise it before this point using new ArrayList<>()

3) When you receive your values from parse update the ArrayList instead of updating the Adapter directly. Something like this:

    for (int i = 0; i < eventsArray.size(); i++) {
                ParseObject eventClass = eventsArray.get(i);
                String title = eventClass.getString("title");
                arrayList.add(title);
    }
    // Once you update your ArrayList you can update the Adapter
    listAdapter.notifyDataSetChanged();

That should do the trick.

Upvotes: 1

Related Questions