Android:选择ListView项目并获取对象

时间:2017-05-29 10:39:22

标签: android listview

我做了一个自定义listView,有两个textViews ..例如学生姓名和滚动没有... 现在我想实现一个选择...例如..我将点击列表视图中的几个学生,对象应该保存在某个地方...例如:

        ClassList.add(new StudentList(99,"student1","code"));
        ClassList.add(new StudentList(70,"student2","code"));
        ClassList.add(new StudentList(20,"student3","code"));
        ClassList.add(new StudentList(30,"student4","code"));

        adapter = new StudentListViewAdapter(this, ClassList,"code");
        listView.setAdapter(adapter);

现在我应该能够设置另一种方法,例如:

ClassList2=listView.getSelectedStudents();

这应该返回选定的学生..例如,如果我选择学生1,学生2它应该返回他们两个的学生列表对象,所以我可以访问那里滚动和代码 还有另一个问题......当我取消选择它时应该从列表中删除该对象.. 如果有人会编写示例代码

,那将会非常有帮助

我就是这样做的。但我不认为这是一个很好的方法

LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (ItemView == null) {
        ItemView = inflater.inflate(R.layout.customvistview_atd, parent, false);
    }

    //find the hotels to work with
    final studentlist currentList = totalClass_list.get(position);

    //fill the view
    final TextView textview_studentName = (TextView) ItemView.findViewById(R.id.textview_atd_studentName);
    textview_studentName.setText((currentList.getName()));

    final TextView textview_studentRollno = (TextView) ItemView.findViewById(R.id.textview_atd_studentRollno);
    textview_studentRollno.setText((currentList.getRoll_no() + ""));

    final CardView cardView = (CardView) ItemView.findViewById(R.id.card_view_atd);
    cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {



            for (i = 0; i < presentStudents.size(); i++) {
                if (presentStudents.get(i).getMindex() == position) {
                    System.out.println("m-index : "+presentStudents.get(i).getMindex());
                    System.out.println("position : "+position);
                    isChecked = true;
                    System.out.println("I am in the loop, I am at position: "+i);
                    me = i;
                    break;
                }
            }


            if (isChecked) {
                presentStudents.remove(me);
                isChecked = false;
                ColorDrawable[] color = {new ColorDrawable(Color.parseColor("#198b9f")), new ColorDrawable(Color.WHITE)};
                TransitionDrawable trans = new TransitionDrawable(color);
                cardView.setBackground(trans);
                trans.startTransition(1500);

                textview_studentName.setTextColor(Color.GRAY);
                textview_studentName.setTextColor(Color.GRAY);

                //cardView.setBackgroundColor(Color.WHITE);
                System.out.println("I am in isChecked");
            } else {
                presentStudents.add(new tempclass(currentList.getRoll_no(),atdCode,position));


                ColorDrawable[] colorCard = {new ColorDrawable(Color.WHITE), new ColorDrawable(Color.parseColor("#198b9f"))};
                TransitionDrawable trans = new TransitionDrawable(colorCard);
                cardView.setBackground(trans);
                trans.startTransition(1500);

                textview_studentName.setTextColor(Color.WHITE);
                textview_studentName.setTextColor(Color.WHITE);
             //   cardView.setBackgroundColor(Color.BLUE);
                System.out.println("I am in else, making it blue");
            }

        }
    });

1 个答案:

答案 0 :(得分:0)

你可以使用这个

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            StudentLists studentList = listView.getItemAtPosition(i);//selected object   
            adapter.dismiss(); // If you want to close the adapter
        }
    });
相关问题