无法将微调器选择值android传递给php

时间:2014-08-06 11:43:23

标签: php android

我需要从外部数据库中读取数百个项目的数据..所以我试图将微调器选择值传递给php

android活动:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.icd);

    spinnerCity = (Spinner) findViewById(R.id.spinnerCity);

    txt = (TextView) findViewById(R.id.textView1);
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, paths);
    dataAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerCity.setAdapter(dataAdapter);

    spinnerCity.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            String data = spinnerCity.getSelectedItem().toString();

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

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

            String response = null;

            // call executeHttpPost method passing necessary parameters
            try {

                response = CustomHttpClient.executeHttpPost(

                urlheart,
                ,       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 {
                    txt.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) {

        }
    });
}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

}

PHP:

if(isset($_POST['786'])){
$id = $_POST['786'];

$sql = "SELECT icd_symptoms FROM icd WHERE icd_disease = 'id'";
$result = mysql_query($sql);
while($row=mysql_fetch_assoc($result))
$output[]=$row;
print(json_encode($output));
mysql_close();   

我试图以这种方式传递价值,但它在我点击按钮之前没有工作,但不知道为什么它不适用于微调器。请帮忙。提前完成。

1 个答案:

答案 0 :(得分:0)

在这里,您必须为微调器设置onItemSlected以获取所选项目。

您正在将适配器设置为微调器,同时尝试获取所选的微调器项而不使用任何操作侦听器

ArrayAdapter<CharSequence> adapter = ArrayAdapter

        .createFromResource(this, R.array.city,

        android.R.layout.simple_spinner_item);

        spinnerCity.setAdapter(adapter);

        ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        String data = spinnerCity.getSelectedItem().toString();

Fisrt点亮了在apinner中选择的项目而不是

String data = spinnerCity.getSelectedItem().toString();

。在其中。

首先做一件事,只检查您是否在所选的微调器项目上获得所选值。只需放下代码的和平,并保持日志打印您的valeu。

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        Object item = parent.getItemAtPosition(pos);
        // put log here to print you value selected
    }
    public void onNothingSelected(AdapterView<?> parent) {
    }
});