Ephraim Schmitt
Ephraim Schmitt

Reputation: 227

Closing dialog from inside a custom ArrayAdapter

I have a custom dialog fragment that has a custom ArrayAdapter inside. Each one of the list items has 3 buttons. On the click of each button I have an OnClickListener() that does a function. After the function is complete I need to have the dialog closed. Because this is done in the ArrayAdapter I cannot call dismiss() like I would inside of the dialog fragment. I tried setting up a OnClickListener() for the buttons inside of the dialog fragment, but I get a null pointer.

I cannot figure out how to get a reference to the dialog fragment from inside the custom arrayAdapter. The onclick event has to happen inside of the ArrayAdapter because it uses info specific to that list item. Any help would be greatly appreciated. Thank you.

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;
}


}

Custom 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;
}

Upvotes: 1

Views: 3660

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

I cannot figure out how to get a reference to the dialog fragment from inside the custom arrayAdapter

DialogFragment also has a dismiss () method, need to call it when want to close DialogFragment.

In your case do it as:

1. Add one more parameter to CustomArmyAdapter class Constructor which is type of SavedArmyDialog :

private SavedArmyDialog objSavedArmyDialog;
public CustomArmyAdapter(SavedArmyDialog objSavedArmyDialog,...){
 this.objSavedArmyDialog=objSavedArmyDialog;
 ....
}

2. Now class objSavedArmyDialog.dismiss() inside onClick of Button :

 public void onClick(View v) {
   ....
   //need to close the dialog from here
   objSavedArmyDialog.dismiss();
  }

3. Create CustomArmyAdapter class object by passing current class Context as:

 CustomArmyAdapter adapter=new CustomArmyAdapter(SavedArmyDialog.this,
                                                  getActivity(), 
                                                  customArmyArray);

Upvotes: 9

Related Questions