---
title: "彈出的dialog如何更新下面的listview"
author: "ltwoini"
date: "2010-07-26T14:37:44Z"
tags: []
categories: ["编程语言"]
slug: "dialog-listview"
draft: false
lastmod: "2010-07-26T14:38:32Z"
id: 721401
url: "https://ltwoini.iteye.com/blog/721401"
---

兩種情況

1.兩個activity之間跳轉,當back的時候,主activity顯示會調用onResume()方法,所以刷新可以這樣處理:

 @Override  
 protected void onResume() {  
  super.onResume();  
  //onPause==&gt;onResume,重新獲取lv數據,刷新頁面  
  freeAdapter.setMData(AppDataManage.getData(true));  
  freeAdapter.notifyDataSetChanged();// 通知listview頁面重繪  
  paymentAdapter.setMData(AppDataManage.getData(false));  
  paymentAdapter.notifyDataSetChanged();  
  recentpayment.setMData(AppDataManage.getRecentData());  
  recentpayment.notifyDataSetChanged();  
 }

2.如果activiy里彈出dialog,并不會調用onResume()方法,處理方式如下:

 /\**  
  * @param context  
  * @param title  
  * @param item  
  * @param refreshAdapter listview's adapter做數據更新  
  \*/  
 public static void showProgreeCommentDialog(Context context, String title,  
   final App item,final AppCommentAdapter refreshAdapter) {  
  LayoutInflater inflater = LayoutInflater.from(context);  
  final View textEntryView = inflater.inflate(  
    com.cms.activity.R.layout.dialogcomment, null);  
  AlertDialog.Builder builder = new AlertDialog.Builder(context);  
  builder.setTitle(title);  
  builder.setIcon(com.cms.activity.R.drawable.comment);  
  builder.setView(textEntryView);  
  builder.setPositiveButton(SingleApplication.nfcstore.getResources().getString(R.string.finish), new DialogInterface.OnClickListener() {  
   public void onClick(DialogInterface dialog, int which) {  
    int num = item.getComments().size();  
    AppComment comment = new AppComment();  
    comment.setCommentDate(new Date());  
    Long zero = new Long(0);  
    comment.setId(++num+zero);  
    EditText etcomment = (EditText) textEntryView.findViewById(R.id.dialog\_comment);  
    comment.setContent(etcomment.getText().toString());  
    EditText etuser = (EditText) textEntryView.findViewById(R.id.dialog\_username);  
    comment.setCritics(etuser.getText().toString());  
    RatingBar rb = (RatingBar) textEntryView.findViewById(R.id.dialog\_ratingBar);  
    float rating=rb.getRating();  
    comment.setGrade(rating);  
    item.addComment(comment);  
    dialog.dismiss();  
    if(null != refreshAdapter){  
     refreshAdapter.setMData(AppDataManage.getCommentData(item));//設置新的數據  
     refreshAdapter.notifyDataSetChanged();       
    }  
   }  
  });  
  builder.setNegativeButton(SingleApplication.nfcstore.getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {  
   public void onClick(DialogInterface dialog, int which) {  
    dialog.dismiss();  
   }  
  });  
  builder.create().show();  
 }

 

attention:一定要重置更新后的數據,再通知更新.