使用FOR循环汇总和改进ArrayAdapter

时间:2016-09-30 08:46:34

标签: java android arrays for-loop android-arrayadapter

我想用循环

来总结我的代码

例如,如果我有这样的代码:

spinnerDinamik.addView(txtKolom[0]);
spinnerDinamik.addView(spnKolom[0]);
spinnerDinamik.addView(txtKolom[1]);
spinnerDinamik.addView(spnKolom[1]);
spinnerDinamik.addView(txtKolom[2]);
spinnerDinamik.addView(spnKolom[2]);

我可以这么简单:

for (int itung=0;itung<labelKolom.size();itung++){
  spinnerDinamik.addView(txtKolom[itung]);
  spinnerDinamik.addView(spnKolom[itung]);
}

但在我的问题上,我想将这个循环用于我的数组适配器。

首先,我声明我的数组适配器:

ArrayAdapter<String>[] dataAdapterKolom;
dataAdapterKolom = new ArrayAdapter[labelKolom.size()];

我制作了这样的代码:

final List<String> labelJenis = db.getJenis();
final List<String> labelTipe = new ArrayList<String>();
final List<String> labelKode = new ArrayList<String>();

dataAdapterKolom[0] = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item, labelJenis);
dataAdapterKolom[1] = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item, labelTipe);
dataAdapterKolom[2] = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item, labelKode);

for (int itung=0;itung<labelKolom.size();itung++){
 dataAdapterKolom[itung].setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 spnKolom[itung].setAdapter(dataAdapterKolom[itung]);
}

我的期望

在此代码中:

final List<String> labelJenis = db.getJenis();
final List<String> labelTipe = new ArrayList<String>();
final List<String> labelKode = new ArrayList<String>();

我想将final List<String>转换为数组,并使用for循环。

然后我可以更改此代码:

dataAdapterKolom[0] = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item, labelJenis);
dataAdapterKolom[1] = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item, labelTipe);
dataAdapterKolom[2] = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item, labelKode);

进入此代码:

for (int itung=0;itung<labelKolom.size();itung++){
  dataAdapterKolom[itung] = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item, labelKolom[itung]);
}

这是我的完整代码

public class MainActivity  extends Activity{

    TextView[] txtKolom;
    Spinner[] spnKolom;
    ArrayAdapter<String>[] dataAdapterKolom;
    ViewGroup spinnerDinamik;

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

        spinnerDinamik = (ViewGroup) findViewById(R.id.bikinLayout);

        final DataHelper db = new DataHelper(getApplicationContext());
        final List<String> labelKolom = db.getKolom();

        txtKolom = new TextView[labelKolom.size()];
        spnKolom = new Spinner[labelKolom.size()];
        dataAdapterKolom = new ArrayAdapter[labelKolom.size()];

        for (int itung=0;itung<labelKolom.size();itung++){
            txtKolom[itung] = new TextView(this);
            txtKolom[itung].setText(labelKolom.get(itung).toString());
            spnKolom[itung] = new Spinner(this);
            spinnerDinamik.addView(txtKolom[itung]);
            spinnerDinamik.addView(spnKolom[itung]);
        }

        final List<String> labelJenis = db.getJenis();
        final List<String> labelTipe = new ArrayList<String>();
        final List<String> labelKode = new ArrayList<String>();

        dataAdapterKolom[0] = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item, labelJenis);
        dataAdapterKolom[1] = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item, labelTipe);
        dataAdapterKolom[2] = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item, labelKode);

        for (int itung=0;itung<labelKolom.size();itung++){
            dataAdapterKolom[itung].setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spnKolom[itung].setAdapter(dataAdapterKolom[itung]);
        }

        spnKolom[0].setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                labelTipe.clear();
                List<String> labelTipeIsi = db.getTipe(labelJenis.get(i).toString());

                for (String loop : labelTipeIsi) {
                    labelTipe.add(loop);
                }

                dataAdapterKolom[1].notifyDataSetChanged();
                dataAdapterKolom[2].notifyDataSetChanged();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });
        spnKolom[1].setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                labelKode.clear();
                List<String> labelKodeIsi = db.getKode(spnKolom[0].getSelectedItem().toString(), labelTipe.get(i).toString());
                for (String looop : labelKodeIsi) {
                    labelKode.add(looop);
                }
                dataAdapterKolom[2].notifyDataSetChanged();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });
    }
}

0 个答案:

没有答案