티스토리 뷰

SWDesk/App

[Android] Spinner Example

inhae 2021. 8. 29. 20:59

스피너 예제

 

	Spinner spModelList1;

    protected void onCreate(Bundle savedInstanceState){
		...
        
		spModelList1 = (Spinner) findViewById(R.id.sp_modellist1);

		spModelList1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                ModelIndex = i;
                JSONObject jsonObj = ModelInfos.get(ModelIndex);
                try {
                    etSNo1.setText(jsonObj.getString("SNo"));
                    etModelName1.setText(jsonObj.getString("ModelName"));
                } catch (Exception e) {
                    Log.d(TAG, "[?-1] onItemSelected()");
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                ModelIndex = 0;
                try {
                    JSONObject jsonObj = ModelInfos.get(ModelIndex);
                    etModelName1.setText(jsonObj.getString("ModelName"));
                } catch (Exception e) {
                    Log.d(TAG, "[?-1] onNothingSelected()");
                }
            }
        });
        ...
        
        SetSpinnerItems(modelInfos);
    }

    private void SetSpinnerItems(JSONArray modelInfos) {
        ModelInfos = new ArrayList<>();
        ...
        for (JSONObject modelInfo : ModelInfos) {
            try {
                modelName = modelInfo.getString("ModelName");
                spinnerItems.add(modelName);
                ModelNames.add(modelName);
            } catch (Exception e) {
                continue;
            }
        }
        ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, spinnerItems);
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spModelList1.setAdapter(spinnerAdapter);
    }
반응형

'SWDesk > App' 카테고리의 다른 글

RecyclerVieiwTest02  (0) 2021.11.06
[Android] JSON String from Web  (0) 2021.10.09
[Android] HTTP POST Request  (0) 2021.08.22
[Android] Displaying AlertDialog  (0) 2021.08.14
[Android] Generating Beep  (0) 2021.07.31