如何使用asynctask刷新android中的listview?

时间:2013-05-24 09:52:36

标签: android listview android-asynctask

有两项活动。第一个Activity具有列表视图以查看共享内容,第二个活动具有编辑文本框(输入以便共享)和按钮。单击该按钮,它会返回字符串,这是json响应,我需要在上一个活动中添加它。

现在的问题是,当我刷新第一页完全点击服务器时,它会得到响应,但这不是我想要的。它不应该回到服务器。它应该只是添加到列表视图适配器。

我已经对PostExecute()中的代码进行了评论。我已经尝试过各种方法,但它没有反映出来。我也发布了我的代码供您参考。

    public class ShareAsyncTask extends AsyncTask<String, Void, ArrayList<EventsStreamBean>> {

    public ProgressDialog pd = new ProgressDialog(EventStreamActivity.this);

    String success_share_val;

    @Override
    protected ArrayList<EventsStreamBean> doInBackground(
            String... result) {
        // TODO Auto-generated method stub


        JSONObject jsonobj = new JSONObject(result[0].toString());

        success_share_val = jsonobj.getString(Constants.SUCCESS);


        //checks the success value
        if(success_share_val.equalsIgnoreCase("1")) {            

         JSONArray events_stream_share_array = jsonobj.getJSONArray("streamArray");

         if(events_stream_share_array.length() > 0) {


                 for(int i=0; i<events_stream_share_array.length(); i++) {

                     EventsStreamBean events_stream_bean = new EventsStreamBean();

                     JSONObject events_stream_object = events_stream_share_array.getJSONObject(i);

                     events_stream_bean.setStreamId(events_stream_object.getString(Constants.STREAM_ID));
                     events_stream_bean.setStreamType(events_stream_object.getString(Constants.STREAM_TYPE));

                     events_stream_bean.setUserId(events_stream_object.getString(Constants.USER_ID));
                     events_stream_bean.setUserName(events_stream_object.getString(Constants.USER_NAME));
                     events_stream_bean.setUserType(events_stream_object.getString(Constants.USER_TYPE));
                     events_stream_bean.setUserAvatar(events_stream_object.getString(Constants.USER_AVATAR));

                     arraylist_events_stream.add(events_stream_bean);

                 }
         }else {
             Log.i("Test", "No Events Streams Available");
         }

        }

        }catch(Exception e) {}



        return arraylist_events_stream;
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();

        this.pd.setMessage("Loading....");
        pd.setCanceledOnTouchOutside(false); 
        pd.setCancelable(false);
        this.pd.show();
    }

    @Override
    protected void onPostExecute(final ArrayList<EventsStreamBean> result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        if(this.pd.isShowing()) {
            this.pd.dismiss();
        }


        Toast.makeText(EventStreamActivity.this, "Post shared successfully", Toast.LENGTH_SHORT).show();
        new EventsStreamAsyncTask().execute(temp_val);



        /*runOnUiThread(new Runnable() {
            public void run() {

                //EventStream_Customadapter adapter = (EventStream_Customadapter) list_view.getAdapter();
                //adapter.clearData();

                adapter.updateData(result);
                //adapter = new EventStream_Customadapter(EventStreamActivity.this, arraylist_events_stream);
                //list_view.setAdapter(adapter);
                //adapter.notifyDataSetChanged();

            }
        });*/

    }

}

谢谢

0 个答案:

没有答案