Android扩展listview里面的scrollview取决于值的数量

时间:2015-11-10 05:43:01

标签: android android-studio android-listview android-scrollview

我创建了一个由教师列表视图组成的应用程序,其中数据是sqlite。当我点击其中一个列表视图时,将显示教师的个人资料,其中有一个学生列表。 BTW教师的个人资料有一个滚动,因为它有很多信息。我申请the top answer here,因此列表视图将自动延长,具体取决于教师的学生人数。但是当我申请时,我得到了错误。我将在下面发布错误和我的代码。

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

错误

     

尝试调用虚方法'android.widget.ListAdapter   android.widget.ListView.getAdapter()'在null对象引用上   com.example.jathniel.studentlist.MainActivity.setListViewHeightBasedOnChildren(MainActivity.java:243)   在   com.example.jathniel.studentlist.MainActivity $ 1.onItemClick(MainActivity.java:100)

     

第243行:ListAdapter listAdapter = listView.getAdapter();

     

第100行:setListViewHeightBasedOnChildren(listView);

2 个答案:

答案 0 :(得分:2)

为什么要在setListViewHeightBasedOnChildren中输入列表视图,将其更改为setListViewHeightBasedOnChildren(),并在oncreate() ListView listView = (ListView)view.findViewById(R.id.profileStudentList);中声明  然后试试 修改

在你的方法中将其更改为:

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

和oncreate()方法

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

         listView = (ListView)findViewById(R.id.profileStudentList);

答案 1 :(得分:0)

交换您的第100行和第101行。 作为第101行设置适配器到listView。

第100行尝试从listView获取适配器。

但是在第100行的时候,没有适配器设置为列表视图。所以它会抛出空指针异常。

所以代码就像这样

listView.setAdapter(new ViewAdapter2());
setListViewHeightBasedOnChildren(listView);