2012-08-23 14 views
0

我为的删除确认建立在我的ListActivity一个LongClick其调用与选项deletecancelAlertDialogue,紧接着又AlertDialogue未能刷新ListActivity以下数据库删除

不过,我需要刷新列表以显示更改,但不能看到如何做到这一点(列表填充使用ResourceCursorAdapter):

public class InspectionActivity extends ListActivity { 
private Button newInspection; 
private static final int ACTIVITY_CREATE=0; 

private RMDbAdapter rmDbHelper; 
//private AlertDialog longClickOptionsDialog; 
private AlertDialog clickOptionsDialog; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_inspection); 
    setUpViews(); 
    setLongClick(); 

    rmDbHelper = new RMDbAdapter(this); 
    rmDbHelper.open(); 

    // Get a Cursor for the list items 
    Cursor listCursor = rmDbHelper.fetchAllInspections(); 
    startManagingCursor(listCursor);   

    // set the custom list adapter  
    setListAdapter(new MyListAdapter(this, listCursor)); 


} 



private void setUpViews() { 
    newInspection = (Button)findViewById(R.id.new_inspection); 
    newInspection.setOnClickListener(new View.OnClickListener() {      
     public void onClick(View v) { 
      createInspection(); 
     } 
    }); 

} 

private void setLongClick() { 
    ListView lv = getListView(); 
    lv.setOnItemLongClickListener(new OnItemLongClickListener() { 
     public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int row, final long id) { 
      Cursor cursor = (Cursor) rmDbHelper.fetchInspection(id); 

      String inspectionRef = RMUtilities.notEmpty(cursor.getString(cursor.getColumnIndex( 
        RMDbAdapter.INSPECTION_REF)), "Reference unknown"); 
      String companyName = RMUtilities.notEmpty(cursor.getString(cursor.getColumnIndex( 
        RMDbAdapter.INSPECTION_COMPANY)), "company unknown"); 
      final String inspectionDialogueText = "(" + inspectionRef + ", " + companyName + ")"; 

      clickOptionsDialog = new AlertDialog.Builder(InspectionActivity.this) 
      .setTitle(inspectionDialogueText) 
      .setMessage(R.string.general_text_select_option) 
      .setPositiveButton(R.string.general_button_delete, new AlertDialog.OnClickListener(){ 
       private AlertDialog confirmDeleteDialog; 
       public void onClick(DialogInterface dialog, int which){ 
        confirmDeleteDialog = new AlertDialog.Builder(InspectionActivity.this) 
        .setTitle(R.string.general_text_confirmation) 
        .setMessage(R.string.general_text_confirm_delete) 
        .setPositiveButton(R.string.general_button_yes, new AlertDialog.OnClickListener(){ 
         public void onClick(DialogInterface dialog, int which){ 
          rmDbHelper.deleteInspection(id); 

**//REFRESH STATEMENT NEEDED HERE..** 
          } 
         }) 
        .setNegativeButton(R.string.general_button_cancel, new AlertDialog.OnClickListener(){ 
         public void onClick(DialogInterface dialog, int which){ 
          confirmDeleteDialog.cancel(); 
          } 
         }) 
        .create(); 
        confirmDeleteDialog.show(); 
        } 
       })     
      .setNegativeButton(android.R.string.cancel, new AlertDialog.OnClickListener(){ 
       public void onClick(DialogInterface dialog, int which){ 
        clickOptionsDialog.cancel(); 
        } 
       }) 
      .create(); 
      clickOptionsDialog.show(); 
      return true; 
     } 
    }); 
} 

// In your ListActivity class, create a new inner class that extends ResourceCursorAdapter. 
//This inner class is the custom CursorAdapter we will use to manage how data is bound to a list item: 

private class MyListAdapter extends ResourceCursorAdapter { 

    public MyListAdapter(Context context, Cursor cursor) { 
     super(context, R.layout.inspection_row, cursor); 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     TextView title = (TextView) view.findViewById(R.id.inspection_row_item_main_title); 
     title.setText(cursor.getString(       
       cursor.getColumnIndex(RMDbAdapter.INSPECTION_REF))); 

     TextView description = (TextView) view.findViewById(R.id.inspection_row_item_description);   

     String companyName = RMUtilities.notEmpty(cursor.getString(cursor.getColumnIndex(
         RMDbAdapter.INSPECTION_COMPANY)), "Company unknown"); 
     String inspectionDate = RMUtilities.notEmpty(cursor.getString(cursor.getColumnIndex(
         RMDbAdapter.INSPECTION_DATE)), "date unknown"); 
     description.setText("(" + companyName + ", " + inspectionDate + ")"); 
    } 
} 


private void createInspection() { 
    Intent i = new Intent(this, InspectionEdit.class); 
    startActivityForResult(i, ACTIVITY_CREATE); 
} 


protected void onListItemClick(ListView l, View v, final int pos, final long id){ 


    Cursor cursor = (Cursor) rmDbHelper.fetchInspection(id); 

    String inspectionRef = RMUtilities.notEmpty(cursor.getString(cursor.getColumnIndex( 
      RMDbAdapter.INSPECTION_REF)), "Reference unknown"); 
    String companyName = RMUtilities.notEmpty(cursor.getString(cursor.getColumnIndex( 
      RMDbAdapter.INSPECTION_COMPANY)), "company unknown"); 
    final String inspectionDialogueText = "(" + inspectionRef + ", " + companyName + ")"; 

    super.onListItemClick(l, v, pos, id); 
    clickOptionsDialog = new AlertDialog.Builder(InspectionActivity.this) 
    .setTitle(inspectionDialogueText) 
    .setMessage(R.string.general_text_select_option) 
    .setPositiveButton(R.string.general_button_open, new AlertDialog.OnClickListener(){ 
     public void onClick(DialogInterface dialog, int which) { 
      Intent i = new Intent(InspectionActivity.this, AreaActivity.class); 
      i.putExtra("intentID", id); 
      startActivityForResult(i, ACTIVITY_CREATE); 
      } 
     }) 
    .setNeutralButton(R.string.general_button_edit, new AlertDialog.OnClickListener(){ 
     public void onClick(DialogInterface dialog, int which){ 
      Intent i = new Intent(InspectionActivity.this, InspectionEdit.class); 
      i.putExtra("intentID", id); 
      startActivityForResult(i, ACTIVITY_CREATE); 
      } 
     }) 

    .setNegativeButton(android.R.string.cancel, new AlertDialog.OnClickListener(){ 
     public void onClick(DialogInterface dialog, int which){ 
      clickOptionsDialog.cancel(); 
      } 
     }) 
    .create(); 
    clickOptionsDialog.show(); 

} 

感谢您的帮助。

亲切的问候,

戴夫

+0

检查这个 http://stackoverflow.com/questions/5650086/refresh-an-listview-after-insert-on-database – Aamirkhan

回答

0

您需要重新拉动光标。您正在更改数据库,因此您需要更新光标,这会导致您的列表被更新。

根据你所显示的代码,listCursor.requery()应该为你做。

请注意,对于较新的API,您应该使用CursorLoaders将数据活动从UI线程移出。

+0

谢谢巴拉克 - 这是做的伎俩。将看看CursorLoaders,但我现在正在2.2版本的开发(不知道这是否公平)。 – Scamparelli

1

尝试使用notifyDataSetChanged()适配器。

youradapter.notifyDataSetChanged() 
+0

不,不行 - 我只得到通知选项()或notifyAll()对我的适配器... – Scamparelli