Step 1: Open Eclipse :P
Step 2 : Add the following Code to activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Spinner
android:id="@+id/spinner2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10dp"
android:layout_marginTop="99dp" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="99dp"
android:layout_marginLeft="10dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Button" />
</RelativeLayout>
Step 3 : Add the following code to mainActivity.java :
package com.example.spinner_ex10;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
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
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Step 4 : Add the Code to AndroidManifest.xml :
<uses-permission android:name="android.permission.INTERNET"/>
Step 5 : Create a Database and Table like below :
Step 6 : Add the following code to spinner.php :
<?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);
?>
Step 7 : Now save and execute the spinner.php file in your web browser. Your output will be like this:
{"0":"082IT108","roll_no":"082IT108","1":"Arul Kumar","name":"Arul kumar"},
{"0":"082IT110","roll_no":"082IT110","1":"Ashok kumar","name":"Ashok kumar"},
{"0":"082IT111","roll_no":"082IT111","1":"Balaji","name":"Balaji"},
{"0":"082IT113","roll_no":"082IT113","1":"Boopathy","name":"Boopathy"},
{"0":"082IT123","roll_no":"082IT123","1":"Godwin","name":"Godwin"}]
Step 8 : The “roll_no” and “name” are our variable to carry the data from PHP to Android application. We can access this in Android as below that i already gave in mainActivity.java :
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");
}
Step 9 : Now can save and execute the Application. Your output will be like this :
Data copied from here
No comments:
Post a Comment