简体   繁体   中英

Pass string from onLoadFinished method to another activity

I have created a content retriever which calls data from the content provider in another application, I can read the data successfully, but I want to pass the data from the onLoadFinished Method in my content retriever to an activity which should display the data on button press. But I am unsure of how to do it, I tried using a getter and setter, but it does not print anything.

Here are the rows in my content provider:

String[] columns = new String[]{"_id", "item"};
MatrixCursor matrixCursor = new MatrixCursor(columns);


matrixCursor.addRow(new Object[]{1, "FOUND"});

//Test is a string object that gets serialized to Json
matrixCursor.addRow(new Object[]{2, new Gson().toJson(test)});


matrixCursor.setNotificationUri(getContext().getContentResolver(), uri);
return matrixCursor;

I want to get the data from the 2nd row and this is what I have in my onLoadFinished in my Retriever class:

class Retriever : LoaderManager.LoaderCallbacks<Cursor>{

private var dataFromJson = ""

//getters and setters
fun setData(data: String){
    this.dataFromJson = data
}

fun getData():String{
    return dataFromJson
}

//other Loader method such as onCreateLoader, etc.

override fun onLoadFinished(loader: Loader<Cursor>, data: Cursor?) {
        try
        {
            val success: Boolean
            if (data != null && data.getCount() > 0)
            {
                data.moveToFirst()
                val dataString = data.getString(1)
                //this works, I am able to find the "found" text at the first row
                success = dataString.equals("FOUND", ignoreCase = true)
                //move to 2nd row
                data.moveToNext()
                //get data from 2nd row does not work
                val JSonData = data.getString(1)
                setData(JSonData)

            }
            else
            {
               Timber.d( "onLoadFinished Unknown error occurred")
                success = false
            }
            //custom interface listener
            listener.onClassListener(success)
        }
        catch (e:Exception) {
            Timber.d("Error")
        }
    }
  }

And in my activity when I create a Retriever object and call the getData() method, it displays nothing.

class TestActivity{
 private lateinit var dataRetriever: Retriever

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test_activity)
        //I have a textView called dataTxt
       //displays nothing
        dataTxt.text = dataRetriever.getData()
    }
}

Any help or advice will be highly appreciated

This is because of the loading task is an async operation, when you call the getData() function, the value of dataFromJson is probably still an empty string.

To easily deal with this, you can declare an interface like this:

interface OnDataRetrieveListener{
    fun onDataLoad(dataString: String)
}

and then create an instance of it inside the Retriever

private lateinit var listener: OnDataRetrieveListener

fun setListener(listener: OnDataRetrieveListener){
    this.listener = listener
}

set the listener in your activity (if anonymously, it's like below):

 dataRetriever.setListener(object: Retriever.Listener{
        override fun onDataLoad(data: String) {
                    dataTxt.text = dataRetriever.getData()
        }
 })

callback the listener when the string is loaded in Retriever:

...
 val jsonData = data.getString(1)
 listener.onDataLoad(jsonData)
...

then the dataTxt will update

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM