Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pass a string between classes

I have a simple question even though I can’t answer it myself.

I have a class which is my Api class with a single which expect a string query parameeter:

public Single<List<Products>> searchProducts(){
        return api.searchProducts();
    }

and I have a Activity class with the data that I need which is searchEt:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

String searchData = searchEt.toString();

my question is that how can I pass this searchEt to my Api class which need the String query?

api.searchProducts();

I tried to use interface but I’ve got confused.

>Solution :

In the Api class:

    public Single<List<Products>> searchProducts(String query){
    return api.searchProducts(query);
}

In the Activity class:

String searchData = searchEt.getText().toString();
api.searchProducts(searchData)
   .subscribeOn(Schedulers.io())
   .observeOn(AndroidSchedulers.mainThread())
   .subscribe(new SingleObserver<List<Products>>() {
       @Override
       public void onSubscribe(Disposable d) {
           // Handle the subscription
       }

       @Override
       public void onSuccess(List<Products> products) {
           // Handle the successful response
       }

       @Override
       public void onError(Throwable e) {
           // Handle the error
       }
   });

In the updated code, the searchProducts method in the Api class now accepts a query parameter of type String. You can pass the searchData value from your Activity class to this method when calling it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading