如何通过单击android中listview的第一项来更改列表视图的项目

时间:2016-04-12 06:53:25

标签: android listview

在这里,我正在我想要的学校出勤工作(当我点击列表视图的第一项时,它将标记所有缺席或全部存在)我已经使用listview之外的按钮做了这个但我想要它与listview的第一项没有按钮。

public class StudentsAttendanceActivity extends AppCompatActivity implements AsyncResponse, SwipeRefreshLayout.OnRefreshListener {


    public StudentsAttendanceAdapter mAttendanceAdapter;
    public ArrayList<StudentsAttendenceModel> mListInfo = new ArrayList<>();
    int index, mTop,mEmployeeId;
    View mRootView;

    public int classId;
    //private SwipeRefreshLayout swipeLayout;
    private ListView mListView;
    String className;
    String classIdNo;

    @Override
    protected  void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.students_attendance_layout);

        //mRootView = inflater.inflate(R.layout.students_attendance_layout, false);
        //swipeLayout=(SwipeRefreshLayout) mRootView.findViewById(R.id.swipe_container);
        mListView = (ListView) findViewById(R.id.studentsList);
        final Button allStudentSelectionButton = (Button) findViewById(R.id.allStudentSelectionButton);
        allStudentSelectionButton.setTag("Present");
        //ListView mEmpty = (ListView) mRootView.findViewById(R.id.studentsList);

        StudentsAttendenceModel studentsAttendenceModel1=new StudentsAttendenceModel();
        studentsAttendenceModel1.setStudentId("001");
        studentsAttendenceModel1.setStudentRollNumber("1");
        studentsAttendenceModel1.setStudentName("Anurag Thakur");
        studentsAttendenceModel1.setStudentAttendenceStatus("Present");
        studentsAttendenceModel1.setClassName("5th A");
        mListInfo.add(studentsAttendenceModel1);

        StudentsAttendenceModel studentsAttendenceModel2=new StudentsAttendenceModel();
        studentsAttendenceModel2.setStudentId("002");
        studentsAttendenceModel2.setStudentRollNumber("2");
        studentsAttendenceModel2.setStudentName("Funsukh Wangadu");
        studentsAttendenceModel2.setStudentAttendenceStatus("Present");
        studentsAttendenceModel2.setClassName("5th A");
        mListInfo.add(studentsAttendenceModel2);

        StudentsAttendenceModel studentsAttendenceModel3=new StudentsAttendenceModel();
        studentsAttendenceModel3.setStudentId("003");
        studentsAttendenceModel3.setStudentRollNumber("3");
        studentsAttendenceModel3.setStudentName("Chatur Lingam");
        studentsAttendenceModel3.setStudentAttendenceStatus("Present");
        studentsAttendenceModel3.setClassName("5th A");
        mListInfo.add(studentsAttendenceModel3);

        StudentsAttendenceModel studentsAttendenceModel4=new StudentsAttendenceModel();
        studentsAttendenceModel4.setStudentId("004");
        studentsAttendenceModel4.setStudentRollNumber("4");
        studentsAttendenceModel4.setStudentName("Virusahastra Budhi");
        studentsAttendenceModel4.setStudentAttendenceStatus("Present");
        studentsAttendenceModel4.setClassName("5th A");
        mListInfo.add(studentsAttendenceModel4);

        mAttendanceAdapter = new StudentsAttendanceAdapter(mListInfo,this);
        mListView.setAdapter(mAttendanceAdapter);
        mListView.setSelectionFromTop(index, mTop);
        //mListView.setEmptyView((TextView) mRootView.findViewById(R.id.studentName));

        /*mAdapter = new ClassStudentsRecyclerAdapter(results,getContext());
        mRecyclerView.setAdapter(mAdapter);*/

        //swipeLayout.setOnRefreshListener(this);
        SharedPreferenceSingleton.getInstance().init(getApplicationContext());
        className = SharedPreferenceSingleton.getInstance().getStringPreference(TeacherPreference.CLASS_NAME);

        SharedPreferenceSingleton.getInstance().init(getApplicationContext());
        classIdNo  =   SharedPreferenceSingleton.getInstance().getStringPreference(SharedContracts.CLASS_LIST_INFO + className);


        allStudentSelectionButton.setOnClickListener(new OnClickListener()
        {
            @Override

            public void onClick(View v)
            {
                String status=(String) v.getTag();
                if (status=="Present")
                {
                    allStudentSelectionButton.setText("Make All Late");
                    allStudentSelectionButton.setTag("Absent");

                    for (int i=0;i<mListInfo.size();i++)
                    {
                     mListInfo.get(i).setStudentAttendenceStatus("Absent");
                    }
                }
                else if (status=="Absent")
                {
                    allStudentSelectionButton.setText("Make All Present");
                    allStudentSelectionButton.setTag("Late");

                    for (int i=0;i<mListInfo.size();i++)
                    {
                        mListInfo.get(i).setStudentAttendenceStatus("Late");
                        // ImageView attendanceStatus=(ImageView) mRootView.findViewById(R.id.attendenceStatusImage);
                    }
                }
                else if(status=="Late")
                {
                    allStudentSelectionButton.setText("Make All Absent");
                    allStudentSelectionButton.setTag("Present");

                    for (int i=0;i<mListInfo.size();i++)
                    {
                        mListInfo.get(i).setStudentAttendenceStatus("Present");
                        // ImageView attendanceStatus=(ImageView) mRootView.findViewById(R.id.attendenceStatusImage);
                    }
                }
                mAttendanceAdapter.notifyDataSetChanged();
            }
        });
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                String status=mListInfo.get(position).getStudentAttendenceStatus();
                if (status=="Present")
                {
                    mListInfo.get(position).setStudentAttendenceStatus("Absent");

                }
                else if (status=="Absent")
                {
                    mListInfo.get(position).setStudentAttendenceStatus("Late");
                }
                else if(status=="Late")
                {
                        mListInfo.get(position).setStudentAttendenceStatus("Present");
                }
                mAttendanceAdapter.notifyDataSetChanged();
            }
        });
    }

1 个答案:

答案 0 :(得分:0)

在onItemClick方法中,如果position == 0,则将实现更改为标记Absent或Present。