Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
PHP
public class MainActivity extends Activity {
InputStream is=null;
String result=null;
String line=null;

String[] state_name,city_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://localhost/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("state_name");
name[i]=json.getString("city_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, state_name);
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, city_name);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter2);


spinner1.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView{
// TODO Auto-generated method stub

spinner2.setSelection(position);

}

@Override
public void onNothingSelected(AdapterView{
// TODO Auto-generated method stub
}

});


spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView// TODO Auto-generated method stub

spinner1.setSelection(position);

}

@Override
public void onNothingSelected(AdapterView// TODO Auto-generated method stub
}
});

}
Posted
Updated 26-Sep-15 12:05pm
v2
Comments
Richard MacCutchan 25-Sep-15 8:38am    
What is the problem, where is the error? And, please format your code properly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900