我有一个微调器,我正在填充它。我有值列表,我也得到了微调器选择的位置。现在我想显示基于 Spinner Item Selected 位置的值列表。怎么做。谁能帮帮我吗?
问问题
981 次
2 回答
2
我认为问题在于构建数据
结构数据如下
1- 使用 Bean/Model 将所有数据包装为一个单元,从 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) {
}
于 2012-06-27T07:33:05.627 回答
2
嘿,这对我有用……第一件事是……说这个国家的微调器
/ * ** * ** * ** * ** * *国家/地区微调器* ** * ** * ** * *** /
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*******************/
然后现在在覆盖的方法中
@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*******************/
}
所以这就是你的做法......
于 2012-06-27T07:47:28.233 回答