从片段到活动问题的接口通信

时间:2014-07-19 21:32:45

标签: android android-fragments

我最近尝试使用接口进行片段活动通信。这个想法是当一个片段中按下一个按钮时,它从同一个片段中的EditText中检索数据,然后将该字符串发送到MainActivty - 这将控制我的所有片段 - 然后启动另一个片段并将字符串传递给它片段供以后使用,但是,我在初始设置发送数据的第一个接口时遇到了麻烦。不幸的是没有任何事情发生,因此我无法进入应该显示的下一个片段。另外我尝试使用getActivity()但它无法在片段中找到相关的方法,这让我相信片段不会直接连接到MainActivity(我只是抓住了Java的基础知识和很少的Android,只是学习。)

我已在下面列出相关信息,感谢您的帮助!

片段

public class CreateWorkoutFragment extends Fragment implements OnClickListener {

View rootViewCreateWorkoutFragment;

EditText editTextWorkoutName;

// Using an ImageView for custom button
ImageView buttonNext;

String valueCreateWorkoutEditText;

OnDataPass dataPasser;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    rootViewCreateWorkoutFragment = inflater.inflate(R.layout.fragment_create_workout, container, false);

    buttonNext = (ImageView) rootViewCreateWorkoutFragment.findViewById(R.id.button_workout_name_next);

    editTextWorkoutName = (EditText) rootViewCreateWorkoutFragment.findViewById(R.id.edit_text_workout_name);

    buttonNext.setOnClickListener(this);

    return rootViewCreateWorkoutFragment;
}

public interface OnDataPass {
    public void onDataPass(String data);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    dataPasser = (OnDataPass) activity;
}

public void passData(String data) {
    dataPasser.onDataPass(data);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button_workout_name_next:
            valueCreateWorkoutEditText = editTextWorkoutName.getText().toString();
            passData(valueCreateWorkoutEditText);
            break;
    }
}
}

主要活动

public class MainActivity extends FragmentActivity implements OnClickListener, CreateWorkoutFragment.OnDataPass {

ImageView buttonCreate;

Fragment newFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(R.style.AppThemeBlue);
    setContentView(R.layout.activity_main);

    buttonCreate = (ImageView) findViewById(R.id.create_foreground);

    buttonCreate.setOnClickListener(this);

    FragmentManager fragManager = getSupportFragmentManager();
    FragmentTransaction tranManager = fragManager.beginTransaction();
    CreateWorkoutFragment createWorkoutFrag = new CreateWorkoutFragment();

    // fragment_change is just the area in XML where fragments switch
    tranManager.add(R.id.fragment_change, createWorkoutFrag);
    tranManager.commit();

    newFragment = null;
}

@Override
public void onDataPass(String data) {
    // CreateFragment is not to be confused with CreateWorkoutFragment
    // CreateFragment is the fragment I'm trying to start when any strings
    // are obtained from CreateWorkoutFragment
    newFragment = new CreateFragment();
}

@Override
public void onClick(View v) {

    switch (v.getId()) {
    // create_foreground is just an ImageView used as a button
    // Additionaly, other buttons are used to create other fragments,
    // I've cut them out currently as they are not nessesary which is
    // why CreateWorkoutFragment is only button and default currently
    case R.id.create_foreground:
        newFragment = new CreateWorkoutFragment();
        break;
    default:
        newFragment = new CreateWorkoutFragment();
    }

FragmentTransaction tranManager = getSupportFragmentManager().beginTransaction();
    tranManager.replace(R.id.fragment_change, newFragment);
    tranManager.addToBackStack(null);
    tranManager.commit();
}
}

对不起,代码并不完全整洁,但是,它是从大型课程中删除的最相关的代码。正如我所说,我尝试过其他方法但无论如何也无法从MainActivity获得任何响应。提前谢谢!

就在我发布之前:让应用程序向我写入logcat消息,它设法在单击按钮时传递数据 - 至少我认为,并且与片段有关没开始!在MainActivity> onDataPass()> new Fragment = new CreateFragment()任何想法?如前所述,其他按钮确实存在并设法更改片段。但是,为了减少发布的代码量,我们进行了剪裁。

1 个答案:

答案 0 :(得分:1)

  

getActivity()但它无法在片段

中找到关联的方法

这是因为getActivity()会返回Activity,而不是MainActivity,这是您的自定义子类。您可以使用演员轻松修复此问题。例如,在您的片段中,您可以执行以下操作:

OnDataPass main = (OnDataPass) getActivity();
main.onDataPass(message);

由于需要这样的演员表,界面似乎在我看来妨碍了。您可以轻松地直接投射到MainActivity

MainActivity main = (MainActivity) getActivity();
main.onDataPass(message);