1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
James
  • 3,765
  • 4
  • 48
  • 79
  • is your application closing or not? – Rod_Algonquin Jun 25 '14 at 19:52
  • You are hiding the ProgressBar right after starting the Thread, and also inside the thread. Are you sure about this behaviour? – Abhishek Shukla Jun 25 '14 at 19:52
  • @Rod_Algonquin No, the app does not crash. It shows my list view just fine and I can scroll and click. The only thing it is not doing is hiding the progress bar. – James Jun 25 '14 at 19:59
  • @AbhishekShukla Yes, I'm sure. I'm just trying to get it to hide. After than I can make it hide only after it needs to. But it's not hiding at all, unfortunately. – James Jun 25 '14 at 19:59
  • Bad idea to put a progress bar in an xml. Better create one on the fly. – greenapps Jun 25 '14 at 20:07
  • @greenapps Why is it better to create it on the fly? – James Jun 25 '14 at 20:08
  • Don't know but try and you will see. Just happened to me. Thats all. – greenapps Jun 25 '14 at 20:09
  • @greenapps Do you mean you couldn't hide yours until you created it programmatically? – James Jun 25 '14 at 20:10
  • Something like that. Don't know anymore if it was about hiding. Anyhow it gave troubles. And please.. dont call a progressbar an activityindicator if you want readable code. – greenapps Jun 25 '14 at 20:11
  • @James remove your thread and try to test it if your code for hiding the progress bar wrks. – Rod_Algonquin Jun 25 '14 at 20:12
  • YES... He has an asynctask in a thread. Good catch!! – greenapps Jun 25 '14 at 20:13
  • @Rod_Algonquin Actually, you can see it there again at the bottom where I am setting the view to `GONE` which is outside of the thread statement. So I'm trying to do it twice and its not working in either place. – James Jun 25 '14 at 20:36
  • And now `status bar` ? Well create it on tje fly! Wenn will you try? – greenapps Jun 25 '14 at 20:44
  • Stupid question: You confirmed that `isConnected()` returns true? – HHK Jun 25 '14 at 21:25
  • @HansKratz Yes, my list view populates correctly, which the functionality is only called if it returns true. – James Jun 25 '14 at 21:27

1 Answers1

2

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.

James
  • 3,765
  • 4
  • 48
  • 79