Reputation: 495
I have one spinner, and I am populating that. I have list of values and I am getting the spinner selected position also. Now I want to display the list of values based on Spinner Item Selected position. How to do that. Can anyone please help me?
Upvotes: 2
Views: 1003
Reputation: 6128
Hey this is working for me...First thing is...say this the spinner for country
/**************spinner for country*************/
ArrayList<NameValuePair> alcountry = new ArrayList<NameValuePair>();
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://blah.com/country.php");
httppost.setEntity(new UrlEncodedFormEntity(alcountry));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
Countryresult=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
countryarray = new JSONArray(Countryresult);
JSONObject json_data=null;
for(int i=0;i<countryarray.length();i++){
json_data = countryarray.getJSONObject(i);
Countrylist.add(json_data.getString("country_name"));
System.out.println("hq value is " +Countrylist);
Countryadapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,Countrylist);
country.setAdapter(Countryadapter);
country.setOnItemSelectedListener(this);
}
}
catch(JSONException e1){
Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
/*****************************end of spinner for country*******************/
then now in the overidden method
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
// TODO Auto-generated method stub
state = (Spinner)findViewById(R.id.etstate);
Statelist = new ArrayList<String>();
/*****************************spinner for state based on country**************************/
ArrayList<NameValuePair> alstate = new ArrayList<NameValuePair>();
alstate.add(new BasicNameValuePair("country_name",country.getSelectedItem().toString()));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://blah.com/state.php");
httppost.setEntity(new UrlEncodedFormEntity(alstate));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
Stateresult=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
statearray = new JSONArray(Stateresult);
JSONObject json_data=null;
for(int i=0;i<statearray.length();i++){
json_data = statearray.getJSONObject(i);
Statelist.add(json_data.getString("state_name"));
System.out.println("hq value is " +Statelist);
Stateadapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,Statelist);
state.setAdapter(Stateadapter);
}
}
catch(JSONException e1){
Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
state.setOnItemSelectedListener(this);
/*****************************end of spinner for state*******************/
}
So this is how you do it...
Upvotes: 2
Reputation: 15701
I think problem is of structuring the data
Structure data as below
1- use Bean/Model to wrap all the data as a unit get from json
if (loadseq == index) {
Dealer dealer = new Dealer();
dealer.setVIN(js.getString("vin");)
.......
.....
al.add(dealer);
public class Dealer{
private String dealer_name1 ;
private Vehicle ;
private String vin ;
private String color ;
private String parking ;
private String vindesc ;
//getter seeter of all
@Override
public String toString() {
return dealer_name1;
}
}
class mySpinnerListener implements Spinner.OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
Dealer d = (Dealer) parent.getItemAtPosition(position);
or
Dealer d = al.elmentAt(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
Upvotes: 2