0

I'm new to android and I need help in retrieving the array that I created in parse server named as "busStops" but I can't figure out where the problem exists in my code

 ParseQuery<ParseObject> parseQuery = new ParseQuery<ParseObject> 
("RouteDetails");

 parseQuery.whereEqualTo("routeNumber",searchView.getText().toString());

    parseQuery.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> busStops, ParseException e) {
            if(e == null){
                final List<String> arrayList = new ArrayList<>();
                    for (ParseObject parseObject : busStops){
                        if (parseObject.getList("busStops") != null) {
                                arrayList.add(parseObject.getList("busStops").toString());
                        }
                    }
                arrayAdapter = new ArrayAdapter(SearchForRoutes.this,android.R.layout.simple_list_item_1,arrayList);
                listView.setAdapter(arrayAdapter);
            }

        }
    });
Tom Fox
  • 897
  • 3
  • 14
  • 34

2 Answers2

0

It is possible to show an Array in a ListView. You should use getJSONArray instead of getList in your JAVA code. See my code below.

My structure class is:

enter image description here

Then, I added the code below into my Activity (MainActivity.java):

final ListView listView = (ListView) findViewById(R.id.listviewA);

    ParseQuery<ParseObject> query = ParseQuery.getQuery("RouteDetails");
    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> results, ParseException e) {
            if (e == null) {
                for(int i = 0; i < results.size(); i++) {
                    JSONArray array = results.get(i).getJSONArray("busStop");

                    for(int j = 0; j < array.length(); j++) {
                        try {
                            dataList.add(array.getString(j));
                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }
                    }

                    myArray = dataList.toArray(new String[dataList.size()]);

                    ArrayAdapter<String> adapterList
                            = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_single_choice, myArray);

                    listView.setAdapter(adapterList);

                }
            } else {
                final Toast toast = Toast.makeText(
                        MainActivity.this,
                        String.valueOf("Error =>" + e.getMessage()),
                        Toast.LENGTH_LONG
                );
                toast.show();
            }
        }
    });

And the result will be something like as this:

enter image description here

nataliec
  • 502
  • 4
  • 14
0

First of all, you may don't need to create another ArrayList than the list that is returned by parseObject.getList("busStops"). You can directly pass it to the adapter constructor. But since you may want to do a process on the list, iterate the list like : for(Object s : parseObject.getList("busStops") { String current = s.toString(); }

Also you can save the list to another ArrayList by passing it to ArrayList constructor. ArrayList<String> al = new ArrayList<>(parseObject.getList("busStops"))

And finally, you can fix your code (that is worst-practice :) ) by simply deleting the .toString() from the line in your none-null if statement and changing add to addAll. like this : arrayList.addAll(parseObject.getList("busStops"));

AliAndArdDev
  • 181
  • 1
  • 8