Fattie
Fattie

Reputation: 12625

Get "the" dialogFragment from a ListView setOnItemClickListener view

Here's one! You have a ...

public class SomePopup extends DialogFragment implements View.OnClickListener

it creates a ListView inside itself

public ListView happyListView;
public HappyAdapter ourAdaptor;

Then inside this ...

public View onCreateView(LayoutInflater inflater,
    ViewGroup container,
    Bundle savedInstanceState)

.. as usual you set up the list. in this case the user can click on "any row", and do this to it...which is also inside onCreateView...

happyListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
      {
      ParseUser chosen = (ParseUser) happyListView.getItemAtPosition(position);
      Utils.Log("YOU CLICKED ON .......... " + .. chosen);

      // actually close this entire DialogFragment
      SOMETHING.dismiss();

      }
    });

Notice the SOMETHING. How do you do that?!

I solved it by calling back to the class which created the DialogFragment and having that instance .dismiss() the DialogFragment.

But how do you get at "this" DialogFragment when you're inside happyListView.setOnItemClickListener?

Cheers

Upvotes: 0

Views: 761

Answers (1)

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

You can call the class and put the this (as the reference to your SomePopup) after the class nameSomePopup.this.. it will only work if happyListView.setOnItemClickListener is inside the dialogfragment.. because it is an anonymous class..

example:

happyListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
  {
  ParseUser chosen = (ParseUser) happyListView.getItemAtPosition(position);
  Utils.Log("YOU CLICKED ON .......... " + .. chosen);

  // actually close this entire DialogFragment
  SomePopup.this.dismiss();

  }
});

Upvotes: 3

Related Questions