首页 > 解决方案 > 我无法使用从 Activity 传递到片段的捆绑字符串数据在 textView 中设置文本

问题描述

数据从 ProjectDetailActivity 传递到 ProjectDetailFragment ,我已经检查了数据是否可以通过使用 Toast 显示数据传递给片段,如您在代码中看到的那样,但问题是我无法使用来自 bundle 的数据在 Textview 中设置文本。如果您在此代码中看到一些错误,请告诉我。非常感谢。

ProjectDetailActivity 类

         private String project_key,project_name,project_priority,project_category,
                        project_start_date ,project_end_date,project_description;
         @Override
         protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_project_detail);

              Intent intent = getIntent();
              project_key         = intent.getStringExtra("project_key");
              project_name        = intent.getStringExtra("project_name");
              project_category    = intent.getStringExtra("project_category");
              project_priority    = intent.getStringExtra("project_priority");

              Bundle bundle = new Bundle();
              bundle.putString("project_key",project_key);
              bundle.putString("project_name",project_name);
              bundle.putString("project_category",project_category);
              bundle.putString("project_priority",project_priority);

              ProjectDetailFragment project_detail_fragment =new ProjectDetailFragment();
              project_detail_fragment.setArguments(bundle);
              getSupportFragmentManager().beginTransaction()
                .replace(R.id.project_detail_fragment,project_detail_fragment)
                .commit();

          }
     }

ProjectDetailFragment 类

 public class ProjectDetailFragment extends Fragment {
      private View root;

      private String project_key,project_name,project_priority,project_category,


      private TextView project_name_textView , project_priority_textView ,
                       project_category_textView ;

      private Context mContext;

      public ProjectDetailFragment() { }

      @Override
      public void onCreate(@Nullable Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           if (getArguments() != null) {
                Bundle bundle = getArguments();
                project_key           = bundle.getString("project_key");
                project_name          = bundle.getString("project_name");
                project_priority      = bundle.getString("project_category");
                project_category      = bundle.getString("project_priority");
           }
      }

      public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle 
                  savedInstanceState) {
            root =  inflater.inflate(R.layout.fragment_project_detail, container,false);

            project_name_textView        = root.findViewById(R.id.project_name_textView);
            project_priority_textView    = root.findViewById(R.id.priority_textView);
            project_category_textView    = root.findViewById(R.id.category_textView);


            project_name_textView.setText(project_name);
            project_priority_textView.setText(project_priority);
            project_category_textView.setText(project_category);

            Toast.makeText(mContext,"TASK ACTIVITY\n"+
                    project_key+"\n"+project_name+"\n"+project_category+"\n"
                    +project_priority+"\n",Toast.LENGTH_SHORT).show();

            return root;
       }

       @Override
       public void onAttach(Context context) {
            super.onAttach(context);
            mContext=context;
       }
 }

标签: javaandroidtextviewbundle

解决方案


如果 Toast 向您显示数据,则表示接收数据没有问题,并且我看不到将数据设置为文本视图时没有错误,但请检查您的 xml,如果文本颜色与背景颜色匹配,请检查文本颜色,例如文本视图背景是白色的,文本颜色也是白色的,显然比你看不到文本。或者如果没有,你可以这样尝试:::: project_name_textView.setText(String.value of(project_name));


推荐阅读