jeudi 2 avril 2015

Android: Trying to access unique Ids with listView

I'm having a problem with my listView and getting items retrieved correctly from it. My application is an admin app that allows teachers to add lesson notes against specific students. My scenario is as follows:


When the teacher adds a new lesson note to the list it automatically gives it a unique ID


so student 1 (id 1)has lesson notes with ids: 1,3,4,5 and 6


student 2 (id 2)has lesson notes with ids : 2,7,8 and 9


Student 1 has the list view of:1,2,3,4,5


Student 2 has the list view of:1,2,3,4,5


So when student 1 accesses note 1, the query is as follows: select * from table_lessons where lessonID = 1 and StudentID = 1


Note 2: select * from table_lessons where lessonID = 2 and StudentID = 1


Note 3: select * from table_lessons where lessonID = 3 and StudentID = 1


And so on....


But you see that student 1 doesn't have the lesson note '2' - so the program crashes


I think the position of the listView is throwing the program off...


This is what is in my lessonNotesList class:



ArrayList array_list = dyslexiaDb.getAllLessonNotes(studentID);
//Creating an Array Adapter to manipulate the contacts data into a ListView
ArrayAdapter arrayAdapter = new ArrayAdapter(this,R.layout.list_layout, array_list);
//Adding the contacts to the list view.
lessonNote = (ListView)findViewById(R.id.listViewMain);
lessonNote.setAdapter(arrayAdapter);
//Setting an onClickListener to process the user's actions
lessonNote.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

//Prepare the data from the ListView item to be passed to new activity
//arg2 is increased by one as its a zero based index but the contacts table in the database works of a base index of 1

int id_To_Search = arg2+1;
Bundle extras = getIntent().getExtras();
int studentId = extras.getInt("studentId");
//Create a new intent to open the DisplayContact activity

Intent intent = new Intent(getApplicationContext(), com.example.ltss.dyslexia.app.LessonNotes.class);
intent.putExtra("lessonId", id_To_Search);
intent.putExtra("studentId", studentId);
startActivity(intent);
}
});


I read something about using a custom adapter? Is this the correct thing to do, and if so, how do I go about implementing it?


Thanks


Aucun commentaire:

Enregistrer un commentaire