Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android在asynctask中显示dialogfragment_Android_Android Fragments_Android Alertdialog - Fatal编程技术网

android在asynctask中显示dialogfragment

android在asynctask中显示dialogfragment,android,android-fragments,android-alertdialog,Android,Android Fragments,Android Alertdialog,我在AsyncTask中的对话框片段有一些问题。基本上这里是发生的事情的要点。我有一个自定义列表适配器来构建ListView。在preExecute上,我显示我的对话框。数据通过doInBackground加载到列表中。在postExecute上,我将关闭对话框。问题是我现在只剩下一个空白屏幕。对话框出现,然后消失,但没有项目列表。真的不知道发生了什么。这是我的DialogFragment类: public class ProcessingDialog extends DialogFragmen

我在AsyncTask中的对话框片段有一些问题。基本上这里是发生的事情的要点。我有一个自定义列表适配器来构建ListView。在preExecute上,我显示我的对话框。数据通过doInBackground加载到列表中。在postExecute上,我将关闭对话框。问题是我现在只剩下一个空白屏幕。对话框出现,然后消失,但没有项目列表。真的不知道发生了什么。这是我的DialogFragment类:

public class ProcessingDialog extends DialogFragment {

    private String dialog_text;


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();

        View dialog_view = inflater.inflate(R.layout.dialog_loading, null);
        final TextView tv1 = (TextView) dialog_view.findViewById(R.id.dialog_text);
        dialog_text = getArguments().getString("message");
        tv1.setText(dialog_text);
        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout

        builder.setView(dialog_view);

        // Create the AlertDialog object and return it
        return builder.create();
    }
}
以下是我的主要活动:

public class MainActivity extends FragmentActivity {

    private Context context = this;
    private List<RandomdataModel> list;
    private LayoutInflater layoutInflater;
    private CustomListAdapter listAdapter;
    private ProcessingDialog dialog_fragment_loading;
    private MessageDialog dialog_fragment_message;

    private final String[] parts = new String[]{"Sprocket", "Screw", "Nail",
            "Widget", "Gear", "Cog", "Tube", "Spring", "Tape", "Bolt"};


    private static final String LOGTAG = "MainActivity";
    private static final boolean DEBUG = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_view);

        listAdapter = new CustomListAdapter(this);
        ListView listView = (ListView) this.findViewById(R.id.listView);
        listView.setAdapter(listAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
                if (DEBUG) {
                    Log.d(LOGTAG, "onItemClick(): arg0=" + arg0.getClass().getSimpleName());
                    Log.d(LOGTAG, "onItemClick(): arg1=" + view.getClass().getSimpleName());
                }

                TextView tv1 = (TextView) view.findViewById(R.id.listitemTextIDValue);
                TextView tv2 = (TextView) view.findViewById(R.id.listitemTextPartValue);
                RandomdataModel data = new RandomdataModel();

                data.setId(Integer.parseInt(tv1.getText().toString()));
                data.setPart(tv2.getText().toString());

                Intent intent = new Intent(context, DisplayActivity.class);
                intent.putExtra("data", data);

                if (intent.resolveActivity(getPackageManager()) != null) {
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public void onResume() {
        //called after onStart() just before activity comes to the foreground
        super.onResume();
        new UpdateList().execute();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    class CustomListAdapter
            extends BaseAdapter {
        private Context context;
        private List<RandomdataModel> list;
        private LayoutInflater layoutInflater;

        CustomListAdapter(Context context) {
            this.context = context;
        }

        public void setList(List<RandomdataModel> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return ((list == null) ? 0 : list.size());
        }

        @Override
        public Object getItem(int position) {
            //  In theory we should not be called if getCount() returned 0;
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        class ViewHolder {
            int position;
            TextView textView1;
            TextView textView2;
            Button button;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (DEBUG) {
                Log.d(LOGTAG, "getView.position=" + position);
                Log.d(LOGTAG, "getView.convertView=" + convertView);
            }
            if (list == null) {
                //  In theory it should not happen but handle this in some graceful way.
                //  Returning null will not produce graceful results.


                //display dialog of no data found....
                return null;
            }

            ViewHolder holder;

            if (convertView == null) {
                convertView = getLayoutInflator().inflate(R.layout.list_view_items, null);
                holder = new ViewHolder();
                holder.textView1 = (TextView) convertView.findViewById(R.id.listitemTextIDValue);
                holder.textView2 = (TextView) convertView.findViewById(R.id.listitemTextPartValue);
                holder.button = (Button) convertView.findViewById(R.id.listitemButton);
                holder.button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {


                    }
                });
                convertView.setTag(holder);

            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            RandomdataModel item = list.get(position);
            holder.textView1.setText("" + item.getId());
            holder.textView2.setText(item.getPart());
            holder.button.setText("Submit " + item.getId());

            return convertView;
        }

        private LayoutInflater getLayoutInflator() {
            if (layoutInflater == null) {
                layoutInflater = (LayoutInflater)
                        this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            }
            return layoutInflater;
        }
    }

    class UpdateList
            extends AsyncTask<Void, Void, List<RandomdataModel>> {
        private final String LOGTAG = "Updating List";
        private final boolean DEBUG = true;
        private ProgressDialog dialog;


        @Override
        protected void onPreExecute() {
            //progressBarCircle.setVisibility(View.VISIBLE);
            dialog_fragment_loading = processingDialog("Loading Data...");
            dialog_fragment_loading.show(getSupportFragmentManager(), "loading_data");
        }

        @Override
        protected List<RandomdataModel> doInBackground(Void... params) {
            if (DEBUG) Log.d(LOGTAG, "**** doInBackground() STARTING");

            List<RandomdataModel> listData = new ArrayList<>();

            RandomdataModel data;

            int len = parts.length;
            try {
                Thread.sleep(4000);

                //loading data from array
                for (int i = 0; i < len; i++) {
                    data = new RandomdataModel();
                    data.setId(i);
                    data.setPart(parts[i]);
                    listData.add(data);
                }
            } catch (Exception e) {
                if (DEBUG) Log.d(LOGTAG, e.toString());
                e.printStackTrace();
            }

            return listData;
        }

        @Override
        protected void onPostExecute(List<RandomdataModel> listData) {
            //close dialog
            dialog_fragment_loading.dismiss();
            listAdapter.setList(listData);
        }
    }

    public static ProcessingDialog processingDialog(String message) {
        final ProcessingDialog f = new ProcessingDialog();
        final Bundle args = new Bundle();
        args.putString("message", message);
        f.setArguments(args);
        return f;
    }    
}
public类MainActivity扩展了FragmentActivity{
私有上下文=这个;
私人名单;
私人停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场;
私有CustomListAdapter-listAdapter;
私有处理对话框\u片段\u加载;
私有消息对话框\u片段\u消息;
私有最终字符串[]部分=新字符串[]{“链轮”、“螺钉”、“钉子”,
“小部件”、“齿轮”、“齿轮”、“管”、“弹簧”、“胶带”、“螺栓”};
私有静态最终字符串LOGTAG=“MainActivity”;
私有静态最终布尔调试=true;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.list\u视图);
listAdapter=新的CustomListAdapter(此);
ListView ListView=(ListView)this.findViewById(R.id.ListView);
setAdapter(listAdapter);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView arg0,视图,整型位置,长id){
如果(调试){
Log.d(LOGTAG,“onItemClick():arg0=“+arg0.getClass().getSimpleName());
Log.d(LOGTAG,“onItemClick():arg1=“+view.getClass().getSimpleName());
}
TextView tv1=(TextView)view.findViewById(R.id.listitemTextIDValue);
TextView tv2=(TextView)view.findViewById(R.id.listitemTextPartValue);
RandomdataModel数据=新的RandomdataModel();
setId(Integer.parseInt(tv1.getText().toString());
setPart(tv2.getText().toString());
意向意向=新意向(上下文,DisplayActivity.class);
意向。额外(“数据”,数据);
if(intent.resolveActivity(getPackageManager())!=null){
星触觉(意向);
}
}
});
}
@凌驾
恢复时公开作废(){
//在活动进入前台之前的onStart()之后调用
super.onResume();
新建UpdateList().execute();
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单菜单菜单主菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
类CustomListAdapter
扩展BaseAdapter{
私人语境;
私人名单;
私人停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场;
CustomListAdapter(上下文){
this.context=上下文;
}
公共无效集合列表(列表){
this.list=列表;
}
@凌驾
public int getCount(){
返回((list==null)?0:list.size();
}
@凌驾
公共对象getItem(int位置){
//理论上,如果getCount()返回0,则不应该调用我们;
返回列表。获取(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
类视图持有者{
内部位置;
文本视图文本视图1;
文本视图文本视图2;
按钮;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
如果(调试){
Log.d(LOGTAG,“getView.position=“+position”);
Log.d(LOGTAG,“getView.convertView=“+convertView”);
}
if(list==null){
//从理论上讲,这不应该发生,但要以一种优雅的方式来处理。
//返回null将不会产生优美的结果。
//显示未找到数据的对话框。。。。
返回null;
}
视窗座;
if(convertView==null){
convertView=GetLayoutFlator()。充气(R.layout.list\u view\u items,null);
holder=新的ViewHolder();
holder.textView1=(TextView)convertView.findViewById(R.id.listitemTextIDValue);
holder.textView2=(TextView)convertView.findViewById(R.id.listitemTextPartValue);
holder.button=(button)convertView.findViewById(R.id.listitemButton);
holder.button.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
}
});
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
RandomdataModel项=list.get(位置);
holder.textView1.setText(“+item.getId());
holder.textView2.setText(item.getPart());
holder.button.setText(“Submit”+item.getId());
返回视图;
}