Android onResume不使用新数据更新ListView
我试图在按下后退按钮后更新上一个片段的列表视图。调用onResume(使用Toast验证),运行webservice(清除后显示listView)。问题是调用accessWebService_getUsername后,ListView仍然显示旧值,而不是新值。我验证了MySQL中的值,即使数据库已更新,ListView也只返回旧值Android onResume不使用新数据更新ListView,android,Android,我试图在按下后退按钮后更新上一个片段的列表视图。调用onResume(使用Toast验证),运行webservice(清除后显示listView)。问题是调用accessWebService_getUsername后,ListView仍然显示旧值,而不是新值。我验证了MySQL中的值,即使数据库已更新,ListView也只返回旧值 @Override public void onResume() { Toast.makeText(
@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();
}
}
私有类JsonGetUsername扩展异步任务{
//待定01
private ProgressDialog=新建ProgressDialog(getActivity());
@凌驾
受保护的void onPreExecute(){
this.dialog.setMessage(“正在加载联系人,请稍候”);
this.dialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(参数[0]);
试一试{
HttpResponse response=httpclient.execute(httppost);
jsonResult=inputStreamToString(response.getEntity().getContent()).toString();
}
捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回null;
}
私有StringBuilder inputStreamToString(InputStream为){
字符串rLine=“”;
StringBuilder answer=新建StringBuilder();
BufferedReader rd=新的BufferedReader(新的InputStreamReader(is));
试一试{
而((rLine=rd.readLine())!=null){
答:追加(rLine);
}
}
捕获(IOE异常){
//e.printStackTrace();
Toast.makeText(getActivity(),“Error…”+e.toString(),Toast.LENGTH_LONG).show();
}
返回答案;
}
@凌驾
受保护的void onPostExecute(字符串结果){
//待定02
if(dialog.isShowing()){
dialog.dismise();
}
adapter.notifyDataSetChanged();
试试{
ListDrawer_getUsername();//具有ConnectionException(当它无法到达服务器时)
}捕获(例外e){
Toast.makeText(getActivity(),“请检查您的连接…”,Toast.LENGTH_LONG.show();
}
}
}//结束异步任务
public void accessWebService_getUsername(){
JsonGetUsername任务=新的JsonGetUsername();
//传递URL字符串数组的值
task.execute(新字符串[]{”http://mywebsite/php/get_username.php?pIMEI=“+IMEI});
}
//为列表视图生成哈希集
public void listprawer_getUsername(){
试一试{
JSONObject jsonResponse=新的JSONObject(jsonResult);
JSONArray jsonMainNode=jsonResponse.optJSONArray(“用户名\信息”);
for(int i=0;i
更新2:
//联系萨帕特
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;
}
}
class ContactsAdapter扩展了ArrayAdapter
{
语境;
列出请求联系人;
列出请求名称;
列表请求\消息计数;
列出请求时间;
列表请求图像;
ContactsAdapter(上下文c、列表请求\联系人、列表请求\名称、列表请求\消息计数、列表请求\时间、列表请求\图像)
{
超级(c、R.layout.activity\u contacts\u single、R.id.textContact、Request\u Contact);
context=c;
this.Request\u Contact=Request\u Contact;
this.Request\u nameurname=Request\u nameurname;
this.Request\u MessageCount=Request\u MessageCount;
这个.Request\u Time=Request\u Time;
this.Request\u Image=Request\u Image;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//TODO自动生成的方法存根
视图行=转换视图;
if(行==null)
{
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
row=充气机。充气(R.layout.activity\u contacts\u 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));
txtnamersurname.setText(请求_namersurname.get(位置));
txtMessageCount.setText(Request_MessageCount.get(position));
setText(请求时间获取(位置));
毕加索.with(context).load(arrRequest\u Image.get(position)).transform(new circlettransform()).pla
@Override
public void clear() {
super.clear();
Request_Contact.clear();
Request_NameSurname.clear();
Request_MessageCount.clear();
Request_Time.clear();
Request_Image.clear();
}