Saturday, June 18, 2011

Change values from one spinner to another in Android

Hi friends,In this article i would like to share how to bind values From one spinner to another spinner...Now just follow the steps to bind the data from one spinner to another spinner depending up on selection
First,We need to write a Query in DBAdapter.class file to display the values in Spinner

This is the code for get all the values in First spinner

// ---retrieves all Friend Names---
public Cursor getAllProjects() {

return db.query(DATABASE_TABLE_FRIEND, new String[] { FRIEND_ID, FRIEND_NAME,
}, null, null, null, null, null);
}

public Cursor getAllFriendslocations_from_projectid(int fid) {

Cursor mCursor = db.query(DATABASE_TABLE_FRIENDlocations, new String[] { FRIEND_ID,FRIEND_NAME,
},FRIEND_ID + "='" +fid+"'",null, null, null, null, null);

if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
} 

Now,Create a new Xml file and save the file like MySpinner.xml

Next,open the file and write the Follwoing code in that





Next save that file and kept this folder in Layout folder

Next Create a class file and save the file like SpinnerClass.class

Next,Write the following code in that

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);

Friendnames= (Spinner) findViewById(R.id.Friendnames);
FriendLOCATIONS = (Spinner) findViewById(R.id.FriendLOCATIONS);

BindValues(); 

}

public void BindValues()
{

try{
List list = new ArrayList();
DBAdapter db = new DBAdapter(getBaseContext());
db.open();
Cursor cursor = db.getAllProjectss();

if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(2)); 
} while (cursor.moveToNext());
}

db.close();
FRND_NAME = (String []) list.toArray (new String [list.size ()]);
Log.v("PO's",String.valueOf(FRND_NAME.length));         

ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_single_choice, FRND_NAME);
Friendnames.setAdapter(adapter);

FRND_NAME.setOnItemSelectedListener(new OnItemSelectedListener() {

public void onItemSelected(AdapterView arg0, View arg1,
int arg2, long arg3) {

bind_locations(arg2);

}
public void onNothingSelected(AdapterView arg0) {
}

});

}
catch(Exception e)
{
Log.v("Error","e.tostring()");

}

Now Bind the Values in Location spinner..write the following code

public void bind_locations(int arg2){
List locations_list = new ArrayList();
try{
DBAdapter db1 = new DBAdapter(getBaseContext());
db1.open();
Cursor c = db1.getProjectID_from_projName(FRND_NAME[arg2]);
String f_id = c.getString(0);
String selected_f_id = c.getString(0).toString();

Cursor c1 = db1.getAllProducts_by_project_id(project_id);
if (c1.moveToFirst()) {
do {
locations_list.add(c1.getString(3));
} while (c1.moveToNext());
}
db1.close();
LOCATIONS= (String[]) locations_list.toArray(new String[locations_list.size()]);


}catch (Exception e) {


}

ArrayAdapter adapter2 = new ArrayAdapter(this,
R.layout.spinrow,R.id.weekofday, LOCATIONS);
FriendLOCATIONS.setAdapter(adapter2);

Finally save the file and run it

Thats it..

developercode
About the Author
Sayyad is a Software Engineer, Blogger and Founder of Developers Code from India.His articles mainly focus on .Net (Web and Windows based applications), Mobile Technologies (Android,IPhone,BlackBerry) and SEO.

Labels: , , , , , ,

5 Comments:

At June 18, 2011 at 2:15 PM , Anonymous Anonymous said...

Thanks for sharing .......Good One

 
At June 18, 2011 at 2:15 PM , Anonymous Anonymous said...

Hey back to Android

 
At June 19, 2011 at 6:05 PM , Blogger twittersongs said...

Good Post..can u explain briefly

 
At June 28, 2011 at 10:16 AM , Anonymous Anonymous said...

Hey back to Android

 
At August 18, 2014 at 10:57 AM , Anonymous Anonymous said...

Nice post..Will u explain how to get the particular _id of the selected item from spinner1 to retrieve the value based on the _id to populate the spinner2.

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home