如何在选择微调器后从我的数据库中填充ListView?

时间:2014-12-09 00:27:01

标签: android sqlite listview android-spinner

我正在尝试在选择我的两个微调器之后从我的SQLite数据库填充列表视图。 活动包括:2个微调器,3个文本视图,1个编辑文本 我试图在选择后显示从数据库中检索的数据。

例如,从第一个微调器中选择第一个项目后,将是" bachelor",以及第二个微调器中的第一个项目,这将是" Business Computer",选中后,将从loginDataBaseAdapter调用 BusinessCourses 方法

我的LoginDataBaseAdapter中的getCourses方法: 被修改

   public List<BusinessCourses> getCourses(String degreeCode, String majorCode){
           List<BusinessCourses> busCoursesList = new ArrayList<BusinessCourses>();
           String MY_QUERY = "SELECT course.courseID, course.courseName, "
                + "course.credits FROM course INNER JOIN curriculum ON "
                + "course.courseID=curriculum.courseID WHERE curriculum.majorCode=? and"
                + " curriculum.degCode=?";
           SQLiteDatabase db = dbHelper.getWritableDatabase();
           Cursor cursor = db.rawQuery(MY_QUERY, new String[]{String.valueOf(majorCode), 
                   String.valueOf(degreeCode)});
           if(cursor.moveToFirst()){
               do{
                   BusinessCourses busCourses = new BusinessCourses();
                   busCourses.setCredits(cursor.getString(0));
                   busCourses.setCourseCode(cursor.getString(1));
                   busCourses.setCourseTitle(cursor.getString(2));

                   busCoursesList.add(busCourses);
               } while(cursor.moveToNext());
           }

           return busCoursesList;
       }

微调代码 被修改

 final Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
        this, R.array.degree_array,
        android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setPrompt("Choose a degree");
spinner1.setOnItemSelectedListener(this);

spinner1.setAdapter(new NothingSelectedSpinnerAdapter(adapter1,
        R.layout.choose_degree, this));

spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,
            int position, long id) {

        if (spinner1.getSelectedItemPosition() == 0 ){
             degreeCode = "L";}
        if (spinner1.getSelectedItemPosition() == 1 ){
             degreeCode = "M1";}
        if (spinner1.getSelectedItemPosition() == 2 ){
             degreeCode = "M2";}
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {

        // TODO Auto-generated method stub

    }
});

final Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);

ArrayAdapter<CharSequence> adapter2 = ArrayAdapter
        .createFromResource(this, R.array.major_array,
                android.R.layout.simple_spinner_item);

adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setPrompt("Choose a major");

spinner2.setAdapter(new NothingSelectedSpinnerAdapter(adapter2,
        R.layout.choose_major, this));

spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,
            int arg2, long arg3) {
        // TODO Auto-generated method stub

        if (spinner2.getSelectedItemPosition() == 0 ){
             majorCode = "MIS";}
        if (spinner2.getSelectedItemPosition() == 1 ){
             majorCode = "Acc";}
        if (spinner2.getSelectedItemPosition() == 2 ) {
             majorCode = "Mar";}
        if (spinner2.getSelectedItemPosition() == 3 ){
             majorCode = "Man";}
        if (spinner2.getSelectedItemPosition() == 4 ){
             majorCode = "B&F";}
        if (spinner2.getSelectedItemPosition() == 5 ){
             majorCode = "MCB";}



    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }

});

ListView list = (ListView)findViewById(R.id.BBACList);
LoginDataBaseAdapter db = new LoginDataBaseAdapter(this);
String[] columns = new String[]{businessCourses.CourseCode, businessCourses.CourseTitle, businessCourses.Credits};
int[] viewIDs = new int[]{R.id.courseTitle, R.id.courseCode, R.id.credits};

Cursor cursor = (Cursor) db.getCourses(degreeCode, majorCode);
SimpleCursorAdapter adapter;
adapter = new SimpleCursorAdapter(getBaseContext(), R.layout.list_item, cursor, columns, viewIDs, 0);
list.setAdapter(adapter);

1 个答案:

答案 0 :(得分:-1)

检查此链接!愿它帮助你!

Android Populating Spinner data from MySQL Database Source:AndroidHive

尝试在Google First上提出您的问题!再做一次,Atleast检查8-10网站然后发布命令!

干杯 -Aman