在DialogFragment中慢速加载自定义ArrayAdapter的位置实现Async任务的位置?

时间:2015-08-07 09:00:23

标签: android listview asynchronous

我有一个Dialog片段,其中包含一个带有ListView的布局。我在自定义DialogFragment.onCreateDialog()中使用了Alert Builder。在将对话框返回到调用片段之前,我创建了我的适配器并将listView的适配器设置为它。这很慢,我的自定义适配器还有很多工作要做。我尝试使用异步任务在添加默认适配器后设置适配器,但它仍然滞后。我假设默认适配器会快速加载,然后当异步完成它会更新listView。

自定义DialogFragment onCreateDialog()

@Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        currentGoals = MhaCacheManager.getInstance().getUserGoals();
        if(currentGoals!=null)
        {
            Log.d("GoalsEdit", "Added onCreateDialog here");
            LayoutInflater inflater = getActivity().getLayoutInflater();
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle("Goal edit")
                    .setMessage("Edit your goals")
                    .setView(rootView = inflater.inflate(R.layout.fragment_goal_edit_dialog, null))
                    .setPositiveButton("Accept", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            listener.onEditValue(currentGoals);
                            Log.d("GoalsEdit", "Accept goals edit!");
                        }
                    })
                    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //AddFriendDialog.this.getDialog().cancel();
                        }
                    });
            Dialog result = builder.create();

            result.show();
            result.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
            result.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

            goalsList = (ListView) rootView.findViewById(R.id.listView);

            String[] test = {"Loading Data"};
            ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, test);
            goalsList.setAdapter( myAdapter);

            new DialogLoaderAsync(getActivity(),goalsList,this).execute(); // The asymc task that sets the adapter in the passed listView!

            return result;
        }
        return null;
    }

异步任务DialogLoaderAsync

public class DialogLoaderAsync extends AsyncTask<Void, Void, Void>
{

    private Context context;

    private ProgressDialog mDialogProgress;
    private ListView lView;
    private GoalsArrayAdapter.OnDialogUpdated mParent;
    ArrayAdapter<String> myAdapter;

    public interface OnArrayAdapterLoaded
    {
        public void onArrayAdapterLoaded();
    }
    public OnArrayAdapterLoaded listener;

    public DialogLoaderAsync(Context context, ListView lView,GoalsArrayAdapter.OnDialogUpdated mParent)
    {
        this.context=context;
        this.lView=lView;
        this.mParent=mParent;
    }

    @Override
    protected Void doInBackground(Void... params)
    {
        ArrayList<MhaCacheManager.UserGoals> currentGoals = MhaCacheManager.getInstance().getUserGoals();
        ArrayAdapter<String> myAdapter = new GoalsArrayAdapter(context, currentGoals, mParent);
        lView.setAdapter(myAdapter); // Really time consuming.
        return null;
    }

    @Override
    public void onPostExecute(Void param)
    {
        lView.deferNotifyDataSetChanged();
    }

}

就好像在活动视图中添加任何内容时,它会挂起UI线程吗?我在哪里可以做这项工作?

1 个答案:

答案 0 :(得分:2)

我不认为问题是自定义适配器。看来你在这里获取主线程的数据

@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
    currentGoals = MhaCacheManager.getInstance().getUserGoals();
    if(currentGoals!=null)
    ...
    ...

为什么我这样说:您在Async任务中也使用相同的方法currentGoals = MhaCacheManager.getInstance().getUserGoals();。好像你在主线程中也使用相同的方法。尝试从onCreateDialog删除上述代码,它应该可以正常工作。