【问题标题】:Dynamically adding Text views through java code in android在android中通过java代码动态添加Text视图
【发布时间】:2017-11-22 12:09:37
【问题描述】:

我想根据从数据库收到的信息在我的应用程序中动态添加文本视图。我引用了线性布局,然后在循环中实例化文本视图,将 LayoutParams 设置为它们,然后将它们添加到布局中,所有这些都是在静态内部片段类中完成的。我已经完成了几乎所有解决方案,并且我正在做与其他人相同的事情,但是当我在设备上运行我的应用程序时,我看不到动态添加的视图。请帮我解决这个问题。 这是我的代码的副本:

 public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        final int PICKFILE_RESULT_CODE = 1;
        String FilePath="";
        UploadFileAsync u;

        public PlaceholderFragment() {
        }

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView;
//            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
//            textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
            int sectionNumber = getArguments().getInt(ARG_SECTION_NUMBER);
            switch (sectionNumber){
                case 1: rootView = inflater.inflate(R.layout.fragment_faculty_home_info, container, false);
                    facultyInfoFragment(rootView);
                    break;
                case 2: rootView = inflater.inflate(R.layout.fragment_faculty_home_exam, container, false);
                    facultyExamFragment(rootView);
                    break;
                case 3: rootView = inflater.inflate(R.layout.fragment_faculty_home_info, container, false);
                    facultyEventsFragment(rootView);
                    break;
                default: rootView = inflater.inflate(R.layout.fragment_faculty_home_info, container, false);
            }
            return rootView;
        }

        public void facultyInfoFragment(View rootView){

            TextView facultyInfoName = (TextView)rootView.findViewById(R.id.faculty_info_name_view);
            facultyInfoName.setText("Ravi Singh");

            TextView facultyInfoID = (TextView)rootView.findViewById(R.id.faculty_info_ID_view);
            facultyInfoID.setText("12345");

            TextView facultyInfoDept = (TextView)rootView.findViewById(R.id.faculty_info_dept_view);
            facultyInfoDept.setText("CSE");

            Button facultyInfoCaddButton = (Button)rootView.findViewById(R.id.faculty_info_cADD_button);
            Button facultyInfoCdeleteButton = (Button)rootView.findViewById(R.id.faculty_info_cDelete_button);

            EditText facultyInfoCIDField = (EditText)rootView.findViewById(R.id.faculty_info_cID_field);
            EditText facultyInfoCnameField = (EditText)rootView.findViewById(R.id.faculty_info_cname_field);

            if (facultyInfoCIDField.getText().toString() != null && facultyInfoCnameField.getText().toString() != null){
                facultyInfoCaddButton.setEnabled(true);
                facultyInfoCdeleteButton.setEnabled(true);
            }
            else{
                facultyInfoCaddButton.setEnabled(false);
                facultyInfoCdeleteButton.setEnabled(false);
            }

            facultyInfoCaddButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });

            facultyInfoCdeleteButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
// this is the part where I dynamically try  to add views
            LinearLayout facultyInfoLayout = (LinearLayout)rootView.findViewById(R.id.faculty_info_layout);
            int numberOfCourses = super.getArguments().getInt(SignupActivity.courses);// no. of courses returned from database
            TextView coursesTextView[] = new TextView[numberOfCourses];
            for (int i = 0; i < numberOfCourses; i++){
                coursesTextView[i] = new TextView(getContext());
                coursesTextView[i].setText("CSE1212 RDBMS"); //enter the course ID and course name returned from database
                coursesTextView[i].setLayoutParams(new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                facultyInfoLayout.addView(coursesTextView[i]);
            }

            return;
        }

        public void facultyExamFragment(View rootView){

            Button uploadExamScheduleButton = (Button)rootView.findViewById(R.id.faculty_exam_upload_button);
            uploadExamScheduleButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
                    intent.setType("*/*");
                    startActivityForResult(intent,PICKFILE_RESULT_CODE);
                }
            });
        }

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {

            switch(requestCode){
                case PICKFILE_RESULT_CODE:
                    if(resultCode==RESULT_OK){

                        FilePath = data.getData().getPath();
                        FilePath=FilePath.split(":")[1];
                        u=new UploadFileAsync(FilePath);
                        u.execute("");
                    }
                    break;
            }
        }

        public static void facultyEventsFragment(View rootView){

        }
    }

【问题讨论】:

  • 您是否检查过numberOfCourses 返回的数字是否有效。表示是否为零。
  • @Dharmishtha 是的...实际上我已经硬编码了这个数字以进行测试,因为那时我还没有建立我的服务器连接

标签: android dynamic-view


【解决方案1】:

为要添加文本视图的父线性布局设置方向。

LinearLayout facultyInfoLayout = (LinearLayout)rootView.findViewById(R.id.faculty_info_layout);
facultyInfoLayout.setOrientation(LinearLayout.VERTICAL);

【讨论】:

  • 方向已经在布局文件中设置,我也按照你说的在java代码中添加了它,但仍然没有工作。
【解决方案2】:

删除LinearLayoutCompact,而只在布局参数中使用LinearLayout

coursesTextView[i].setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

【讨论】:

    【解决方案3】:

    这是我的 xml 部分

    <LinearLayout
              android:layout_width="match_parent"
              android:id="@+id/parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">
    

    这是我的java部分

    LinearLayout parent = (LinearLayout) findViewById(R.id.parent);
            TextView coursesTextView[] = new TextView[5];
            for (int i = 0; i < 5; i++){
              coursesTextView[i] = new TextView(this);
              coursesTextView[i].setText("CSE1212 RDBMS"); //enter the course ID and course name returned from database
              coursesTextView[i].setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
              parent.addView(coursesTextView[i]);
            }
    

    这会一个接一个地添加 5 个文本视图

    【讨论】:

      【解决方案4】:

      改变这一行

        coursesTextView[i].setLayoutParams(new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
      

      到 .可能是这个问题。并且还设置了文本颜色,可能是相同的颜色不显示文本视图。

        tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        tv.setTextColor(mHostActivity.getResources().getColor(R.color.orange));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-07
        相关资源
        最近更新 更多