Async and ListView Android

可紊 提交于 2019-12-24 14:52:14

问题


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.


回答1:


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



回答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.




回答3:


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


来源:https://stackoverflow.com/questions/36564789/async-and-listview-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!