显示spinner从sql数据库中选择的项目ID

时间:2014-10-02 10:38:51

标签: android mysql android-spinner

以下是我主要活动的一部分

MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sp_country = (Spinner) findViewById(R.id.spin_country);
    sp_operator = (Spinner) findViewById(R.id.spin_operator);
    sp_prod = (Spinner) findViewById(R.id.spin_prod);
    sp_voc = (Spinner) findViewById(R.id.spin_voc);
    logoo = (ImageView) findViewById(R.id.b_logo);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);
    /*-----------Check if Wifi Connection----------------------*/

    ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    {
        if (conMgr.getActiveNetworkInfo() != null
                && conMgr.getActiveNetworkInfo().isAvailable()
                && conMgr.getActiveNetworkInfo().isConnected()) {
        } else {
            finish();
            Toast.makeText(getApplicationContext(),
                    "INTERNET CONNECTION NOT PRESENT", Toast.LENGTH_SHORT)
                    .show();

    }
}
    String data = "";
    ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();

    // define the parameter
    postParameters.add(new BasicNameValuePair("785", data));
    try {
        CustomHTTPClient.executeHttpGet("785");
    } catch (Exception e1) {

        e1.printStackTrace();
    }

    String response = null;
    // call executeHttpPost method passing necessary parameters
    try {

        response = CustomHTTPClient.executeHttpPost(

        url,
                postParameters);

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

        // parse json data

        try {

            JSONArray jArray = new JSONArray(result);

            name = new String[jArray.length() + 1];
            c_id = new String[jArray.length()];

            /*----set default value in spinner----*/
            name[0] = "Select Country";

            // end

            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json_data = jArray.getJSONObject(i);

                // Log.i("tagconvertstr", "[" + data + "]");

                name[i + 1] = json_data.getString("c_name");
                c_id[i] = json_data.getString("c_id");
                Log.d("id is", "" + c_id[i]);

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

        try {

            dataAdapter1 = new ArrayAdapter<String>(
                    getApplicationContext(), R.layout.spinner_item, name);
            dataAdapter1.setDropDownViewResource(R.layout.spinner_item);

            sp_country.setAdapter(dataAdapter1);

        } 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());
    }
    sp_country.setOnItemSelectedListener(new OnItemSelectedListener() {

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

            int aa = sp_country.getPositionForView(view);

            long id = sp_country.getSelectedItemId();
            String stringpf = String.valueOf(id);

            Toast.makeText(getApplicationContext(), "" + stringpf, 0)
                    .show();
}

我从sql数据库获取sp_country的所有项目的id ..如果我有这种类型的数据

c_id | c_name
   1 | A
   2 | B
   4 | C
   5 | F

我想使用c_id来获取下一个微调器的数据库中的项目,以便下一个微调器使用前一个微调器的db项目编号.....

0 个答案:

没有答案