-1

I want to dynamically add text views in my app depending on the information I receive from the database. I am taking a reference to the linear layout and then instantiating text views in a loop, setting LayoutParams to them and then adding them to the the layout all this is done inside a static inner fragment class. I have gone through almost every solution and I am doing the same thing as others did but when I run my app on device I don't see the dynamically added views. Please help me solve this issue. Here is a copy of my code:

 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){

        }
    }
sameer soin
  • 55
  • 1
  • 8

4 Answers4

0

Set orientation for your parent Linearlayout to which you are adding your textviews.

LinearLayout facultyInfoLayout = (LinearLayout)rootView.findViewById(R.id.faculty_info_layout);
facultyInfoLayout.setOrientation(LinearLayout.VERTICAL);
Yyy
  • 2,285
  • 16
  • 29
  • orientation is already set in the layout file and I added it in the the java code also as you said but still not working. – sameer soin Jun 20 '17 at 06:30
0

Remove LinearLayoutCompact instead use only LinearLayout in layout params

coursesTextView[i].setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
VinayagaSundar
  • 1,673
  • 17
  • 18
0

This is my xml part

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

This is my java part

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]);
        }

And this adds 5 textviews one after the other

Arun Shankar
  • 2,295
  • 16
  • 20
0

change this line

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

to . May be that is problem.and also set text color , may be same color not display textview.

  tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
  tv.setTextColor(mHostActivity.getResources().getColor(R.color.orange));
Dharmishtha
  • 1,313
  • 10
  • 21