2014-05-18 35 views
0

我打电话给我的ArrayAdapterActivity,它显示了我的数据库中的数据集列表。如果用户点击ArrayAdapter的项目,它将打开AlertDialog的扩展。如果用户点击此AlertDialog中的按钮,数据库中的数据集将被删除,其中AlertDialogdismiss()。现在我想刷新ArrayAdapter的视图。刷新从ArrayAdapter的AlertDialog后的活动

我发现像

remove(position); 
notifyDataSetChanged(); 

但我在哪里可以把这个解决方案?如果用户点击特定按钮来说“hey adapter,请删除列表中的当前项目”,我可以将AlertDialog的布尔值向后传递给ArrayAdapter吗?

这里的一些代码:

活动:

final ListView list = (ListView) findViewById(R.id.myList); 
DBhandler db = new DBhandler (context); 
list.setAdapter(new MyAdapter(context, db.getMyItems())) 

MyAdapter:

public View getView(int position, View row, ViewGroup parent) { 
    if(row != null) { 
     row.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       final MyDialog myDialog = new MyDialog(context, currentItem); 
       myDialog.show(); 
      } 
     }); 
    } 
} 

MyDialog:

protected void onCreate(Bundle savedInstanceState) { 
    Button btn_delete = (Button) findViewById(R.id.btn_delete); 
    btn_delete.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      DBhandler db = new DBhandler (context); 
      db.delete(currentItem); 
      Toast.makeText(context, "item is deleted", Toast.LENGTH_SHORT).show(); 
      dismiss(); 
     } 
    }); 
} 

我还发现Passing Events Back to the Dialog's Host,但我不“知道我该如何使用,对于我的AlertDialog ...

回答

0

你可以叫你的自定义适配器的功能驳回警告对话框,

if(adapter.remove(postion)){ 
    adapter.refreshdata(); 
} 

和像

public boolean removedata(int position){ 
    boolean isremoved = false; 
    // loop array data and remove specific item 
    // and return isRemoved value. 
    } 
public void refreshdata(){ 
     notifyDataSetChanged(); 
     } 
+0

遗憾的功能之前,我不明白...我应该在哪里调用这些方法? AlertDialog中没有适配器实例,是吗? – lis