onResume不会使用新数据更新ListView

时间:2016-06-22 18:40:29

标签: android

我试图在按下后退按钮后更新上一个片段的Lis​​tView。调用onResume(使用Toast验证)并运行webservice(清除后显示listView)。问题是,在调用accessWebService_getUsername之后,ListView仍然显示旧值而不是新值。我验证了MySQL的值,即使更新了DB,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;
                    }
                }

1 个答案:

答案 0 :(得分: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();
}