2014-05-18 39 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