从主要活动更改片段的Textview

时间:2017-06-26 12:59:35

标签: java android android-fragments

我一直在关注如何使用三个片段实现底部导航栏的教程。当我需要更新我的个人资料片段的textview时,我现在卡住了。 MainActivity代码:

public class MainActivity extends AppCompatActivity {
    String activeUser = "", response="myresponse";
    ProgressDialog progDailog;

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

    activeUser = getIntent().getStringExtra("currentUser");

    setupNavigationView();

    new getProfileData().execute("myurl");

    getSupportActionBar().setTitle("Home");


}


private void setupNavigationView() {
    BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
    if (bottomNavigationView != null) {

        // Select first menu item by default and show Fragment accordingly.
        Menu menu = bottomNavigationView.getMenu();
        selectFragment(menu.getItem(1));

        // Set action to perform when any menu-item is selected.
        bottomNavigationView.setOnNavigationItemSelectedListener(
                new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        selectFragment(item);
                        return false;
                    }
                });
    }
}

/**
 * Perform action when any item is selected.
 *
 * @param item Item that is selected.
 */
protected void selectFragment(MenuItem item) {

    item.setChecked(true);

    switch (item.getItemId()) {
        case R.id.menu_home:
            // Action to perform when Home Menu item is selected.
            pushFragment(new HomeFragment());

            break;
        case R.id.menu_news:
            // Action to perform when News Menu item is selected.
            pushFragment(new NewsFragment());
            break;
        case R.id.menu_profile:
            // Action to perform when Profile Menu item is selected.
            pushFragment(new ProfileFragment());
            break;
    }
}

/**
 * Method to push any fragment into given id.
 *
 * @param fragment An instance of Fragment to show into the given id.
 */
protected void pushFragment(Fragment fragment) {
    if (fragment == null)
        return;

    FragmentManager fragmentManager = getFragmentManager();
    if (fragmentManager != null) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        if (ft != null) {
            ft.replace(R.id.main_container, fragment);
            ft.commit();
        }
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.three_dots_items, menu);
    return true;
}

ProfileFragment代码:

public class ProfileFragment extends Fragment {

TextView profile;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.profile_fragment, container, false);
    profile = (TextView) view.findViewById(profileData);
    return  view;
}

public void updateTextView(String text){
    profile.setText(text);
}

我的profilefragment.xml有一个带有id profileData的textview。在我的ProfileFragment java类中,我介绍了一个方法updateTextView来更新配置文件布局,但我不知道在mainactivity.java中何处或如何调用此方法。我已经搜索了关于这个主题的几个问题并尝试了在stackoverflow上提出的不同解决方案,但每当我尝试在mainactivity中调用该方法时,我都会收到空指针异常错误。请有人帮帮我吗?

3 个答案:

答案 0 :(得分:1)

您需要在MainActivity中进行更改:

使用以下方法获取您的片段(即ProfileFragment):

Profile Fragment fragment =  (ProfileFragment)getSupportFragmentManager().findFragmentByTag("tag");
 if(fragment != null)
 fragment.updateTextView();

这里tag是一个String常量,需要在将fragment替换为时添加 下面:

在您的代码中进行以下更改

FragmentTransaction ft = fragmentManager.beginTransaction();
    if (ft != null) {
        ft.replace(R.id.main_container, fragment,"tag");

答案 1 :(得分:0)

getInstance()

中创建Fragment功能
public static ProfileFragment mProfileFragment =null;
public static ProfileFragment getInstance(){
if(mProfileFragment == null){
mProfileFragment = new ProfileFragment();
}
return mProfileFragment;
}

当您尝试设置文本值时,在Activity课程中,检查Fragment的第一个实例是 null 。如果不是null设置文本值。

if(ProfileFragment.getInstance() != null){
textview.SetText("Your Text Value here"); 
}

答案 2 :(得分:0)

使用Fragment替换容器的内容时​​,您始终可以通过使用该容器的ID调用Fragment来获取当前显示的getSupportFragmentManaget().findFragmentById(...)。然后,您可以检查当前片段是否为ProfileFragment,如果是,则进行更新:

    Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.main_container);
    if (fragment instanceof ProfileFragment) {
        ((ProfileFragment) fragment).updateTextView(text);
    }