dimanche 9 août 2015

How to make shorter code

This is part of code from my first application. At the moment I have added just 2 TextViews with default value gab, where clicking on them popups PopupMenu with 6 values. By clicking any value in PopupMenu, value of TextView changes to the clicked value (not changing other not clicked TextView values). I am planning to add like 100 or maybe even more this kind of TextViews. Writing, copying and editing ID's and etc for each would be too messy.

How this could be made shorted? Amount of values won't change (6), amount of ViewId (for example augli_mervieniba and bites_mervieniba) will be huge.

    public void popup_mervieniba(final View view)
    {
        PopupMenu popupMenu = new PopupMenu(this, view);
        MenuInflater menuInflater = popupMenu.getMenuInflater();
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {

This part is for that if first button is clicked

              if (view==findViewById(R.id.augli_mervieniba))

                    switch (item.getItemId()) {
                        case R.id.gab:
                            TextView t_gab = (TextView) findViewById(R.id.augli_mervieniba);
                            t_gab.setText(R.string.gab);
                            return true;
                        case R.id.iep:
                            TextView t_iep = (TextView) findViewById(R.id.augli_mervieniba);
                            t_iep.setText(R.string.iep);
                            return true;
                        case R.id.g:
                            TextView t_g = (TextView) findViewById(R.id.augli_mervieniba);
                            t_g.setText(R.string.g);
                            return true;
                        case R.id.kg:
                            TextView t_kg = (TextView) findViewById(R.id.augli_mervieniba);
                            t_kg.setText(R.string.kg);
                            return true;
                        case R.id.ml:
                            TextView t_ml = (TextView) findViewById(R.id.augli_mervieniba);
                            t_ml.setText(R.string.ml);
                            return true;
                        case R.id.l:
                            TextView t_l = (TextView) findViewById(R.id.augli_mervieniba);
                            t_l.setText(R.string.l);
                            return true;
                }

This part is for that if second button is clicked

            else if (view==findViewById(R.id.bietes_mervieniba))
                    switch (item.getItemId()) {
                        case R.id.gab:
                            TextView t_gab = (TextView) findViewById(R.id.bietes_mervieniba);
                            t_gab.setText(R.string.gab);
                            return true;
                        case R.id.iep:
                            TextView t_iep = (TextView) findViewById(R.id.bietes_mervieniba);
                            t_iep.setText(R.string.iep);
                            return true;
                        case R.id.g:
                            TextView t_g = (TextView) findViewById(R.id.bietes_mervieniba);
                            t_g.setText(R.string.g);
                            return true;
                        case R.id.kg:
                            TextView t_kg = (TextView) findViewById(R.id.bietes_mervieniba);
                            t_kg.setText(R.string.kg);
                            return true;
                        case R.id.ml:
                            TextView t_ml = (TextView) findViewById(R.id.bietes_mervieniba);
                            t_ml.setText(R.string.ml);
                            return true;
                        case R.id.l:
                            TextView t_l = (TextView) findViewById(R.id.bietes_mervieniba);
                            t_l.setText(R.string.l);
                            return true;
                }

end of code

return false;
            }
        });
        menuInflater.inflate(R.menu.popup_mervieniba, popupMenu.getMenu());
        popupMenu.show();
    }

Aucun commentaire:

Enregistrer un commentaire