Android AsyncTask加载屏幕永远不会消失

时间:2014-02-21 16:12:59

标签: android android-asynctask

我的问题是我正在从Web服务加载一些数据,我想通知用户我正在加载数据。因为我正在使用AsyncTask但我的Datagetter方法不起作用,我无法弄清楚为什么。这是我的班级:

public class HaberlerDynamic extends Activity {

    ArrayList<ArrayList> result = new ArrayList<ArrayList>();
    ListView list;
    DynamicListView haber ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.kurumsal_menu);
        list = (ListView) findViewById(R.id.list);

        new GetTask(this ,"gereksiz").execute();
    }


    class GetTask extends AsyncTask<Object, Void, Void> {
        Context context;
        ProgressDialog mDialog ;

        GetTask(Context context, String userid) {
            this.context = context;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            mDialog = new ProgressDialog(context);
            mDialog.setMessage("Yükeniyor...");
            mDialog.show();
        }

        @Override
        protected Void doInBackground(Object... params) {
            // here you can get the details from db or web and fetch it..

            Log.i("HaberlerDynamicAs","Request yapmadan once");
            try {    
                //this part does not work
                result = MethodInfoGetter.methodRequest("HaberGetir", "", "");

                //this part does not work
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            }

            haber = new DynamicListView( HaberlerDynamic.this , result , "Haberler" );

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            mDialog.dismiss();
            list.setAdapter( haber );

        }
    }
}

如果我不使用AsyncTask并在onCreate中调用该部分,那么它可以正常工作。

修改

方法InfoGetter.methodRequest(“HaberGetir”,“”,“”);这个方法里面有自己的异步方法。它会影响吗?

EDIT2:

移动方法list.setAdapter(haber);从doInBackgroundonPostExecute仍然没有改变任何内容。

1 个答案:

答案 0 :(得分:0)

 1. list.setAdapter(haber); -->Remove this line from doInbackground

您无法在doInBackground线程中设置UI

2.setAdapter onPostExecute

 list.setAdapter(haber);