Home How to put ParseQuery in seperate class and call this method in every activity in android?
Reply: 2

How to put ParseQuery in seperate class and call this method in every activity in android?

datacrusader
1#
datacrusader Published in 2018-02-14 08:55:24Z

i'm new to android and coding in general

i have many textviews in my app so i'm retrieving string from parse and displaying it in textview.

i'm using the following parse query

 ParseQuery<ParseObject> query=ParseQuery.getQuery("Topics");
    query.getInBackground("fRGt3oz8af", new GetCallback<ParseObject>(){
        public void done(ParseObject arg0, ParseException arg1) {

            if (arg1==null)
            {
                    subTopicName = arg0.getString("class_1");
                    textView.setText("" + subTopicName);

            }

            else
            {
                Log.d("topic", "Error: " + arg1.getMessage());
            }

}});

since i've many textview i have to repeat the same code for retrieve each string. so i'm trying to avoid code redundancy.

my question is how can i put the above query in different java class and call that method for retrieving each strings? or is there different method way to do it. I've never worked on parse server before so I'm really struggling with this.

Any help would be appreciated.

Senya.dev
2#
Senya.dev Reply to 2018-02-14 09:16:22Z

Try something like this:

interface ParseCallback {
    void onComplete(String result);
    //todo onError() ?
}

protected void parse(ParseCallback callback) {
    ParseQuery<ParseObject> query = ParseQuery.getQuery("Topics");
    query.getInBackground("fRGt3oz8af", new GetCallback<ParseObject>() {
        public void done(ParseObject arg0, ParseException arg1) {
            if (arg1 == null) {
                String subTopicName = arg0.getString("class_1");
                if (subTopicName != null) {
                    callback.onComplete(subTopicName);
                }
            } else {
                Log.d("topic", "Error: " + arg1.getMessage());
            }
        }
    });
}

public void example() {
    parse(result -> {
        runOnUiThread(() -> {
            myTextView.setText(result);
        });
    });
}
Sudhin Philip
3#
Sudhin Philip Reply to 2018-02-14 09:15:04Z

I would recommend to create observer pattern and on implementing the interface callback function you will able to get notified in your UI.

i.e. Create a new class extending ParseQuery and override done function. Create an interface in this class and call the interface function inside done function. Then implement this interface in your Activity class and update the UI.

Hope it give you some idea on how to achieve what you are looking for.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.305528 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO