调用AsyncTask doInBackground

时间:2015-11-26 10:09:42

标签: android listview

我有一个适用于我的listView的适配器,以及从互联网填充的数据, 后台进程运行良好,结果出现在Log Cat中,但在我的列表视图中没有,这里有什么问题?我做错了吗?这是我的代码

PortalGroup mPortalGroup;
ArrayList<PortalItemData> mItems;
PortalItemListAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_group);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    new MyPortal().execute();
        mItems = new ArrayList<PortalItemData>();
        mAdapter = new PortalItemListAdapter(getApplicationContext(), mItems);

        JazzyListView list = (JazzyListView) findViewById(R.id.listGroup);
        list.setAdapter(mAdapter);
        list.setTransitionEffect(new FlyEffect());

}

private class MyPortal extends AsyncTask<Void, Void,Void> {
    ProgressDialog pDialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(GroupActivity.this);
        pDialog.setOnDismissListener(new OnDismissListener(){

            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                MyPortal.this.cancel(true);
            }
        });
        pDialog.setMessage("Loading...");
        pDialog.show();
    }

    protected Void doInBackground(Void... params) {
        Portal portal = AccountManager.getInstance().getPortal();
        try {
            PortalInfo portalInfo = portal.fetchPortalInfo();
            List<String> querys = portalInfo.getFeaturedGroupsQueries();
            for (String query : querys) {
                Log.d("Query Group", "[query] " + query);
                PortalQueryResultSet<PortalGroup> result = portal
                        .findGroups(new PortalQueryParams(query));
                for (PortalGroup group : result.getResults()) {
                    Log.d("Available Group", "[group title] " + group.getTitle());
                    PortalQueryParams queryParams = new PortalQueryParams();
                    queryParams.setQuery(PortalItemType.WEBMAP, group.getGroupId(), null);
                    PortalQueryResultSet<PortalItem> results = portal.findItems(queryParams);
                    for (PortalItem item : results.getResults()) {
                        Log.d("Group Item", "[item title] " + item.getTitle());
                        byte[] data = item.fetchThumbnail();
                        if (data != null) {
                            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                            PortalItemData portalItemData = new PortalItemData(item, bitmap);
                            mItems.add(portalItemData);

                        }
                    }
                }
            }
            pDialog.dismiss();
        } catch (Exception e) {
            e.printStackTrace();
            onError(e);
        }
        return null;
    }

    public void onError(Throwable e) {
        Toast.makeText(GroupActivity.this, getString(R.string.failed_sign_in), Toast.LENGTH_SHORT).show();
        pDialog.dismiss();
    }

    protected void onPostExecute() {
         mAdapter.notifyDataSetChanged();
        pDialog.dismiss();
    }
}

private class PortalItemListAdapter extends ArrayAdapter<PortalItemData> {
    private LayoutInflater inflater=null;

    public PortalItemListAdapter(Context context, ArrayList<PortalItemData> items) {
        super(context, 0, items);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_group, parent, false);
        }

        // Setup item thumbnail
        PortalItemData item = getItem(position);
        ImageView image = (ImageView) convertView.findViewById(R.id.img);
        image.setImageBitmap(item.itemThumbnail);

        // Setup item title
        TextView text = (TextView) convertView.findViewById(R.id.txt);
        text.setText(item.portalItem.getTitle());
        return convertView;
    }

}


private class PortalItemData {
    PortalItem portalItem;

    Bitmap itemThumbnail;

    public PortalItemData(PortalItem item, Bitmap bt) {
        this.portalItem = item;
        this.itemThumbnail = bt;
    }
}

4 个答案:

答案 0 :(得分:0)

onPostExecute中,您可能希望填充ListView

在您获取Adapter的值后,在doInBackground中实例化您的mItems即mAdapter。最后将Adapter设置为ListView中的onPostExecute

答案 1 :(得分:0)

您只更新mItems的数据,但不通知JazzyListView list。注意:您必须在主线程上更新UI。调用mAdapter.notifyDataSetChanged()更新AsyntTask

onPostExecute()上的列表

答案 2 :(得分:0)

2个解决方案

  • mAdapter.notifyDataSetChanged()
  • 上添加onPostExecute()
  • onPostExecute()中创建适配器并将其设置为Listview

您当前的代码在某些情况下实际上可能导致Exception

答案 3 :(得分:-1)

使用以下代码

private class MyPortal extends AsyncTask<Void, Void,Void> {
ProgressDialog pDialog;
@Override
protected void onPreExecute() {
    super.onPreExecute();
    pDialog = new ProgressDialog(GroupActivity.this);
    pDialog.setOnDismissListener(new OnDismissListener(){

        @Override
        public void onDismiss(DialogInterface dialogInterface) {
            MyPortal.this.cancel(true);
        }
    });
    pDialog.setMessage("Loading...");
    pDialog.show();
}

protected Void doInBackground(Void... params) {
    Portal portal = AccountManager.getInstance().getPortal();
    try {
        PortalInfo portalInfo = portal.fetchPortalInfo();
        List<String> querys = portalInfo.getFeaturedGroupsQueries();
        for (String query : querys) {
            Log.d("Query Group", "[query] " + query);
            PortalQueryResultSet<PortalGroup> result = portal
                    .findGroups(new PortalQueryParams(query));
            for (PortalGroup group : result.getResults()) {
                Log.d("Available Group", "[group title] " + group.getTitle());
                PortalQueryParams queryParams = new PortalQueryParams();
                queryParams.setQuery(PortalItemType.WEBMAP, group.getGroupId(), null);
                PortalQueryResultSet<PortalItem> results = portal.findItems(queryParams);
                for (PortalItem item : results.getResults()) {
                    Log.d("Group Item", "[item title] " + item.getTitle());
                    byte[] data = item.fetchThumbnail();
                    if (data != null) {
                        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                        PortalItemData portalItemData = new PortalItemData(item, bitmap);
                        mItems.add(portalItemData);

                    }
                }
            }
        }
        pDialog.dismiss();
    } catch (Exception e) {
        e.printStackTrace();
        onError(e);
    }
    return null;
}

public void onError(Throwable e) {
    Toast.makeText(GroupActivity.this, getString(R.string.failed_sign_in), Toast.LENGTH_SHORT).show();
    pDialog.dismiss();
}

protected void onPostExecute() {
    list.setAdapter(mAdapter);
    list.setTransitionEffect(new FlyEffect());
    pDialog.dismiss();
}

}