java - Android:对 arrayList 中的名称列表进行排序并将它们链接到适当的 ID

标签 java android arraylist

我的 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小时内完成的项目)

我的 StudentList 类中有以下内容:

    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。

实现这一目标的最佳方法是什么?

谢谢

最佳答案

  1. 从 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,其中学生 ID 作为键,学生姓名作为值。

  2. 现在您的代码在 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); 
           } 
         }
        }
     });
    

关于java - Android:对 arrayList 中的名称列表进行排序并将它们链接到适当的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30467625/

相关文章:

java - 为什么 DataSourceTransactionManage 不回滚而 HibernateTransactionManager 回滚?

java - Spring MVC 在一个请求中返回多个对象

java - 什么是 Kotlin 函数类型的 Java 等价物?

java - 有没有办法实现以下 JSON pojo 类以及如何将数据传递到列表?

java - 更新 ArrayList

java - 如何使用java检查24H是否已经过去?

android - 如何在完成应用程序之前完成处理程序?

java - PushApps 注册和注销菜单按钮

android - Android 中的 Facebook json 解析

java - 使用Bitmap ArrayList动态填充ListView