【问题标题】:Change fragment's Textview from main activity从主要活动更改片段的 Textview
【发布时间】:2017-06-26 12:59:34
【问题描述】:

我一直在关注如何使用三个片段实现底部导航栏的教程。当我需要更新我的个人资料片段的文本视图时,我现在卡住了。 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;
}

个人资料片段代码:

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 中调用该方法时,我都会收到一个空指针异常错误。请问有人可以帮我吗?

【问题讨论】:

  • 你在哪里调用了 updateTextView(String text) 方法?

标签: java android android-fragments


【解决方案1】:

您需要在MainActivity 中进行以下更改:

使用方法通过标签获取您的片段(即ProfileFragment):

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

这里的标签是一个字符串常量,你需要在替换片段时添加它 下面:

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

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

【讨论】:

    【解决方案2】:

    Fragment 中创建getInstance() 函数

    public static ProfileFragment mProfileFragment =null;
    public static ProfileFragment getInstance(){
    if(mProfileFragment == null){
    mProfileFragment = new ProfileFragment();
    }
    return mProfileFragment;
    }
    

    在您的Activity 类中,当您尝试设置文本值时,请检查Fragment 的第一个实例是否为null。如果不为空,则设置文本值。

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-24
        • 1970-01-01
        • 2022-01-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多