我有2个纱厂一个省和其他的城市,如果我在省选中豪登微调第二微调必须出示约翰内斯堡,pretoria,百夫长,如果我在全省选择KZN我想第二微调显示Petermaritsburg ,德班,乌伦迪
下面是我的code,我曾尝试使用,但在all.i不工作将appriciate您的帮助
INT CT_ID; 的String [] ct_name = NULL; 公共无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.extras); 普罗=(微调)findViewById(R.id.spinprovince); 城市=(微调)findViewById(R.id.spincity); 节省=(按钮)findViewById(R.id.btnsave); 字符串结果= NULL; InputStream为= NULL; StringBuilder的SB = NULL; // HTTP POST 尝试{ HttpClient的HttpClient的=新DefaultHttpClient(); HttpPost httppost =新HttpPost(http://10.0.2.2/php/ttz.php); // httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中)); HTT presponse响应= httpclient.execute(httppost); HttpEntity实体= response.getEntity(); 是= entity.getContent(); }赶上(例外五){ Log.e(log_tag,在HTTP连接错误+ e.toString()); } //响应转换为字符串 尝试{ 读者的BufferedReader =新的BufferedReader(新的InputStreamReader( 是,ISO-8859-1),8); SB =新的StringBuilder(); sb.append(reader.readLine()+\\ n); 串行=0; 而((行= reader.readLine())!= NULL){ sb.append(行+\\ n); } is.close(); 结果= sb.toString(); }赶上(例外五){ Log.e(log_tag,错误转换结果+ e.toString()); } //配对数据 JSONArray jArray; 尝试{ jArray =新JSONArray(结果); JSONObject的json_data = NULL; ct_name =新的String [jArray.length()]; 的for(int i = 0; I< jArray.length();我++){ json_data = jArray.getJSONObject(ⅰ); CT_ID = json_data.getInt(idprovince); ct_name [I] = json_data.getString(provincename); ArrayAdapter AA =新ArrayAdapter(这一点,android.R.layout.simple_spinner_item,ct_name); aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); provins.setAdapter(AA); } }赶上(JSONException E1){ Toast.makeText(getBaseContext(),无城找到,Toast.LENGTH_LONG) 。显示(); }赶上(ParseException的E1){ e1.printStackTrace(); }}}
解决方案
试试这个code ...
公共类MainActivity延伸活动{InputStream为= NULL;字符串结果= NULL;串线= NULL;的String [] roll_no,名称;微调spinner1,spinner2;@覆盖公共无效的onCreate(捆绑savedInstanceState){super.onCreate(savedInstanceState);的setContentView(R.layout.activity_main);spinner1 =(微调)findViewById(R.id.spinner1);spinner2 =(微调)findViewById(R.id.spinner2);最终名单<串GT; list1的=新的ArrayList<串GT;();最终名单<串GT;列表2 =新的ArrayList<串GT;();按钮B =(按钮)findViewById(R.id.button1);b.setOnClickListener(新View.OnClickListener(){@覆盖公共无效的onClick(视图v){ // TODO自动生成方法存根尝试{HttpClient的HttpClient的=新DefaultHttpClient();HttpPost httppost =新HttpPost(http://10.0.2.2/spinner.php);HTT presponse响应= httpclient.execute(httppost);Log.e(故障1,3);HttpEntity实体= response.getEntity();Log.e(故障1,4);是= entity.getContent();Log.e(传1,连接成功);}赶上(例外五){Log.e(故障1,e.toString());Toast.makeText(getApplicationContext(),无效的IP地址,Toast.LENGTH_LONG).show();完();}尝试{读者的BufferedReader =新的BufferedReader(新的InputStreamReader(是,ISO-8859-1),8);StringBuilder的SB =新的StringBuilder();而((行= reader.readLine())!= NULL){sb.append(行+\\ n);}is.close();结果= sb.toString();}赶上(例外五){Log.e(故障2,e.toString());}尝试{JSONArray JA =新JSONArray(结果);JSONObject的JSON = NULL;roll_no =新的String [JA.length()];NAME =新的String [JA.length()];的for(int i = 0; I< JA.length();我++){JSON = JA.getJSONObject(ⅰ);roll_no [I] = json.getString(roll_no);名称[I] = json.getString(名称);}Toast.makeText(getApplicationContext(),SSS,Toast.LENGTH_LONG).show();的for(int i = 0; I< roll_no.length;我++){list1.add(roll_no [I]);list2.add(名称[I]);}spinner_fn();}赶上(例外五){Log.e(故障3,e.toString());//login.this.finish();}}});}私人无效spinner_fn(){// TODO自动生成方法存根ArrayAdapter<串GT; dataAdapter1 =新ArrayAdapter<串GT;(getApplicationContext() android.R.layout.simple_spinner_item,roll_no);dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);spinner1.setAdapter(dataAdapter1);ArrayAdapter<串GT; dataAdapter2 =新ArrayAdapter<串GT;(getApplicationContext() android.R.layout.simple_spinner_item,名);dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);spinner2.setAdapter(dataAdapter2);spinner1.setOnItemSelectedListener(新OnItemSelectedListener(){@覆盖公共无效onItemSelected(适配器视图<>为arg0,ARG1查看,INT位置,长ID){// TODO自动生成方法存根spinner2.setSelection(位置);}@覆盖公共无效onNothingSelected(适配器视图<>为arg0){// TODO自动生成方法存根}});spinner2.setOnItemSelectedListener(新OnItemSelectedListener(){@覆盖公共无效onItemSelected(适配器视图<>为arg0,ARG1查看,INT位置,长ARG3){// TODO自动生成方法存根spinner1.setSelection(位置);}@覆盖公共无效onNothingSelected(适配器视图<>为arg0){// TODO自动生成方法存根}});}
不要忘了给Androidmanifest权限:
<使用许可权的android:NAME =android.permission.INTERNET对/>
和您的spinner.php文件:
< PHP$主机=127.0.0.1;$的uname ='root'_name;$ PWD ='密码';$ DB ='机器人';$ CON = mysql_connect($主机,$的uname,$ PWD)或死亡(连接失败);mysql_select_db($数据库,$ CON)或死亡(DB选择失败);$ R =的mysql_query(选择从类*,$ CON);而($行= mysql_fetch_array($ R)){$ CLS [] = $行;//回声$鳍< BR>中。}打印(json_en code($ CLS));mysql_close($ CON);?>
i have 2 spinners one for Province and other for City and if i select Gauteng in Province spinner the second spinner must show Johannesburg,Pretoria,Centurion and if i select KZN in province i want the second spinner to show Petermaritsburg,Durban,Ulundi
Here is my code that i have tried to use but is not working at all.i will appriciate your help
int ct_id;
String[] ct_name = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.extras);
provins = (Spinner) findViewById(R.id.spinprovince);
city = (Spinner) findViewById(R.id.spincity);
save = (Button) findViewById(R.id.btnsave);
String result = null;
InputStream is = null;
StringBuilder sb = null;
// http post
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/php/ttz.php");
// httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
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());
}
// convert response to string
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();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
// paring data
JSONArray jArray;
try {
jArray = new JSONArray(result);
JSONObject json_data = null;
ct_name = new String[jArray.length()];
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
ct_id = json_data.getInt("idprovince");
ct_name[i] = json_data.getString("provincename");
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,ct_name);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
provins.setAdapter(aa);
}
} catch (JSONException e1) {
Toast.makeText(getBaseContext(), "No City Found", Toast.LENGTH_LONG)
.show();
} catch (ParseException e1) {
e1.printStackTrace();
}
}
}
解决方案
Try this Code...
public class MainActivity extends Activity {
InputStream is=null;
String result=null;
String line=null;
String[] roll_no,name;
Spinner spinner1,spinner2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
final List<String> list1 = new ArrayList<String>();
final List<String> list2 = new ArrayList<String>();
Button b=(Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/spinner.php");
HttpResponse response = httpclient.execute(httppost);
Log.e("Fail 1", "3");
HttpEntity entity = response.getEntity();
Log.e("Fail 1", "4");
is = entity.getContent();
Log.e("Pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",Toast.LENGTH_LONG).show();
finish();
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}
try
{
JSONArray JA=new JSONArray(result);
JSONObject json= null;
roll_no = new String[JA.length()];
name = new String[JA.length()];
for(int i=0;i<JA.length();i++)
{
json=JA.getJSONObject(i);
roll_no[i] = json.getString("roll_no");
name[i]=json.getString("name");
}
Toast.makeText(getApplicationContext(), "sss",Toast.LENGTH_LONG).show();
for(int i=0;i<roll_no.length;i++)
{
list1.add(roll_no[i]);
list2.add(name[i]);
}
spinner_fn();
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
//login.this.finish();
}
}
});
}
private void spinner_fn() {
// TODO Auto-generated method stub
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item, roll_no);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter1);
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item, name);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter2);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long id)
{
// TODO Auto-generated method stub
spinner2.setSelection(position);
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
});
spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long arg3) {
// TODO Auto-generated method stub
spinner1.setSelection(position);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
Dont forget to give the Androidmanifest permission :
<uses-permission android:name="android.permission.INTERNET"/>
And your spinner.php file :
<?php
$host='127.0.0.1';
$uname='root'_name;
$pwd='password';
$db='android';
$con = mysql_connect($host,$uname,$pwd) or die("connection failed");
mysql_select_db($db,$con) or die("db selection failed");
$r=mysql_query("select * from class",$con);
while($row=mysql_fetch_array($r))
{
$cls[]=$row;
//echo $fin."<br>";
}
print(json_encode($cls));
mysql_close($con);
?>
上一篇:转换对象到urlen codeD字符串字符串、对象、urlen、codeD
下一篇:C#WinForms应用程序失败在发送电子邮件:远程名称无法解析:'smtp.gmail.com;操作超时应用程序、发送电子邮件、名称、操作