ListView和自定义适配器

时间:2017-03-20 20:09:48

标签: android listview

在main中使用putDate方法,我在dbManager类中有一个名为updateStudentDate的方法,它获取id和date,当我点击save按钮时,我从数据库获得的ID和当前日期被发送到updateStudentDate。 但是会发生错误。

这是主要活动。

public class StartAttendance extends AppCompatActivity implements View.OnClickListener{
    private DBManager dbManager;
    private List<UserModel>  students;
    private String studentSubjectId;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start_attendance);

        final Button save = (Button) findViewById(R.id.save);
        final Button cancel = (Button) findViewById(R.id.cancel);
        final ListView listOfTakeAtt = (ListView) findViewById(R.id.listOfTakingAttendance);


        Intent intent = getIntent();
         studentSubjectId = intent.getStringExtra("studentSubjectId");


        dbManager = new DBManager(StartAttendance.this);
        dbManager.open();
        ArrayList arrayList2 = dbManager.getAllStudentsName(Integer.valueOf(studentSubjectId));//Get the names from database.


        students = new ArrayList<>();

        setData(arrayList2);


        final CustomLayoutOfTakingAttendance adapter = new CustomLayoutOfTakingAttendance(this, students);
        listOfTakeAtt.setAdapter(adapter);

        listOfTakeAtt.setDividerHeight(17);


        cancel.setOnClickListener(this);
        save.setOnClickListener(this);


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

                UserModel model = students.get(i);

                int y=model.isSelected?1:0;

                model.setSelected(true);

                students.set(i, model);


                if(y==1){
                    model.setSelected(false);
                 }


                adapter.updateRecords(students);
            }
        });
    }

    public void setData(ArrayList arrayList2){
        for(int i=0;i<arrayList2.size();i++){
            students.add(new UserModel(false, (String) arrayList2.get(i)));
        }
    }


    @Override
    public void onClick(View v){
            switch (v.getId()){
                //When save button is clicked i want to see present radioButton is checked or not for each item.
                //if checked save current date into database for this id.
        case R.id.save:
           putDate();
            break;

        case R.id.cancel:
                Intent i=new Intent(StartAttendance.this,ContentOfEachSubject.class);
                i.putExtra("studentSubjectId",studentSubjectId);
                startActivity(i);
                break;
        }
}

    public void putDate(){
        SimpleDateFormat DateFormat=new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
        Date d=new Date();
        String date=DateFormat.format(d);

        boolean isUpdated=false;
        UserModel model;
            ArrayList arrayList1 = dbManager.getAllStudentsId(Integer.valueOf(studentSubjectId));//Get the ids from the database.
            for(int i=0;i<students.size();i++)
            {
                 model = students.get(i);

                if(model.isSelected){
                    dbManager = new DBManager(StartAttendance.this);
                    dbManager.open();
                    isUpdated = dbManager.UpdateStudentDate((Integer) arrayList1.get(i), date);
                }
            }

        if(isUpdated){Toast.makeText(StartAttendance.this,"You took attendance successfully..",Toast.LENGTH_SHORT);}

        else {Toast.makeText(StartAttendance.this,"Fail while getting attendance!!",Toast.LENGTH_SHORT);}
    }
}

UserModel类

public class UserModel {

    boolean isSelected;
    String name;


    public UserModel(boolean isSelected,String name) {
        this.isSelected = isSelected;
        this.name=name;
    }

    public boolean isSelected() {
        return isSelected;
    }

    public void setSelected(boolean selected) {
        isSelected = selected;
    }



    public String getStudentName() {
        return name;
    }

    public void setStudentName(String name) {
        this.name = name;
    }

}

dbManager类

public boolean UpdateStudentDate(int id,String date){
        ContentValues cv=new ContentValues();
        cv.put(DatabaseHelper.KEY_STUDENT_DATE,date);
       long result=database.update(DatabaseHelper.STUDENT_TABLE,cv,DatabaseHelper.KEY_STUDENT_ID+" = "+id,null);
        databaseHelper.close();
        if(result==-1){return false;}

        else return true;
    }

1 个答案:

答案 0 :(得分:0)

getAllStudentsId(int id)方法的实施出错。 如果您的ID假设为Integer类型,我建议您更改方法的签名,而不是ArrayList,而是ArrayList<Integer>。返回一些通用类型的列表会更好,例如List<Integer> is。

但是,主要问题是,在此方法中,您要添加到集合String实例,而不是Integer s。

arrayList.add(cursor.getString(cursor.getColumnIndex(Databas‌​eHelper.KEY_STUDENT_‌​ID)));

我不知道如何在数据库中保留数据,无论您的ID是数字格式还是varchar格式,但我想将cursor.getString(...)更改为cursor.getInt(...)

应该足够了

然后,此方法的实现可能类似于

public List<Integer> getAllStudentsId(int id){ 
    List<Integer> result =new ArrayList<>(); 
    String[] columns = new String[]{DatabaseHelper.KEY_STUDENT_ID}; 
    Cursor cursor = database.query(DatabaseHelper.STUDENT_TABLE, columns, D‌​atabaseHelper.KEY_ST‌​UDENT_SUBJECT_ID + " = " + id, null, null,null,null); 

    while(cursor.moveToNext()){ 
        result.add(cursor.getInt(cursor.getColumnIndex(Databas‌​eHelper.KEY_STUDENT_‌​ID))); 
    } 
    return result; 
}

另外,不要忘记在StartAttendance.java课程中更改列表类型。

List<Integer> arrayList1 = dbManager.getAllStudentsId(Integer.valueOf(studentSubjectId));//Get the ids from the database.