2017-03-20 8 views
0

putDateメソッドをmainに使用すると、IDと日付を取得するdbManagerクラスのupdateStudentDateというメソッドがあります。保存ボタンをクリックすると、データベースから取得したIDと現在の日付がupdateStudentDateに送られます。 ですが、エラーが発生します。ListViewとカスタムアダプタ

これは主なアクティビティです。

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

エラーが説明されている種類のログを提供できますか? – jpact

+0

Broボタンをクリックすると、アプリが自動的に停止し、エラーは表示されません。 –

+0

logcatも空ですか? (Android Montiorタブ) – jpact

答えて

0

あなたはgetAllStudentsId(int id)メソッドの実装に誤りがあります。 IDがInteger型であると思われる場合は、ArrayListではなく、ArrayList<Integer>を返すようにメソッドのシグネチャを変更することをお勧めします。 List<Integer>など、一般的なタイプのリストを返す方が良いでしょう。

ただし、主な問題は、Integerではなく、Stringのコレクションにこのメソッドを追加することです。

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

私はあなたのIDが数値またはvarchar型の形式であるかどうか、データをDBに永続化されているかわからないが、私は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. 
+0

<3 <3あなたがエラーを見つけた兄弟ありがとう、私はあなたにどうもありがとうございます。 –

+0

ちょうどupvote答えと正しい1つとしてマーク:)あなたは歓迎です – jpact

+0

残念私は今すぐ15の評判を持っていない仲間:)ありがとう。 –

関連する問題