Cannot hide ProgressBar programmatically in Android

泄露秘密 提交于 2019-12-11 08:34:16

问题


I am attempting to hide a ProgressBar after I load data to a ListView. However, the ProgressBar does not seem to be effected by the view change. I get no errors (activityIndicator does not appear to be NULL). Is there something wrong with how I am going about this?

<ProgressBar
    android:id="@+id/activityIndicator"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:indeterminateOnly="true"
    android:keepScreenOn="true"
    android:layout_gravity="center" />

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    theInflater = inflater;
    theContainer = container;

    currentView = inflater.inflate(R.layout.fragment_server_list, container, false);
    activityIndicator = (ProgressBar) currentView.findViewById(R.id.activityIndicator);

    // check if you are connected or not
    if(isConnected()){
        new Thread(new Runnable() {
            public void run() {
                HashMap<String, String> data = new HashMap<String, String>();
                data.put("action", "getServerList");
                data.put("phone_id", "857a95839d7d6c270e97ff6a3a64008ccf96074ebffa6e8e82db932a6c635644");
                data.put("username", "atomic");
                data.put("password", "Pa55word1");
                AsyncHttpPost asyncHttpPost = new AsyncHttpPost(data);

                try {
                    String jsonString = asyncHttpPost.execute("http://website.com/app-files/API.php").get();

                    Log.i("JSON Result", jsonString);

                    try {
                        JSONObject json = new JSONObject(jsonString);

                        if (json.optString("result").equalsIgnoreCase("true")) {
                            JSONArray articles = json.getJSONArray("data");

                            for (int i = 0; i < articles.length(); i++) {
                                JSONObject row = articles.getJSONObject(i);
                                myServers.add(new Server(Integer.parseInt(row.getString("id")), 
                                        row.getString("host"), 
                                        0, //Integer.parseInt(row.getString("port")), 
                                        "", //row.getString("hostname"), 
                                        "", //row.getString("ipAddress"), 
                                        "", //row.getString("checkDate"), 
                                        "", //row.getString("serverInfo"), 
                                        "", //row.getString("lastModified"), 
                                        "", //row.getString("contentType"), 
                                        "", //row.getString("city"), 
                                        "", //row.getString("state"), 
                                        "", //row.getString("zip"), 
                                        "", //row.getString("country"), 
                                        "", //row.getString("latitude"), 
                                        "", //row.getString("longitude"), 
                                        "", //row.getString("timeZone"), 
                                        0, //Integer.parseInt(row.getString("consecutiveFails")), 
                                        Float.parseFloat(row.getString("latency")), 
                                        Float.parseFloat(row.getString("latency")) > 300 ? 
                                                R.drawable.btn_yellow : (Float.parseFloat(row.getString("latency")) > 1 ? 
                                                        R.drawable.btn_green : R.drawable.btn_red)));

                                //Log.i("Current Server's Host", row.getString("host"));
                            }

                            populateListView();

                            registerClickCallback();

                            currentView.post(new Runnable() {
                                public void run() {
                                    activityIndicator.setVisibility(View.INVISIBLE);
                                }
                            });
                        }
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();

        activityIndicator.setVisibility(View.GONE);
    }else{
        Log.i("Connection", "You are NOT connected");
    }

    return currentView;
}

Edit: it seems that if I leave the tab and come back to the tab for this view the progress bar is gone. So it's like it is removing the progress bar, but the view doesn't update to show that it's been reviewed.


回答1:


Well, I think this is rather ridiculous, but here is how I fixed it.

In my xml for the ProgressBar, I added android:visibility="gone" to hide it by default. Then, in my code, I first told it to display (View.VISIBLE) before it tried getting the server list, then I told it to hide (View.GONE) after it was done. This worked (I could see the progress indicator while the data loaded, then it went away). So I suppose I couldn't get it to hide in the code because the code is not what forced it to be visible to begin with... That seems like a bug to me.



来源:https://stackoverflow.com/questions/24417305/cannot-hide-progressbar-programmatically-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!