Retrieving values stored in array in Parse and displaying them in the listView

836 views Asked by At

So I'm trying to retrieve the values stored in the PrescriptionArray column in Parse, which is an Array. I then want to display these values in the ListView lvPrescriptions. Any help would be greatly appreciated!!

  ParseQuery<ParseObject> query = ParseQuery.getQuery("PrescriptionObject");
    query.whereEqualTo("MedicalID", etMedicalID.getText().toString());
    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> prescriptionList, ParseException e) {

            if (e == null) {

                final List<String> arrayprescription = new ArrayList<String>();

                for (int i = 0; i < prescriptionList.size(); i++) {
                    ParseObject p = prescriptionList.get(i);
                    if (p.getList("PrescriptionArray") != null) {


                        arrayprescription.addAll(Arrays.asList("PrescriptionArray"));
                        ArrayAdapter adapter = new ArrayAdapter(PrescriptionsArrayActivity.this, android.R.layout.simple_list_item_1, arrayprescription);
                        lvPrescriptions.setAdapter(adapter);
                    }
2

There are 2 answers

2
AndroidDev21921 On

Try moving the setting of the listview and adapter outside of the parse query and then adding each item to the adapter. Also, do not know why you are creating/setting a list in the query.

ArrayAdapter adapter = new ArrayAdapter(PrescriptionsArrayActivity.this, android.R.layout.simple_list_item_1, arrayprescription);
lvPrescriptions.setAdapter(adapter);    

        ParseQuery<ParseObject> query = ParseQuery.getQuery("PrescriptionObject");
            query.whereEqualTo("MedicalID", etMedicalID.getText().toString());
            query.findInBackground(new FindCallback<ParseObject>() {
                public void done(List<ParseObject> prescriptionList, ParseException e) {

                    if (e == null) {

                        final List<String> arrayprescription = new ArrayList<String>();

                        for (int i = 0; i < prescriptionList.size(); i++) {
                            ParseObject p = prescriptionList.get(i);

    adapter.add(stateList.get(i));




                            }
2
Daniel López On
public void done (List < ParseObject > prescriptionList, ParseException e){
    if (e == null) {
        final List<String> arrayprescription = new ArrayList<String>();
        for (ParseObject p : prescriptionList) {
            if (p.getList("PrescriptionArray") != null) {
                for(Prescription prescriptionObject: p.getList("PrescriptionArray")) {
                    arrayprescription.add(prescriptionObject.getPropertyName());
                }
            }
        }
        ArrayAdapter adapter = new ArrayAdapter(PrescriptionsArrayActivity.this, android.R.layout.simple_list_item_1, arrayprescription);
        lvPrescriptions.setAdapter(adapter);
    }
}

Note that PrescriptionArrayshould contain a list of objects where you can access to their properties using getter methods of the object (or getting properties directly if its a Json), then because you are using an ArrayAdapter and not creating your own, you have to populate the ArrayAdapter data with String objects.