Not seeing EditNameDialogListener interface
Asked Answered
E

3

5

I'd like to pass back user typed string in the dialog fragment: "AddFriendDialogFragment.java" back to the activity that had called it: "HomeActivity.java". I'm doing this thru an interface declared inside "AddFriendDialogFragment.java": "EditNameDialogListener". However for some reason, HomeActivity is not seeing this interface, so I'm getting a "Cannot resolve symbol: "EditNameDialogListener" error.

"HomeActivity.java":

package tutorial.com.example.jerryhou.dialogactionbartutorial;

import android.app.Activity;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;


public class HomeActivity extends Activity implements EditNameDialogListener 
{


@Override
public void onFinishEditDialog(String inputText)
{
    Toast.makeText(this, "Hi, " + inputText, Toast.LENGTH_SHORT).show();
}
public void showUsernameSearchDialog(View v)
{
    FragmentManager fragmentManager = getFragmentManager();
    DialogFragment newFragment = new AddFriendDialogFragment();
    newFragment.show(fragmentManager, "AddFriendDialog");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_home, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
 }

"AddFriendDialogFragment.java":

package tutorial.com.example.jerryhou.dialogactionbartutorial;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;


public class AddFriendDialogFragment extends DialogFragment
{

    private static final String TAG = "AddFriendDialogFragment";

    public interface EditNameDialogListener
    {
        void onFinishEditDialog(String inputText);
    }
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();


        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        View addFriendDialogView = inflater.inflate(R.layout.fragment_add_friend_dialog, null);
        // Set an EditText view to get user input
        final EditText usernameEditText = (EditText) addFriendDialogView.findViewById(R.id.username);

        builder.setView(addFriendDialogView)
                // Add action buttons
                .setPositiveButton(R.string.search, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id)
                    {
                        //Retrieve Username typed in
                        String username_querystr = usernameEditText.getText().toString();

                        //Correctly retrieving query str
                        Log.v(TAG, "Going to search " + username_querystr);

                        //Pass back query str to search in HomeActivity

                        EditNameDialogListener activity = (EditNameDialogListener) getActivity();
                        activity.onFinishEditDialog(usernameEditText.getText().toString());

                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        AddFriendDialogFragment.this.getDialog().cancel();
                    }
                });


        return builder.create();
    }

}
Empathic answered 16/12, 2014 at 0:22 Comment(1)
Strange, because any decent ide would sort this out for you by prefixing the outer class name. What ide are you using?Whip
A
5

Even if they are in the same package, you can't use EditNameDialogListener directly as it's an inner interface.

You have 2 choices here :

-> Use EditNameDialogListener fully qualified name, i.e AddFriendDialogFragment.EditNameDialogListener

public class HomeActivity extends Activity implements AddFriendDialogFragment.EditNameDialogListener

-> Import explicitly EditNameDialogListener

import tutorial.com.example.jerryhou.dialogactionbartutorial.AddFriendDialogFragment.EditNameDialogListener

public class HomeActivity extends Activity implements EditNameDialogListener
Aleta answered 19/12, 2014 at 23:34 Comment(0)
U
3

The name of the implemented interface should be AddFriendDialogFragment.EditNameDialogListener

public class HomeActivity extends Activity implements AddFriendDialogFragment.EditNameDialogListener
Ultraconservative answered 19/12, 2014 at 17:20 Comment(0)
A
0

You could either copy the interface into a separate compilation unit / file

OR

probably using the static scope identifier in front your interface

public static interface EditNameDialogListener
{...}

and something like @Hacketo mentioned will help:

public class HomeActivity extends Activity implements AddFriendDialogFragment.EditNameDialogListener
Apron answered 26/12, 2014 at 16:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.