2014-01-22 48 views
0

我想從php獲取數據,然後在listview中顯示。但我不知道不要告訴listview更新我爲PHP獲得的數據。我嘗試使用notifyDataSetChanged();,但我知道它應該放在哪裏。刷新ListFragment中的數據

public class List_View extends ListFragment{ 
    private String result; 
    private ListView listView; 
    private ArrayList<String> items = new ArrayList<String>(); 
    final String uri = "http://localhost/userinfo.php"; 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 
     View rootView = inflater.inflate(R.layout.list, container, false); 
     setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items)); 
     return rootView; 
    } 

    class sendPostRunnable implements Runnable{ 
     @Override 
     public void run(){ 
      result = sendPostDataToInternet(); 
      String temp = "Not result."; 

      try { 
       JSONTokener jsonTokener = new JSONTokener(result); 
       JSONObject jsonObject = (JSONObject) jsonTokener.nextValue(); 
       JSONArray jarray = jsonObject.getJSONArray("response"); 
       for (int i = 0; i < jarray.length(); i++) { 
        temp = ""; 
        JSONObject jobject = jarray.getJSONObject(i); 
        temp += "name: "+jobject.getString("name")+"\n"; 
        temp += "email: "+jobject.getString("email"); 
        items.add(temp); 
        temp = ""; 

       } 
       if(jarray.length() < 1){ 
        items.add(temp); 
       } 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 


     } 
    } 

    private String sendPostDataToInternet(){ 

     HttpPost httpRequest = new HttpPost(uri); 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("uid", "u1")); 
     try{ 

      httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); 
      HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest); 

      if (httpResponse.getStatusLine().getStatusCode() == 200){ 
       String strResult = EntityUtils.toString(httpResponse.getEntity()); 
       return strResult; 
      } 
     } catch (Exception e){ 
      e.printStackTrace(); 
     } 
     return null; 
    } 
} 
+0

爲什麼不先從服務器獲取數據,然後在收到數據後填充列表?這樣你不必使用notifyDataSetChanged() – Shane

+0

我不確定如果進入Runnable類,你可以添加更多的項目沒有一個FC。 我認爲你必須聲明一個Handler實例並通過Message發送你的數組。這允許從輔助線程將您的數據發送到您的GUI線程。因此,首先將您的項目添加到適配器,然後使用notifyDatasetChanged(); – ClarkXP

回答

0

你應該收集所有到達其他集合在單獨的線程(比列表適配器使用其他的),然後更換名單總彙源和之後比你應該叫notifyDataSetChanged()數據。

 public class List_View extends ListFragment{ 
      private String result; 
      private ListView listView; 
    protected List newData; 
    protected ArrayAdapter listAdapter; 
      private ArrayList<String> items = new ArrayList<String>(); 
      final String uri = "http://localhost/userinfo.php"; 

      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 
       View rootView = inflater.inflate(R.layout.list, container, false); 
    listAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items); 
       setListAdapter(listAdapter); 
       return rootView; 
      } 

      class sendPostRunnable implements Runnable{ 
       @Override 
       public void run(){ 
        result = sendPostDataToInternet(); 
        String temp = "Not result."; 

// a new collection to fill from response 
newData = new ArrayList<String>(); 
        try { 
         JSONTokener jsonTokener = new JSONTokener(result); 
         JSONObject jsonObject = (JSONObject) jsonTokener.nextValue(); 
         JSONArray jarray = jsonObject.getJSONArray("response"); 
         for (int i = 0; i < jarray.length(); i++) { 
          temp = ""; 
          JSONObject jobject = jarray.getJSONObject(i); 
          temp += "name: "+jobject.getString("name")+"\n"; 
          temp += "email: "+jobject.getString("email"); 
    //here you should create a corresponding object from JSON 
    // and add it to newData List 
          newData .add(temp); 
          temp = ""; 

         } 
         if(jarray.length() < 1){ 
          items.add(temp); 
         } 
    // here you will have a newData List filled so you should 
    // replace or add data from this List to ListView's used 
    items.add(newData); 
    listAdapter.notifyDataSetChanged(); 
        } catch (JSONException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 


       } 
      } 

      private String sendPostDataToInternet(){ 

       HttpPost httpRequest = new HttpPost(uri); 
       List<NameValuePair> params = new ArrayList<NameValuePair>(); 
       params.add(new BasicNameValuePair("uid", "u1")); 
       try{ 

        httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); 
        HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest); 

        if (httpResponse.getStatusLine().getStatusCode() == 200){ 
         String strResult = EntityUtils.toString(httpResponse.getEntity()); 
         return strResult; 
        } 
       } catch (Exception e){ 
        e.printStackTrace(); 
       } 
       return null; 
      } 
     }