0

我正在使用微调器过滤一些东西。我有两个微调器,第一个带有州列表,第二个带有大学列表。当我的程序运行时,我在第一个微调器中选择一个州,然后我的程序在第二个微调器中过滤该州的大学。现在,当我更改第一个微调器的状态时,它不会过滤它。我想知道如何选择任何状态,然后进行相应的过滤。

微调器 1 包含;

Select a state
[NY,NJ]

微调器 2 包含;

Select a College
[NYU, NYIT, NJIT] 

我希望能够先选择NY,然后再选择NYUNYIT。然后我想选择 NJ 并查看 NYIT。

请给我一些指导。

4

3 回答 3

1

使用数据库来存储值。有一张州桌和一张大学桌。在 Colleges 表中有一个用于存储状态的列 从 States 表中加载微调器并设置一个 onClik 侦听器。当用户点击 State 时,从 Colleges 表中搜索加载微调器,其中 State = selected State 列。

下面是来自http://www.androidhive.info/2012/06/android-populating-spinner-data-from-sqlite-database/的修改类

我建议通过他的教程。

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHandler extends SQLiteOpenHelper {
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "spinnerExample";

    // Labels table name
    private static final String TABLE_STATES = "states";
    private static final String TABLE_COLLEGES = "colleges";

    // Labels Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_STATE = "state";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        // Category table create query
        String CREATE_STATES_TABLE = "CREATE TABLE " + TABLE_LABELS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT)";
        db.execSQL(CREATE_STATES_TABLE);
        String CREATE_COLLEGES_TABLE = "CREATE TABLE " + TABLE_LABELS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT" + KEY_STATE + "TEXT)";
        db.execSQL(CREATE_COLLEGES_TABLE);
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_STATES);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_COLLEGES);

        // Create tables again
        onCreate(db);
    }

    /**
     * Getting all labels
     * returns list of labels
     * */
    public List<String> getAllStates(){
        List<String> labels = new ArrayList<String>();

        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_STATES;

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                labels.add(cursor.getString(1));
            } while (cursor.moveToNext());
        }

        // closing connection
        cursor.close();
        db.close();

        // returning lables
        return labels;
    }

    public List<String> getCollegs(String state){
            List<String> labels = new ArrayList<String>();

            // Select All Query
            String selectQuery = "SELECT  * FROM " + TABLE_COLLEGES + " WHERE " + KEY_STATE " EQUALS " + state;

            SQLiteDatabase db = this.getReadableDatabase();
            Cursor cursor = db.rawQuery(selectQuery, null);

            // looping through all rows and adding to list
            if (cursor.moveToFirst()) {
                do {
                    labels.add(cursor.getString(1));
                } while (cursor.moveToNext());
            }

            // closing connection
            cursor.close();
            db.close();

            // returning lables
            return labels;
        }
    }
于 2013-08-26T05:01:09.643 回答
0

为您的第一个微调器实现onItemSelectedListener(),借助它,您可以在第一个微调器中获得选定的值,并据此在第一个微调器的onItemSelected方法中填充第二个微调器。

spinner1.setOnItemSelectedListener(new OnItemSelectedListener() 
    {

        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) 
        {


            String selected = spinner1.getSelectedItem().toString();


        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }
    });

您还可以参考以下链接以获取想法。

如何根据 spinner1 选择更新 spinner2

基于某个微调器选择填充另一个微调器

http://www.yogeshblogspot.com/android-spinner-example/

于 2013-08-26T05:00:29.510 回答
0
spinner.setOnItemSelectedListener(new OnItemSelectedListener() 
            {
                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) 
                {
                    String state= arg0.getSelectedItem().toString();

  // Here you can use selected string from spinner and set new value to another spinner with following code and enjoy doing it

                    ArrayAdapter<String> college= new ArrayAdapter<String>(Your_Context, your_layout, Your_Array);

                    spinner2.setAdapter(college);

                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) 
                {
                }
            });
于 2013-08-26T05:02:28.907 回答