androidandroid-asynctaskandroid-parser

can't return data from inner asynctask class to outer class


Hi friends I know there is a bunch of questions about this topic but I can not get any result from them. i am parsing xml data with my ClassIsInternalParser extends default handler. I use this class in my activity in an inner class PostAsync extends AsyncTask

but the reason is i can not return the data that ı collected in PostAsync class to main activity. it sets only null

here is my codes

package com.example.uiexercisesplash;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ClassIsInternalListViewActivity extends Activity implements  OnClickListener{  



TextView tv, tv2;
Button back;

String[][] array = new String[10][3];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.classisinternal_listview);

    new PostAsync().execute();

 tv=(TextView) findViewById(R.id.tatar);
 tv2= (TextView) findViewById(R.id.tatar2);

  tv2.setText(array[0][0]);   //Why this sets null!!

 back= (Button) findViewById(R.id.back);
 back.setOnClickListener(this);

}   

class PostAsync extends AsyncTask<Void, Void,String[][]>{

    ProgressDialog pd;
    ClassIsInternalParser  parser;

    @Override
    protected void onPreExecute() {
        //we can set up variables here
        pd = ProgressDialog.show(ClassIsInternalListViewActivity.this,
"Classisinternal","Loading last post...",true,false);   
    }

    protected void onPostExecute(String[][] result) {

        //in this way it sets correctly 
        tv.setText(result[0][0]);  


         array=result;

    pd.dismiss();

        pd.cancel();
    }

    protected String[][] doInBackground(Void... params) {
        parser = new ClassIsInternalParser();
        parser.get();

        return parser.dataArray;
    }
    }


@Override
public void onClick(View v) {
            finish();   
    }

}

Solution

  • Try to create PostAsync object and call execute() from that object. After that call get() method to retrieve your return array like this:

    PostAsync obj=new PostAsync(this);
    obj.execute();
    String[][] array=obj.get();
    tv2.setText(array[0][0]);