0

I created an app composed of listview of teachers wherein the data is in sqlite. When i click one of the listview, the profile of teacher will appear and there's a list of students inside of it. BTW the profile of teacher has a scroll since it has many info. I apply the top answer here so the listview will automatically extends depends on the number of students of the teacher. But i got error when i apply this. I'll post the error and my code below.

    public class MainActivity extends AppCompatActivity {

        Button btnback, btnnext, btnbackprofile;
        TextView profileteacher;


        List<TeacherModel> GetAllTeacher;
        List<StudentModel> GetTeacherStudent;
        Context context = this;
        DatabaseHelper dbhelper;
        DatabaseHelper db = new DatabaseHelper(this);
        ListView lv,lv2;
        View TeacherListView,TeacherProfileView;

        int index = 0;
        private int currentPageIndex = 0;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            dbhelper = new DatabaseHelper(MainActivity.this);

            try{
                dbhelper.createDataBase();
            }
            catch(IOException e){
                e.printStackTrace();
            }
            try {
                dbhelper.openDataBase();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //Till here
            GetAllTeacher = dbhelper.getAllTeacher(index);
            lv = (ListView) findViewById(R.id.teacher_list);
            lv.setAdapter(new ViewAdapter());

            /****************************************************************************************
             *                                  TEACHER PROFILE
             ****************************************************************************************/
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view,final int i, long l) {

                    GetTeacherStudent = dbhelper.getTeacherStudent(GetAllTeacher.get(i).getid());
                    TeacherListView = findViewById(R.id.teacherlayout);
                    ViewGroup parent = (ViewGroup) TeacherListView.getParent();
                    parent.removeView(TeacherListView);
                    // inflate your profile view (or get the reference to it if it's already inflated)
                    TeacherProfileView = getLayoutInflater().inflate(R.layout.profile_teacher, parent, false);
                    // add it to the parent
                    parent.addView(TeacherProfileView);

                    ListView listView = (ListView)view.findViewById(R.id.profileStudentList);
                    setListViewHeightBasedOnChildren(listView);
                    listView.setAdapter(new ViewAdapter2());



                    btnbackprofile = (Button) findViewById(R.id.profileTeacherBack);

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

                            if (TeacherProfileView != null && TeacherProfileView.getParent() != null) {
                                // remove your profile view
                                ViewGroup parent = (ViewGroup) TeacherProfileView.getParent();
                                parent.removeView(TeacherProfileView);

                                // a reference to yourListView has to be saved somewhere; just get it

                                // add your listview to the parent
                                parent.addView(TeacherListView);
                            } else {
                            }


                        }
                    });

                    profileteacher = (TextView) findViewById(R.id.profileTeacherName);
                    profileteacher.setText(GetAllTeacher.get(i).getname());

                }
            });

            btnback = (Button) findViewById(R.id.teacherBack);
            btnnext = (Button) findViewById(R.id.teacherNext);

            btnback.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View convertView) {

                    currentPageIndex -= 20;
                    GetAllTeacher = dbhelper.getAllTeacher(currentPageIndex);
                    lv = (ListView) findViewById(R.id.teacher_list);
                    lv.setAdapter(new ViewAdapter());

                }

            });

            btnnext.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View convertView) {

                    currentPageIndex += 20;
                    GetAllTeacher = dbhelper.getAllTeacher(currentPageIndex);
                    lv = (ListView) findViewById(R.id.teacher_list);
                    lv.setAdapter(new ViewAdapter());

                }
            });



        }

/****************************************************************************************
         *                                      CODE IN THE LINK THAT I APPLY
         ****************************************************************************************/
        public static void setListViewHeightBasedOnChildren(ListView listView) {
            ListAdapter listAdapter = listView.getAdapter();
            if (listAdapter == null)
                return;

            int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
            int totalHeight = 0;
            View view = null;
            for (int i = 0; i < listAdapter.getCount(); i++) {
                view = listAdapter.getView(i, view, listView);
                if (i == 0)
                    view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, AbsListView.LayoutParams.WRAP_CONTENT));

                view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
                totalHeight += view.getMeasuredHeight();
            }
            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
            listView.setLayoutParams(params);
        }


        /****************************************************************************************
         *                                      CUSTOM LIST
         ****************************************************************************************/
        public class ViewAdapter extends BaseAdapter {

            LayoutInflater mInflater;

            public ViewAdapter() {
                mInflater = LayoutInflater.from(context);
            }

            @Override
            public int getCount() {
                return GetAllTeacher.size();
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {

                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.item_teacher,null);
                }

                final TextView names = (TextView) convertView.findViewById(R.id.teacherlist_name);
                final TextView gender = (TextView) convertView.findViewById(R.id.teacherlist_gender);

                names.setText("Dr. "+GetAllTeacher.get(position).getname());
                gender.setText(GetAllTeacher.get(position).getgender());

                return convertView;
            }
        }

        public class ViewAdapter2 extends BaseAdapter {

            LayoutInflater mInflater;

            public ViewAdapter2() {
                mInflater = LayoutInflater.from(context);
            }

            @Override
            public int getCount() {
                return GetTeacherStudent.size();
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {

                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.item_teacherstudent,null);
                }

                final TextView names = (TextView) convertView.findViewById(R.id.teacherlist_name);

                names.setText(GetTeacherStudent.get(position).getstudent());

                return convertView;
            }
        }
    }

Error

Attempt to invoke virtual method 'android.widget.ListAdapter android.widget.ListView.getAdapter()' on a null object reference at com.example.jathniel.studentlist.MainActivity.setListViewHeightBasedOnChildren(MainActivity.java:243) at com.example.jathniel.studentlist.MainActivity$1.onItemClick(MainActivity.java:100)

Line 243: ListAdapter listAdapter = listView.getAdapter();

Line 100: setListViewHeightBasedOnChildren(listView);

Community
  • 1
  • 1
APCM
  • 1,704
  • 1
  • 11
  • 24

2 Answers2

2

why you are entering the list view in setListViewHeightBasedOnChildren change it into like this setListViewHeightBasedOnChildren() and declare this in the oncreate() ListView listView = (ListView)view.findViewById(R.id.profileStudentList); then try edit

in you method change it to like this :

 public static void setListViewHeightBasedOnChildren() {
            ListAdapter listAdapter = listView.getAdapter();
            if (listAdapter == null)
                return;

and on oncreate() method

protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            dbhelper = new DatabaseHelper(MainActivity.this);

         listView = (ListView)findViewById(R.id.profileStudentList);
Moudiz
  • 7,211
  • 22
  • 78
  • 156
  • based from what i understand in what you said. correct me if im wrong i insert this line -> ListView listView = (ListView)findViewById(R.id.profileHospitalList); inside the private void setListViewHeightBasedOnChildren() { – APCM Nov 10 '15 at 06:12
  • error in this line ListAdapter listAdapter = listView.getAdapter(); and setListViewHeightBasedOnChildren(); – APCM Nov 10 '15 at 06:24
  • declare `listview` add above at begning of your code like this `ListView lv,lv2,listview;` if error pressist tell me what it is – Moudiz Nov 10 '15 at 06:29
  • i got error in this line ->listView.setAdapter(new ViewAdapter2()); it says Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference – APCM Nov 10 '15 at 06:33
  • where does it exists in your code ? I couldnt find it – Moudiz Nov 10 '15 at 06:36
  • i solved it already thanks! :) i just remove the view. in ListView – APCM Nov 10 '15 at 06:44
  • 1
    yes that what I mentioned in my answer , the list view should be declared in Oncreate and I removed the vew from it – Moudiz Nov 10 '15 at 06:47
0

Interchange your line 100 and 101. As line 101 setting adapter to listView.

And line 100 try to get adapter from listView.

But at the time of line 100 there will be no adapter set to list view. So it will throw null pointer exception.

So code will be like this

listView.setAdapter(new ViewAdapter2());
setListViewHeightBasedOnChildren(listView);
shreyash mashru
  • 301
  • 2
  • 9
  • you did only is exchange the placing of line 100 and 101 right? – APCM Nov 10 '15 at 06:10
  • Yes i have suggested to exchange this line as you were trying to get adapter before set it. – shreyash mashru Nov 10 '15 at 06:14
  • this is the error i got -> Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at com.example.jathniel.studentlist.MainActivity$1.onItemClick(MainActivity.java:100) – APCM Nov 10 '15 at 06:16
  • this is my line 100: listView.setAdapter(new ViewAdapter2()); since i exchange the placement of 100 and 101 – APCM Nov 10 '15 at 06:19
  • Well that means listView is null. This line is not working. You cannot find this Resource (listview) from this view. ListView listView = (ListView)view.findViewById(R.id.profileStudentList); – shreyash mashru Nov 10 '15 at 06:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94675/discussion-between-shreyash-mashru-and-gem-ubaldo). – shreyash mashru Nov 10 '15 at 06:21