我无法处理AsyncTask

时间:2014-12-08 09:39:06

标签: android android-asynctask

我无法理解我是否做过? 我有一个类引用输入,输出给我一个对象Document(Jsoup库)。然后我在主流中解析对象并设置表单数据。在作品开始时我展示了ProgressDialog,最后我展示了它。但一切工作都不清楚。我运行一个进程asinktask启动并且表单冻结(大约一秒钟)。补充工具栏没有时间关闭。然后形成下垂ProgressDialog一瞬间出现并关闭。数据安装在表单上。一切都非常快,但是什么形式冻结和ProgressDialog只有在它下垂后才开始担心我。

public class MyAsincTask extends AsyncTask<String, Void, Document> {
    private Document document;
    private ProgressDialog progressDialog;

    public MyAsincTask(Context context) {
        progressDialog = MyProgress.getProgressDialog(context);
    }

    @Override
    protected void onPreExecute() {
        progressDialog.show();
        super.onPreExecute();
    }

    @Override
    protected Document doInBackground(String... params) {
        try {
            document = Jsoup.connect(params[0]).get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return document;
    }

    @Override
    protected void onPostExecute(Document document) {
        progressDialog.cancel();
        super.onPostExecute(document);
    }
}

 public void setData(){
            MyAsincTask task = new MyAsincTask(getActivity());
            task.execute(link);
            try {
                document = task.get();
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
            guests = new ArrayList<>();
            Elements elementsUserId = document.select("guest_id");
            Elements elementsNumbers = document.select("room");
            Elements elementsNames = document.select("name");
            ...


            for (int i = 0; i < elementsNumbers.size(); i++) {
                GuestBean guest = new GuestBean();
                guest.setUserId(elementsUserId.get(i).ownText());
                guest.setRoom(elementsNumbers.get(i).ownText());
                guest.setName(elementsNames.get(i).ownText());
                ...
                guests.add(guest);
                count++;
            }
            GuestsListAdapter adapter = new GuestsListAdapter(getActivity(), guests);
            list.setAdapter(adapter);
        }

2 个答案:

答案 0 :(得分:0)

上面提到的AS @ρяσѕρєя K问题是document = task.get();,此处线程将被阻止。您可以使用Interface之类的{{1}}来完成此操作。

答案 1 :(得分:0)

我找到了解决方案

 public void setData() {
        new MyAsincTask(getActivity()){
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected void onPostExecute(Document document) {
                super.onPostExecute(document);
                Elements elementsDate = document.select("date");
                Elements elementsName = document.select("name");
                Elements elementsSumm = document.select("summa");

                for (int i = 0; i < elementsName.size(); i++) {
                    FolioBean folioBean = new FolioBean();
                    folioBean.setDate(elementsDate.get(i).ownText());
                    folioBean.setName(elementsName.get(i).ownText());
                    folioBean.setSumm(elementsSumm.get(i).ownText());
                    folioBeans.add(folioBean);
                }
                TableRow.LayoutParams row_params = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 20f);
                TableRow.LayoutParams row_params2 = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 30f);

                for (FolioBean folioBean : folioBeans) {
                    TableRow tableRow = new TableRow(getActivity());
                    TextView data = new TextView(getActivity());
                    TextView name = new TextView(getActivity());
                    TextView summ = new TextView(getActivity());
                    data.setTextSize(10);
                    name.setTextSize(10);
                    summ.setTextSize(10);
                    name.setTextColor(Color.BLACK);
                    summ.setTextColor(Color.BLACK);
                    data.setTextColor(Color.BLACK);
                    data.setText(" " + folioBean.getDate());
                    name.setText(" " + folioBean.getName() + " ");
                    summ.setText(folioBean.getSumm() + " ");
                    tableRow.addView(data, row_params2);
                    tableRow.addView(name, row_params2);
                    tableRow.addView(summ, row_params);
                    tableFolio.addView(tableRow);
                }
            }
        }.execute(link);
    }