0

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();   
    }

}
nobalG
  • 4,544
  • 3
  • 34
  • 72
Engin
  • 5
  • 3

2 Answers2

0

tv2.setText(array[0][0]); is null because you are getting array value before setting values in it,i.e. tv2.setText(array[0][0]); is execute before completing Asynctask.

So do this step in onPostExecute method as

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

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

         array=result;
         tv2.setText(array[0][0]);// add here
    pd.dismiss();

        pd.cancel();
    }
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
  • thank you but is there any way to pass variables to array. Actually ı will use that array for ListView not just for tv2 – Engin Jul 14 '14 at 10:37
  • @user3778620 you can set listview in onPostexecute method. – Giru Bhai Jul 14 '14 at 10:39
  • @GiruBhai if I am using separate class for AsyncTask then how can I access to the return content in main ui thread. – Ved Jul 14 '14 at 11:08
  • @ved then use it as callback like this http://stackoverflow.com/questions/14253421/how-one-interface-can-be-used-for-different-background-android-tasks/14376233#14376233 – Giru Bhai Jul 14 '14 at 11:13
0

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]);

Ved
  • 1,035
  • 14
  • 28