无法在Android中以编程方式隐藏ProgressBar

时间:2014-06-25 19:49:03

标签: android android-progressbar

我在将数据加载到ProgressBar后尝试隐藏ListView。但是,ProgressBar似乎不受视图更改的影响。我没有收到任何错误(activityIndicator似乎不是NULL)。我如何解决这个问题有什么问题吗?

<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;
}

编辑:似乎如果我离开标签并返回此视图的标签,则进度条消失了。所以它就像删除进度条一样,但视图没有更新以显示它已被审核。

1 个答案:

答案 0 :(得分:2)

嗯,我觉得这很荒谬,但这就是我修复它的方法。

ProgressBar的xml中,我添加了android:visibility="gone"以默认隐藏它。然后,在我的代码中,我首先告诉它在尝试获取服务器列表之前显示(View.VISIBLE),然后我告诉它在完成后隐藏(View.GONE)。这工作(我可以在加载数据时看到进度指示器,然后它就消失了)。所以我想我无法将其隐藏在代码中,因为代码不是强制它开始时可见的......这对我来说似乎是个错误。