2016-06-22 66 views
1

我試圖在後退按鈕按下後更新前一個片段上的ListView。調用onResume(用Toast驗證)並且webservice運行(listView在清除後顯示)。問題是在調用accessWebService_getUsername之後,ListView仍然顯示舊值而不是新值。我驗證了MySQL中的值,即使DB已更新,ListView也只返回舊值。onResume不會更新帶有新數據的ListView

  @Override 
      public void onResume() { 
      Toast.makeText(getActivity(), "onResume", Toast.LENGTH_SHORT).show(); 

      super.onResume(); 

       adapter.clear(); 

       getIMEI(); 
       accessWebService_getUsername(); 

       adapter.notifyDataSetChanged(); 

      } 

更新:

//的ListView

ListView lv =(ListView)view.findViewById(R.id.listView); 

adapter = new ContactsAdapter(getActivity(), arrRequest_Contact, arrRequest_NameSurname, arrRequest_MessageCount, arrRequest_Time, arrRequest_Image); 
lv.setAdapter(adapter); 

//的Json

  private class JsonGetUsername extends AsyncTask<String, Void, String> { 

      //Pending 01 
      private ProgressDialog dialog = new ProgressDialog(getActivity()); 

      @Override 
      protected void onPreExecute() { 
       this.dialog.setMessage("Loading Contacts, Please Wait"); 
       this.dialog.show(); 
      } 

      @Override 
      protected String doInBackground(String... params) { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(params[0]); 
      try { 
      HttpResponse response = httpclient.execute(httppost); 
      jsonResult = inputStreamToString(response.getEntity().getContent()).toString(); 
      } 


      catch (ClientProtocolException e) { 
      e.printStackTrace(); 
      } catch (IOException e) { 
      e.printStackTrace(); 
      } 
      return null; 
      } 

      private StringBuilder inputStreamToString(InputStream is) { 
      String rLine = ""; 
      StringBuilder answer = new StringBuilder(); 
      BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 

      try { 
      while ((rLine = rd.readLine()) != null) { 
      answer.append(rLine); 
      } 
      } 

      catch (IOException e) { 
      // e.printStackTrace(); 
      Toast.makeText(getActivity(),"Error..." + e.toString(), Toast.LENGTH_LONG).show(); 
      } 
      return answer; 
      } 

       @Override 
       protected void onPostExecute(String result) { 

       //Pending 02 
       if (dialog.isShowing()) { 
        dialog.dismiss(); 
       } 

       adapter.notifyDataSetChanged(); 
       try{ 
        ListDrawer_getUsername(); //has ConnectionException (when it cannot reach server) 
       }catch (Exception e){ 
        Toast.makeText(getActivity(), "Please check your connection..", Toast.LENGTH_LONG).show(); 
        } 
       } 
     }// end async task 

     public void accessWebService_getUsername() { 
     JsonGetUsername task = new JsonGetUsername(); 
      // passes values for the urls string array 
      task.execute(new String[] { "http://mywebsite/php/get_username.php?pIMEI="+IMEI}); 
     } 

     // build hash set for list view 
     public void ListDrawer_getUsername() { 

      try { 
      JSONObject jsonResponse = new JSONObject(jsonResult); 
      JSONArray jsonMainNode = jsonResponse.optJSONArray("username_info"); 

      for (int i = 0; i < jsonMainNode.length(); i++) { 
      JSONObject jsonChildNode = jsonMainNode.getJSONObject(i); 
      request_username = jsonChildNode.optString("Username"); 

      } 

      accessWebService_getContacts(); 

      } catch (JSONException e) { 
       System.out.println("Json Error Rooms" +e.toString()); 
       //Toast.makeText(getApplicationContext(), "No Rooms To Load", Toast.LENGTH_SHORT).show(); 

      } 

    } 

更新2: // ContactsAdpater

   class ContactsAdapter extends ArrayAdapter<String> 
      { 
       Context context; 
       List<String> Request_Contact; 
       List<String> Request_NameSurname; 
       List<String> Request_MessageCount; 
       List<String> Request_Time; 
       List<String> Request_Image; 

       ContactsAdapter(Context c, List<String> Request_Contact, List<String> Request_NameSurname, List<String> Request_MessageCount, List<String> Request_Time, List<String> Request_Image) 
       { 
        super(c, R.layout.activity_contacts_single, R.id.textContact, Request_Contact); 
        this.context=c; 
        this.Request_Contact=Request_Contact; 
        this.Request_NameSurname=Request_NameSurname; 
        this.Request_MessageCount=Request_MessageCount; 
        this.Request_Time=Request_Time; 
        this.Request_Image=Request_Image; 

       } 


       @Override 
       public View getView(int position, View convertView, ViewGroup parent) { 
        // TODO Auto-generated method stub 

        View row=convertView; 
        if(row==null) 
        { 
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        row = inflater.inflate(R.layout.activity_contacts_single, parent, false);  
        } 

        TextView txtContact = (TextView) row.findViewById(R.id.textContact); 
        TextView txtNameSurname = (TextView) row.findViewById(R.id.textNameSurname); 
        TextView txtMessageCount = (TextView) row.findViewById(R.id.textMessageCount); 
        TextView txtTime = (TextView) row.findViewById(R.id.textTime); 

        ImageView imageView = (ImageView) row.findViewById(R.id.imageView); 

        txtContact.setText(Request_Contact.get(position)); 
        txtNameSurname.setText(Request_NameSurname.get(position)); 
        txtMessageCount.setText(Request_MessageCount.get(position)); 
        txtTime.setText(Request_Time.get(position)); 

        Picasso.with(context).load(arrRequest_Image.get(position)).transform(new CircleTransform()).placeholder(R.drawable.ic_launcher).into(imageView);       

        return row; 
        } 
       } 
+0

從代碼中不清楚適配器對象可以看到的數據。僅僅因爲數據正常下降,並不意味着它正確地路由到適配器對象中。 考慮用更多的代碼/方法進行更新。 – FishStix

+0

@FishStix - 我使用代碼 – user3560827

+0

進行了更新當您構建適配器時,它看起來像是在傳遞數據的當前狀態。使用其他數據(網絡)訪問方法,它看起來不像是將任何更新狀態傳遞給適配器。 – FishStix

回答

1

你需要重寫clear方法在ContactsAdapter真正清除您存儲在數據列表。

它看起來像你需要清除所有名單,因此,如果您添加到ContactsAdapter,您的代碼應該按預期工作:

@Override 
public void clear() { 
    super.clear(); 

    Request_Contact.clear(); 
    Request_NameSurname.clear(); 
    Request_MessageCount.clear(); 
    Request_Time.clear(); 
    Request_Image.clear(); 
}