2016-06-22 22 views
1

戻るボタンを押した後、前のフラグメントでListViewを更新しようとしています。 onResumeが呼び出され(Toastで検証されます)、Webサービスが実行されます(listViewがクリアされた後に表示されます)。問題は、ListViewがaccessWebService_getUsernameが呼び出された後に新しい値ではなく古い値を表示し続けていることです。私は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 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(); 

      } 

    } 

UPDATE 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

あなたが実際にあなたがあなたのデータを格納しているリストをクリアするために、あなたのContactsAdapterでclearメソッドをオーバーライドする必要があります。

あなたにこれを追加するので、もしあなたが、すべてのあなたのリストをクリアする必要がありますように見えますContactsAdapterのコードは、期待どおりに動作するはずです。

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

    Request_Contact.clear(); 
    Request_NameSurname.clear(); 
    Request_MessageCount.clear(); 
    Request_Time.clear(); 
    Request_Image.clear(); 
} 
関連する問題