Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.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
Java 加载数据和返回视图时显示ProgressDialog_Java_Android_Android Fragments - Fatal编程技术网

Java 加载数据和返回视图时显示ProgressDialog

Java 加载数据和返回视图时显示ProgressDialog,java,android,android-fragments,Java,Android,Android Fragments,我有一个从web加载数据并显示它们的片段 因此,根视图基本上基于web数据,在加载数据之前无法显示 所以我要做的是在加载数据时显示ProgressDialog 我的代码: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { URL url = null; Inpu

我有一个从web加载数据并显示它们的片段

因此,根视图基本上基于web数据,在加载数据之前无法显示

所以我要做的是在加载数据时显示ProgressDialog

我的代码:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    URL url = null;
    InputStream is = null;
    HttpURLConnection urlConn = null;
    int responseCode = 0;
    try {
        View rootView = inflater.inflate(R.layout.todo, container, false);
        rootView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        url = new URL(getString(R.string.url)+"/todo.php");
        urlConn = (HttpURLConnection) url.openConnection();
        // ADD DATA TO ROOT VIEW
        }
        return rootView;
    }catch...

如果我正确理解了您的问题,那么您希望在进度栏中显示进度。 首先,将progressbar添加到布局中或以编程方式添加它(同时使用id)。然后通过findViewById在代码中找到它

现在你基本上可以做到:

try {
        ProgressBar pb = new ProgressBar(this); // findViewById instead!
        HttpURLConnection urlConnection = (HttpURLConnection) new URL("www.google.com").openConnection();
        urlConnection.connect();
        InputStream is = urlConnection.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String str;
        while ((str = br.readLine()) != null) {
            // here you can set an indeterminate progress bar for example
            pb.setIndeterminate(true);
            Log.d("log", str);
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

但是你应该考虑在后台(例如用AcyCastTebug)来做这件事,因为否则它会是坏的做法,它会拖延用户的交互,因为主/ UI线程已经凝固了。


以下是您希望实现的一系列目标。

如果我正确理解了您的问题,则您希望在进度栏中显示进度。 首先,将progressbar添加到布局中或以编程方式添加它(同时使用id)。然后通过findViewById在代码中找到它

现在你基本上可以做到:

try {
        ProgressBar pb = new ProgressBar(this); // findViewById instead!
        HttpURLConnection urlConnection = (HttpURLConnection) new URL("www.google.com").openConnection();
        urlConnection.connect();
        InputStream is = urlConnection.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String str;
        while ((str = br.readLine()) != null) {
            // here you can set an indeterminate progress bar for example
            pb.setIndeterminate(true);
            Log.d("log", str);
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

但是你应该考虑在后台(例如用AcyCastTebug)来做这件事,因为否则它会是坏的做法,它会拖延用户的交互,因为主/ UI线程已经凝固了。 以下是你想要实现的一系列目标