Reputation: 1368
I am implementing JSON array which I retrieved using AsyncTask method for background process,
I have an array variable which is declared before OnCreate and initialized with some value,
String [] str=new String[100];
Storing in tat array variable while parsing the json array like this in a for loop,
str[i]=c.getString(TAG_NAME);
storing perfectly, I did Log and its showing in logcat too,
but the problem is when I use "str" array variable outside of my function like this,
void func()
{
List<String> top250 = new ArrayList<String>();
for(int x=0;x<str.length;x++)
{
top250.add(str[x]);
}
}
The str variable is storing only the string "null" for initialized memory.
It's storing "null" for 100 times.
So how to fetch the value which is processed in doInBackground method inside of AsyncTask?
Upvotes: 2
Views: 104
Reputation: 4762
Edit: Asyntask Example for reference Code and concept here.
As we Know AsynTask has three parameters
new `AsynTask < Params , Progress , Result >`
You can do Like This:
new AsyncTask<Void, Void, String>(){
@Override
protected String doInBackground(Void... params) {
//Parse your json here
//You will get this result into result of onPostExceute()
return json;
}
@Override
protected void onPostExecute(String result) {
//Use your result here
super.onPostExecute(result);
}
}.execute();
Using this you can get your String(here) or WhatEver you defined into Result Section of AsynTask
Hope this could Help
Upvotes: 1
Reputation: 25757
You can return your list of strings in post execute()
private class LongOperation extends AsyncTask<String, Void, List<String>> {
@Override
protected List<String> doInBackground(String... params) {
List<String> top250 = new ArrayList<String>();
for(int x=0;x<str.length;x++)
{
top250.add(str[x]);
}
return top250;
}
@Override
protected void onPostExecute(List<String> result) {
//validate result and assign to something useful like a field
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {}
}
Then you could assign result to a field somewhere in your class after validating it.
Upvotes: 2