0

I'm looking to conduct a query to my Parse database and display this information in a list view.

Currently in my activity I have setContentView of my activity to a xml document which has a list with the id resultsView.

In my Parse database I have a table named Testing and want to receive all the information containing the Strings "Test1" from the column "A". I then want to display this information in the resultsView list.

ParseQuery<ParseObject> query = ParseQuery.getQuery("Testing");

query.whereEqualTo("A","Test1");

query.findInBackground(new FindCallback<ParseObject>()

{
    public void done(List<ParseObject> resultsList, ParseException e) {
        if (e == null) {

        } else {

        }
    }

});

What is wrong with this query and how would I go about adding the queried information to the resultsView list?

1 Answers1

0

Have you initialized parse, like this?

Parse.initialize(_App, key1, key2);

To add your results to your resultView list do

public class CopyOfShareActivity extends Activity {

    String[] resultsAsString = {""};
    ListView listView;

    ArrayAdapter<String> adapter;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.search);
        adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, resultsAsString);
        ParseQuery<ParseObject> query = ParseQuery.getQuery("Testing");

        query.whereEqualTo("A","Test1");

        query.findInBackground(new FindCallback<ParseObject>(){
             public void done(List<ParseObject> resultsList, ParseException e) {
                 if (e == null) {
                    resultsAsString = new String[resultsList.size()];
                    for(int index = 0; index < resultsList.size(); index++){
                       //.get => .getString
                       resultsAsString[index] = resultsList.get(index).getString("field you want to display");
                    }
                    adapter.notifyDataSetChanged();

                 } else {
                     Log.w("Parse query", e.getMessage());
                 }
             }
        });

        listView = (ListView) findViewById(R.id.ResultView);
        listView.setAdapter(adapter);
    }
}

This should do it, I hope this time! :)

The Adapter has android.R.layout.simple_list_item_1

which is the default layout for how a listView should look like. Consider it how the listView will be styled. For example if you wanted it to look different, you could design your own layout and replace 'simple_list_item_1' with the name of your custom ListView representation!

Bart de Ruijter
  • 917
  • 10
  • 21
  • Haha, the code compiles, but the application crashes when launching the activity –  Feb 11 '15 at 15:21
  • Alright 2 things: is this still being called first? "Parse.initialize(_App, key1, key2);" and if yes, could you provide me with a stacktrace? – Bart de Ruijter Feb 11 '15 at 15:22
  • Yeah basically I have a utility class that holds all of that information , it works when posting to the database –  Feb 11 '15 at 15:27
  • I'm an idiot, sorry. Basically what happens is that the Callback is executed after the results of the query. Which means that the code that initializes the array is executed after the listView has been set, which has been supplied with a null array which your app mustn't like for obvious reasons. Anyways, lemme update the answer again – Bart de Ruijter Feb 11 '15 at 15:33
  • It's getting there, seems to be a problem with the **add** in listview.getAdapter().add(resultsAsString[index]); and the notifyDatasetChanged call –  Feb 11 '15 at 15:46
  • Okay if you try to remove ' resultsAsString[index].add(resultsAsString[index]);' and use adapter.notifyDatasetChanged? – Bart de Ruijter Feb 11 '15 at 15:51
  • More specific? What exactly is it crashing on with what error? – Bart de Ruijter Feb 11 '15 at 15:59
  • I'm so terribly sorry for my slow response, but I have managed to find some free time and set up a lil file to figure out why it said it's an unresolved method and little mistakes I made and I think the answer now should be correct, known and should work. So terribly sorry for the inconvience and big amount of time I have consumed so far of you! – Bart de Ruijter Feb 11 '15 at 18:15