2014-07-16 95 views
0

我想关闭对话框后,用户单击对话框中驻留的ListView中的项目。这里是我的listDialog方法被调用来打开对话框:关闭AlertDialog onItemClick后 - 自定义ListViewDialog

public void listDialog() { 
LayoutInflater shoppingListInflater = LayoutInflater.from(this); 

//volley request to get listview data 
getListsRequest(); 

final View allListsView = shoppingListInflater.inflate(R.layout.list_picker_dialog, null); 

listsPickerView = (ListView) allListsView.findViewById(R.id.shopping_lists_all); 

shoppingLists = new ArrayList<ShoppingList>(); 

allListsAdapter = new ShoppingListsAdapter(this, shoppingLists); 

listsPickerView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() { 
    @Override public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) { 

     shopping_list_id = ((TextView) view.findViewById(R.id.list_id)).getText().toString(); 
     JSONObject listItemData = new JSONObject(); 

     try { 
      listItemData.put("inventory_item_id", inventory_item_id); 
      listItemData.put("shopping_list_id", shopping_list_id); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

// a volley network call to an api 
     createListItemRequest(listItemData); 

//where I want to close the dialog - after the network call. Not working currently 
     alertD.cancel(); 
    } 
}); 

listsPickerView.setAdapter(allListsAdapter); 

AlertDialog.Builder listsDialog = new AlertDialog.Builder(this); 

listsDialog.setView(allListsView); 

listsDialog.setCancelable(false) 
      .setNegativeButton("Cancel", 
        new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        dialog.cancel(); 
       } 
      }); 

    AlertDialog alertD = listsDialog.create(); 

    alertD.show(); 

} 

除对话框取消之外的一切工作。由于alertD不在ItemClickListener中,因此我无法引用它。网络通话后如何关闭此对话框 - 参考上面的内容?

+0

用户.dismiss近距离dialoge –

回答

1

与JavaScript不同,Java变量的范围从定义的地方开始。因此,在定义之前,您无法引用alertD。

通话刚刚转会listsPickerView.setOnItemClickListener alertD的,另外在后面定义声明alertD为决赛:

final AlertDialog alertD = listsDialog.create(); 

listsPickerView.setOnItemClickListener(....); 

alertD.show(); 
+0

优秀,并感谢您在这里解释JS的区别和Java。 – settheline