我有一个自定义对话框片段,里面有自定义ArrayAdapter。每个列表项都有3个按钮。点击每个按钮,我有OnClickListener()
执行功能。功能完成后,我需要关闭对话框。因为这是在ArrayAdapter中完成的,所以我不能像在对话框片段中那样调用dismiss()
。我尝试为对话框片段内的按钮设置OnClickListener()
,但我得到一个空指针。
我无法弄清楚如何从自定义arrayAdapter中获取对话框片段的引用。 onclick事件必须在ArrayAdapter内部发生,因为它使用特定于该列表项的信息。任何帮助将不胜感激。谢谢。
DialogFragment:
public class SavedArmyDialog extends DialogFragment {
View mView;
public static SavedArmyDialog newInstance() {
SavedArmyDialog fragment = new SavedArmyDialog();
return fragment;
}
public SavedArmyDialog() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mView = inflater.inflate(R.layout.custom_army_list, container, false);
CustomArmyObjData customArmyObjData = new CustomArmyObjData();
ArrayList<CustomArmy> customArmyArray = customArmyObjData.getCustomArmyArray(getActivity());
CustomArmyAdapter adapter = new CustomArmyAdapter(getActivity(), customArmyArray);
ListView armyList = (ListView) mView.findViewById(R.id.armyListView);
armyList.setAdapter(adapter);
return mView;
}
}
自定义ArrayAdapter:
public View getView(int position, View convertView, ViewGroup parent) {
final CustomArmy army = getItem(position);
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.individual_army_view, parent, false);
holder.armyname = (TextView) convertView.findViewById(R.id.army_name_value);
holder.townHall = (TextView) convertView.findViewById(R.id.townHallValue);
holder.armyType = (TextView) convertView.findViewById(R.id.armyTypeValue);
holder.loadButton = (Button) convertView.findViewById(R.id.loadButton);
holder.deleteButton = (Button) convertView.findViewById(R.id.deleteButton);
holder.gridLayout = (GridLayout) convertView.findViewById(R.id.armyGrid);
buildArmyCountAndIconViews(army, holder.gridLayout);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
holder.gridLayout.removeAllViews();
buildArmyCountAndIconViews(army, holder.gridLayout);
}
holder.armyname.setText(army.getName());
holder.townHall.setText(army.getTownHallRequired());
holder.armyType.setText(army.getArmyType());
holder.loadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mBus.post(new LoadNewArmy(army));
Toast.makeText(getContext(), "Woking Button", Toast.LENGTH_SHORT).show();
//need to close the dialog from here
}
});
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Utilities.deleteCustomArmy(army.getName(), getContext());
Toast.makeText(getContext(), "Army has Been Deleted", Toast.LENGTH_LONG).show();
//need to close the dialog from here
}
});
return convertView;
}
答案 0 :(得分:5)
我无法弄清楚如何从中获取对话框片段的引用 在自定义arrayAdapter中
DialogFragment也有dismiss ()方法,需要在关闭DialogFragment
时调用它。
在您的情况下,请执行以下操作:
1。在CustomArmyAdapter
类构造函数中再添加一个参数,类型为SavedArmyDialog
:
private SavedArmyDialog objSavedArmyDialog;
public CustomArmyAdapter(SavedArmyDialog objSavedArmyDialog,...){
this.objSavedArmyDialog=objSavedArmyDialog;
....
}
2。现在,按钮的objSavedArmyDialog.dismiss()
内的onClick
级:
public void onClick(View v) {
....
//need to close the dialog from here
objSavedArmyDialog.dismiss();
}
3. 通过将当前类Context传递为:
来创建CustomArmyAdapter
类对象
CustomArmyAdapter adapter=new CustomArmyAdapter(SavedArmyDialog.this,
getActivity(),
customArmyArray);