Android : passing spinner selected data from android to php

on Sunday, August 3, 2014


I need to read data from an external database with hundreds of items. so for i am trying to pass spinner selected value to php


android code is here:



protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.icd);
states = getResources().getStringArray(R.array.Diseases);
t1 = (TextView) findViewById(R.id.textView1);

spinner = (Spinner) findViewById(R.id.country_spinner);

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, states);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {

ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();

value = spinner.getSelectedItem().toString();

// define the parameter
postParameters.add(new BasicNameValuePair("789", value));
try {
CustomHttpClient.executeHttpGet("789");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

String response = null;

// call executeHttpPost method passing necessary parameters
try {

response = CustomHttpClient.executeHttpPost(

urlheart,
// in case of remote server
// "http://omega.uta.edu/~kmr2464/jsonscript.php",
postParameters);

// store the result returned by PHP script that runs
// MySQL query
String result = response.toString();

// parse json data
try {
returnString = "";
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag", "id: " + json_data.getInt("id")
+ ", name: " + json_data.getString("name")

);
returnString += "\n" + "Name ="
+ json_data.getString("icd_disease") + "\n"
+ " Symptoms = "
+ json_data.getInt("icd_symptoms") + "\n"
+ " "
+ json_data.getString("icd_treatment");

}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}

try {
t1.setText(returnString);


} catch (Exception e) {
Log.e("log_tag", "Error in Display!" + e.toString());
;
}
} catch (Exception e) {
Log.e("log_tag",
"Error in http connection!!" + e.toString());
}

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {

}
});
}


php:



if(isset($_POST['789'])){
$id = $_POST['789'];
$sql = "SELECT * FROM icd WHERE icd_disease = 'id'";
$result = mysql_query($sql);
while($row=mysql_fetch_assoc($result))
$output[]=$row;
print(json_encode($output));
mysql_close();


My question is how can I pass the data from spinner android to php query so the query will only return the proper items only aginst selected item


0 comments:

Post a Comment