1

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);
                    }
  • Don't you think you have to call setAdapter function on UI thread. And why you are calling setAdapter in For Loop. – Naeem Jan 20 '16 at 15:09
  • Thanks Naheem I didn't spot that.. What do you mean by on UI thread? Is this a seperate Activity?? – Ógie Murphy Jan 20 '16 at 16:48

2 Answers2

0

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




                            }
AndroidDev21921
  • 695
  • 1
  • 8
  • 21
  • Thanks very much I almost have it working!! However it seems to be displaying the column name "PrescriptionArray" as oppose to the contents of the array... Any suggestions? – Ógie Murphy Jan 20 '16 at 16:47
  • If you need to get a different column, you can either create a custom adapter or populate the list from your returned list of objects... p.get("something"). – AndroidDev21921 Jan 21 '16 at 00:52
0
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.

Daniel López
  • 716
  • 7
  • 9
  • Thanks very much I almost have it working!! However it seems to be displaying the column name "PrescriptionArray" as oppose to the contents of the array... Any suggestions? – Ógie Murphy Jan 20 '16 at 16:45
  • You have to create your custom adapter or access to a property of your `PrescriptionArray` object. I will edit my answer, using `propertyName` as the object property you want to show – Daniel López Jan 21 '16 at 09:33