1

BackgorundTask.java (How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?)

public class BackgroundTask extends AsyncTask<String,Void,String> {

    public interface AsyncResponse {
        void processFinish(String output);
    }

    public AsyncResponse delegate = null;

    public BackgorundTask(AsyncResponse delegate){
        this.delegate = delegate;
    }

    protected String doInBackground(String... uri) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response;
        String responseString = null;
        try {
            response = httpclient.execute(new HttpGet(uri[0]));
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                responseString = out.toString();
                out.close();
            } else{
                //Closes the connection.
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (ClientProtocolException e) {
            //TODO Handle problems..
        } catch (IOException e) {
            //TODO Handle problems..
        }
        return responseString;
    }

    protected void onProgressUpdate(Integer... progress) {

    }

    @Override
    protected void onPostExecute(String result) {
        delegate.processFinish(result);
    }
}

ListActivity.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);
 BackgorundTask asyncTask = (BackgorundTask) new BackgorundTask(new BackgorundTask.AsyncResponse(){
        @Override
        public void processFinish(String output){
            listAdapter.add(output);
        }
    }).execute("some url");

    adapter = new MyCustomAdapter(listAdapter, this);

    final ListView mainListView = (ListView)findViewById(R.id.listView);
    mainListView.setAdapter(adapter);

The variable is returned correctly. But I can not add to the ListView (listAdapter) of each item. What could be the reason? I suppose that I have to somehow pull the string variable output from asyncTask to my function onCreate in which it is placed.

Community
  • 1
  • 1
Pietrov
  • 23
  • 6

3 Answers3

2

The code to get the data back to the activity and to your listadapter seems okay to me. I think you just need to add a listAdapter.notifyDataSetChanged(); after listAdapter.add(output); so the listview knows it has to render itself anew.

Ridcully
  • 23,362
  • 7
  • 71
  • 86
2

your code is fine. you just need to call this line after adding data

listAdapter.add(output); // after this line add below line
listAdapter.notifyDataSetChanged(); 
Mustanser Iqbal
  • 5,017
  • 4
  • 18
  • 36
1

You have to set the adapter again when you receive output string as below :

BackgorundTask asyncTask = (BackgorundTask) new BackgorundTask(new BackgorundTask.AsyncResponse(){
        @Override
        public void processFinish(String output){
            listAdapter.add(output);

            adapter = new MyCustomAdapter(listAdapter, this);

            final ListView mainListView = (ListView)findViewById(R.id.listView);
    mainListView.setAdapter(adapter);
        }
    }).execute("some url");
Ameer Faisal
  • 355
  • 1
  • 9