How to check if a spinners item changed by user in android? -
i filtering using spinners. have 2 spinners, 1st list of states, 2nd list of colleges. when program runs, select state in 1st spinner, program filters colleges in state in 2nd spinner. when change state in 1st spinner, not filter it. wanted know how can select state , filter accordingly.
spinner 1 contain;
select state [ny,nj]
spinner 2 contain;
select college [nyu, nyit, njit]
i wanna able select ny first , see nyu , nyit. wanna select nj , see nyit.
please give me guideline.
use database store values. have 1 table states , 1 table colleges. in colleges table have column storing state load spinner states table , set onclik listener. when user clicks on state, search load spinner colleges table column state = selected state.
below modifide class http://www.androidhive.info/2012/06/android-populating-spinner-data-from-sqlite-database/
i suggest going through tutorial.
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 labels * returns list of labels * */ public list<string> getallstates(){ list<string> labels = new arraylist<string>(); // select query string selectquery = "select * " + table_states; sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.rawquery(selectquery, null); // looping through rows , adding list if (cursor.movetofirst()) { { 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 query string selectquery = "select * " + table_colleges + " " + key_state " equals " + state; sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.rawquery(selectquery, null); // looping through rows , adding list if (cursor.movetofirst()) { { labels.add(cursor.getstring(1)); } while (cursor.movetonext()); } // closing connection cursor.close(); db.close(); // returning lables return labels; } }
Comments
Post a Comment