从spinner获取从MySQL获取数据的文本

时间:2018-05-19 19:00:51

标签: android mysql spinner

在MySQL中,我在名为DESIGN_CODE的列中调用以下数字:1000,1001,1002 ......等等。这些数字是旋转的,我试图通过这种方式从微调器中获取数字。

Spinner spinner = (Spinner)findViewById(R.id.spinner);
String text = spinner.getSelectedItem().toString();

我想要文字' 1000'结果,但它显示像这样

{DESIGN_CODE=1000}

问题是什么?如何才能获得结果' 1000'?

这是完整的代码。

public class InputProductionInfoActivity extends Activity {

TextView dateInputButton, timeInputButton;
Spinner spinner;
EditText productAmountEditText;

ArrayList<HashMap<String, String>> mArrayList;
String mJsonString;
String productDate, productTime;

private static String TAG = "ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ";

private static final String TAG_JSON = "design";
private static final String TAG_DESIGN_CODE = "DESIGN_CODE";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_input_production_info);

    spinner = findViewById(R.id.designSpinner);
    dateInputButton = findViewById(R.id.dateInputButton);
    timeInputButton = findViewById(R.id.timeInputButton);

    mArrayList = new ArrayList<>();
    InputProductionInfoActivity.GetData task = new InputProductionInfoActivity.GetData();
    task.execute("http://www.cafe24.com/select_design.php");

    final Calendar c = Calendar.getInstance();
    int mYear = c.get(Calendar.YEAR);
    int mMonth = c.get(Calendar.MONTH);
    int mDay = c.get(Calendar.DAY_OF_MONTH);
    final DatePickerDialog datePickerDialog = new DatePickerDialog(this,
            new DatePickerDialog.OnDateSetListener() {

                @SuppressLint("SetTextI18n")
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    dateInputButton.setText(year + "년 " + (monthOfYear + 1) + "월 " + dayOfMonth + "일");
                    productDate = year + "-" + monthOfYear + "-" + dayOfMonth;
                }
            }, mYear, mMonth, mDay);
    dateInputButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            datePickerDialog.show();
        }
    });
    final int mHour = c.get(Calendar.HOUR_OF_DAY);
    final int mMin = c.get(Calendar.MINUTE);
    final TimePickerDialog timePickerDialog = new TimePickerDialog(this,
            new TimePickerDialog.OnTimeSetListener() {

                @SuppressLint("SetTextI18n")
                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    timeInputButton.setText(hourOfDay + "시 " + minute + "분");
                    productTime = hourOfDay + ":" + minute +":00";
                }
            }, mHour, mMin, true);
    timeInputButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            timePickerDialog.show();
        }
    });


    Button enterProductInformation = findViewById(R.id.enterProductInformation);
    enterProductInformation.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String spinnerItem = spinner.getSelectedItem().toString();
            String productAmount = productAmountEditText.getText().toString();

            final RadioGroup dayOrNightRadioGroup = findViewById(R.id.dayOrNightRadioGroup);
            int checkedId = dayOrNightRadioGroup.getCheckedRadioButtonId();
            RadioButton typeButton = findViewById(checkedId);
            String dayOrNight = typeButton.getText().toString();

            try {
                InsertData task = new InsertData();
                task.execute(productDate, productTime, dayOrNight, spinnerItem, productAmount);
            } catch (Exception e){
                Log.d(TAG, "error : ", e);
            }

        }
    });
}

@SuppressLint("StaticFieldLeak")
private class GetData extends AsyncTask<String, Void, String> {
    ProgressDialog progressDialog;
    String errorString = null;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        progressDialog = ProgressDialog.show(InputProductionInfoActivity.this,
                "Please Wait", null, true, true);
    }


    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        progressDialog.dismiss();
        Log.d(TAG, "response  - " + result);

        if (result == null) {

        } else {

            mJsonString = result;
            showResult();
        }
    }


    @Override
    protected String doInBackground(String... params) {

        String serverURL = params[0];


        try {

            URL url = new URL(serverURL);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();


            httpURLConnection.setReadTimeout(5000);
            httpURLConnection.setConnectTimeout(5000);
            httpURLConnection.connect();


            int responseStatusCode = httpURLConnection.getResponseCode();
            Log.d(TAG, "response code - " + responseStatusCode);

            InputStream inputStream;
            if (responseStatusCode == HttpURLConnection.HTTP_OK) {
                inputStream = httpURLConnection.getInputStream();
            } else {
                inputStream = httpURLConnection.getErrorStream();
            }


            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            StringBuilder sb = new StringBuilder();
            String line;

            while ((line = bufferedReader.readLine()) != null) {
                sb.append(line);
            }


            bufferedReader.close();


            return sb.toString().trim();


        } catch (Exception e) {

            Log.d(TAG, "InsertData: Error ", e);
            errorString = e.toString();

            return null;
        }

    }
}


private void showResult() {
    try {
        JSONObject jsonObject = new JSONObject(mJsonString);
        JSONArray jsonArray = jsonObject.getJSONArray(TAG_JSON);

        for (int i = 0; i < jsonArray.length(); i++) {

            JSONObject item = jsonArray.getJSONObject(i);

            String designCode = item.getString(TAG_DESIGN_CODE);

            HashMap<String, String> hashMap = new HashMap<>();

            hashMap.put(TAG_DESIGN_CODE, designCode);

            mArrayList.add(hashMap);
        }

        SimpleAdapter adapter = new SimpleAdapter(
                InputProductionInfoActivity.this, mArrayList, R.layout.input_info_spinner,
                new String[]{TAG_DESIGN_CODE},
                new int[]{R.id.spinner_item}
        );

        spinner.setAdapter(adapter);

    } catch (JSONException e) {

        Log.d(TAG, "showResult : ", e);
    }

}

@SuppressLint("StaticFieldLeak")
class InsertData extends AsyncTask<String, Void, String> {
    ProgressDialog progressDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        progressDialog = ProgressDialog.show(InputProductionInfoActivity.this,
                "Please Wait", null, true, true);
    }


    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        progressDialog.dismiss();
        //mTextViewResult.setText(result);

        Log.d(TAG, "POST response  - " + result);
    }


    @Override
    protected String doInBackground(String... params) {

        String productDate = params[0];
        String productTime = params[1];
        String dayOrNight = params[2];
        String spinnerItem = params[3];
        String productAmount = params[4];

        String serverURL = "http://www.cafe24.com/insert_product.php";

        String postParameters = "PRODUCT_DATE=" + productDate
                + "&PRODUCT_TIME=" + productTime
                + "&DAY_NIGHT=" + dayOrNight
                + "&DESIGN_CODE=" + spinnerItem
                + "&PRODUCT_OUTPUT=" + productAmount;


        try {

            URL url = new URL(serverURL);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();


            httpURLConnection.setReadTimeout(5000);
            httpURLConnection.setConnectTimeout(5000);
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.connect();


            OutputStream outputStream = httpURLConnection.getOutputStream();
            outputStream.write(postParameters.getBytes("UTF-8"));
            outputStream.flush();
            outputStream.close();


            int responseStatusCode = httpURLConnection.getResponseCode();
            Log.d(TAG, "POST response code - " + responseStatusCode);

            InputStream inputStream;
            if (responseStatusCode == HttpURLConnection.HTTP_OK) {
                inputStream = httpURLConnection.getInputStream();
            } else {
                inputStream = httpURLConnection.getErrorStream();
            }


            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            StringBuilder sb = new StringBuilder();
            String line = null;

            while ((line = bufferedReader.readLine()) != null) {
                sb.append(line);
            }


            bufferedReader.close();


            return sb.toString();


        } catch (Exception e) {

            Log.d(TAG, "InsertData: Error ", e);

            return "Error: " + e.getMessage();
        }

    }
}

}

1 个答案:

答案 0 :(得分:0)

如果要在微调器中显示的是值1000,1001,1002,....那么将代码更改为:

private void showResult() {
    try {
        ArrayList<String> arrayList = new ArrayList<>();

        JSONObject jsonObject = new JSONObject(mJsonString);
        JSONArray jsonArray = jsonObject.getJSONArray(TAG_JSON);

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject item = jsonArray.getJSONObject(i);
            String designCode = item.optString(TAG_DESIGN_CODE, "No Value");
            arrayList.add(designCode);
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                InputProductionInfoActivity.this, 
                android.R.layout.simple_spinner_dropdown_item, 
                arrayList);

        if(spinner != null) {
            spinner.setAdapter(adapter);
        }
    } catch (JSONException e) {
        Log.d(TAG, "showResult : ", e);
    }
}

选择项目后,您只能获得价值&#34; 1000&#34;或&#34; 1002&#34;无论选择什么。