我的dbHelper类中有以下SQL查询:
public ArrayList getAllStudents()
{
ArrayList array_list = new ArrayList();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from "+ TABLE_STUDENTS, null );
res.moveToFirst();
while(res.isAfterLast() == false)
{
array_list.add(res.getString(res.getColumnIndex(ST_SURNAME)) + " , " + res.getString(res.getColumnIndex(ST_FIRST_NAME))); //+ ", " +ST_FIRST_NAME )));
res.moveToNext();
}
return array_list;
}
public ArrayList getAllStudentIds()
{
ArrayList array_list = new ArrayList();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from "+ TABLE_STUDENTS, null );
res.moveToFirst();
while(res.isAfterLast() == false)
{
array_list.add(res.getString(res.getColumnIndex(ST_ID)));
res.moveToNext();
}
return array_list;
}
一种是返回学生姓名以显示在listView中,另一种是访问并返回该学生的ID,这样当用户单击链接时,它会将他们引导到正确的学生档案。
我知道最好的方法是使用自定义适配器,但我无法让它工作,也没有时间对代码进行彻底检查(正如我在30小时内对这个项目进行的演示(
我的学生列表课上有以下内容:
ArrayList array_list = mydb.getAllStudents();
Log.d("array size:,", String.valueOf(array_list.size()));
final ArrayList id_list = mydb.getAllStudentIds();
ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.list_layout, array_list);
//Adding the contacts to the list view.
student = (ListView) findViewById(R.id.listView1);
student.setAdapter(arrayAdapter);
//Setting an onClickListener to process the user's actions
student.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
int id_To_Search = Integer.valueOf((String)id_list.get(arg2));
Bundle dataBundle = new Bundle();
dataBundle.putInt("studentId", id_To_Search);
Intent intent = new Intent(getApplicationContext(), com.example.ltss.dyslexia.app.StudentProfilePage.class);
intent.putExtras(dataBundle);
startActivity(intent);
}
});
我试图使用对列表进行排序
Collections.sort(array_list);
它做的正是我需要它做的——按字母顺序返回学生姓名
但我需要一种方法来链接附在学生档案上的ID。
实现这一目标的最佳方式是什么?
感谢
-
从dbHelper类中删除
public ArrayList getAllStudentIds()
方法(不需要(并更改类似public ArrayList getAllStudents()
的public HashMap<String, String> getAllStudents() { Map<String, String> mapStudent = new HashMap<String, String>(); SQLiteDatabase db = this.getReadableDatabase(); Cursor res = db.rawQuery( "select * from "+ TABLE_STUDENTS, null ); res.moveToFirst(); while(res.isAfterLast() == false) { mapStudent.put(res.getString(res.getColumnIndex(ST_ID)) ,res.getString(res.getColumnIndex(ST_SURNAME)) + " , " + res.getString(res.getColumnIndex(ST_FIRST_NAME))); //+ ", " +ST_FIRST_NAME ))); res.moveToNext(); } return mapStudent; }
所以现在方法将返回一个HashMap,其中Student Id为Key,Student name为Value
-
现在,
StudentList
类中的代码看起来像HashMap<String, String> studentMap = mydb.getAllStudents(); // Converting HashMap values into ArrayList List<String> array_list = new ArrayList<String>(studentMap.values()); Log.d("array size:,", String.valueOf(array_list.size())); ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.list_layout, array_list); //Adding the contacts to the list view. student = (ListView) findViewById(R.id.listView1); student.setAdapter(arrayAdapter); //Setting an onClickListener to process the user's actions student.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Log.d("arg2 ", String.valueOf(arg2)); TextView tempTextView = (TextView) arg1; String data = tempTetxtView.getText(); for (Entry<String, String> entry : studentNameAndId.entrySet()) { if (entry.getValue().equals(data)) { int id_To_Search = Integer.valueOf(entry.getKey()); //id_list.get(arg2+1); Bundle dataBundle = new Bundle(); dataBundle.putInt("studentId", id_To_Search); //Create a new intent to open the DisplayContact activity Intent intent = new Intent(getApplicationContext(), com.example.ltss.dyslexia.app.StudentProfilePage.class); intent.putExtras(dataBundle); startActivity(intent); } } } });
在对名称进行排序之前,将这两个列表循环在一起,并将名称作为关键字放入HashMap
中,id作为值。然后您可以从名称中找到ID。按照相同的列对两个SQL语句进行排序,以确保这两个列表以相同的顺序表示学生。
当然,如果你有两个同名的学生,你将无法区分。如果真的有一个方法连接到数据库(因为您已经执行了SELECT *
(,同时返回ID和名称会更好。我鼓励你这样做,你的代码会更干净,没有bug。
如果只需要对数据进行一次排序(或每次数据库访问一次(,请使用数据库对数据进行排序。像SELECT * FROM students s ORDER BY s.student_last_name
这样的东西就可以了。
但是,你也有一些其他领域需要工作。创建一个具有firstName
、lastName
和id
字段的Student
类。不需要管理多个列表,只需要一个List<Student>
,每个Student
对象都包含学生的姓名和id。
您可以让数据库为您对它们进行排序,也可以为Java排序设置Students
。将implements Comparable<Student>
添加到Student类中,并实现public int compareTo(Student s) { ... }
。如果this
在s
之前,则compareTo()
应返回负数;如果s
在this
之前,则返回正数;如果两者相同,则返回0。我会这样实现:
public int compareTo(Student s) {
int result = this.lastName.compareTo(s.lastName);
if (result == 0) {
result = this.firstName.compareTo(s.firstName);
}
if (result == 0) {
result = this.id - s.id;
}
return result;
}
这将按姓氏、名字、id进行排序(它利用了String现有的compareTo(String)
实现来节省大量工作(。