2017-10-09 4 views
0

とアンドロイドスピナーvalus方法は何がしたいことは同じことを行うことです、コーディングを減らすために、私は別の方法やsprate方法を意味し、これを実行するが、別の方法であるが、diffreent方法で別の方法

public static final int spinner1_op_val = 50; 
public static final int spinner2_op_val = 10; 
public static final int spinner3_op_val = 50; 

    Spinner spinner1 = (Spinner)findViewById(R.id.spinner1); 
    ArrayList<Integer> spinner1_options = new ArrayList<>(); 
    for (i = 0; i <= spinner1_op_val; i += 1) { 
     spinner1_options.add(Integer.valueOf(i)); 
    } 
    ArrayAdapter<Integer> dataAdapter_for_spinner1 = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, spinner1_options); 
    dataAdapter_for_spinner1.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item); 
    spinner1.setAdapter(dataAdapter_for_spinner1); 
    spinner1.setSelection(4); 
    spinner1.setOnItemSelectedListener(this); 





    Spinner spinner2 = (Spinner) findViewById(R.id.spinner2); 
    ArrayList<Integer> spinner2_options = new ArrayList<>(); 
    for (i = 0; i <= spinner2_op_val; i++) { 
     spinner2_options.add(Integer.valueOf(i)); 
    } 
    ArrayAdapter<Integer> dataAdapter_for_spinner2 = new ArrayAdapter<>(this,R.layout.support_simple_spinner_dropdown_item,spinner2_options); 
    dataAdapter_for_spinner2.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item); 
    spinner2.setSelection(2); 
    spinner2.setAdapter(dataAdapter_for_spinner2); 
    spinner2.setOnItemSelectedListener(this); 



    Spinner spinner3 = (Spinner) findViewById(R.id.spinner3); 
    ArrayList<Integer> spinner3_option = new ArrayList<>(); 
    for (i = 0; i <= spinner3_op_val; i += 5) { 
     spinner3_option.add(Integer.valueOf(i)); 
    } 
    ArrayAdapter<Integer> dataAdapter_for_spinner3 = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, spinner3_option); 
    dataAdapter_for_spinner3.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item); 
    spinner3.setAdapter(dataAdapter_for_spinner3); 
    spinner3.setSelection(2); 
    spinner3.setOnItemSe 

    lectedListener(this); 

エクストラエクストラエキストラエキスエキステキストエキスパートエキスパート

答えて

0

はい、このような機能を作成するだけでコードを最小限に抑えることができます。その後、

public void setSpinnerItem(Spinner spinner, int value, int jump, int selection) { 
    ArrayList<Integer> spinnerOption = new ArrayList<>(); 
    for (int i = 0; i < value; i+=jump) { 
     spinnerOption.add(Integer.valueOf(i)); 
    } 
    ArrayAdapter<Integer> integerArrayAdapter = new ArrayAdapter<Integer>(this, R.layout.support_simple_spinner_dropdown_item, spinnerOption); 
    integerArrayAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item); 
    spinner.setAdapter(integerArrayAdapter); 
    spinner.setSelection(selection); 
    spinner.setOnItemSelectedListener(this); 
} 

機能を使用します。

Spinner spinner1 = (Spinner)findViewById(R.id.spinner1); 
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2); 
Spinner spinner3 = (Spinner) findViewById(R.id.spinner3); 

setSpinnerItem(spinner1, spinner_op_val, forLoopJump, spinnerSelection); 
setSpinnerItem(spinner2, spinner_op_val, forLoopJump, spinnerSelection); 
setSpinnerItem(spinner3, spinner_op_val, forLoopJump, spinnerSelection); 
関連する問題