CheckBox [] with onClickListener []?

EDIT: This question is mostly closed, and only the following issues with this code are discussed here

For part of my application, I have a page of elements that are presented as flags, each of which has a logical value associated with it, which ultimately is collected and saved as a string as follows:

final CheckBox gas_oil = (CheckBox) findViewById(R.id.gas_oil);
gas_oil.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
    {
    if (gas_oil.isChecked())
        {
        impacts.append(getString(R.string.gas_oil) + " | ");
        anythingchecked = true;
        }
    }
});

This is very tiring and does not seem to be a very effective way to do this, since I have 9 or 10 items that users can check or not. also this method means that if they press and squeeze something, this element is still in StringBuilder impactsand that if they press it again, it will be there twice.

My solution was to have everything in arrays:

  String[] impactsn = getResources().getStringArray(R.array.impacts);
  final boolean[] impactsb = new boolean[impactsn.length];
  final CheckBox[] impactsc = new CheckBox[impactsn.length];
  View[] impactsv = new View[]{findViewById(R.id.gas_oil),findViewById(R.id.ghost_fishing),findViewById(R.id.marsh_damage),findViewById(R.id.nav_haz),findViewById(R.id.shell_damage),findViewById(R.id.waste_pollution),findViewById(R.id.wild_entang),findViewById(R.id.other)};

  for (int i = 0; i < impactsn.length; i++)
    {
    impactsc[i] = (CheckBox) impactsv[i];
    impactsc[i].setOnClickListener(new OnClickListener()
            {   
            @Override
            public void onClick(View v)
                {
                if (impactsc[i].isChecked())
                    impactsb[i] = true;
                else
                    impactsb[i] = false;
                }
            });
    }

, , ( ) OnClickListener . , , i , .

/ OnClickListeners? , ?

, , , , :

String getImpacts ()
    {
            String[] impactsn = getResources().getStringArray(R.array.impacts);
    StringBuilder impactss = new StringBuilder();
    for (int i = 0; i < impactsn.length; i ++)
        {
        if (impactsb[i])
                    impactss.append(impactsn[i] + " | ");
        }
    return String.valueOf(impactss);
    }

: im, :

com.citsci.mardeb;

import android.app.Activity; import android.content.res.Resources; import android.os.Bundle; android.view.View; import android.widget.CheckBox; import android.widget.EditText;

public class Impacts extends Activity View.OnClickListener {   int length = 7;   boolean [] impactb = new boolean [] {false, false, false, false, false, false, false, false};    EditText;

public void onCreate (Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.impacts);

//for (int = 0; < length; ++) //impactb [i] = false;

  View[] impactsv = new View[]
     {
    findViewById(R.id.gas_oil),
        findViewById(R.id.ghost_fishing),
        findViewById(R.id.marsh_damage),
        findViewById(R.id.nav_haz),
        findViewById(R.id.shell_damage),
        findViewById(R.id.waste_pollution),
        findViewById(R.id.wild_entang),
        findViewById(R.id.other)
        };

  CheckBox[] impactsc = new CheckBox[length];

  for (int i = 0; i < length; i++)
     {
     impactsc[i] = (CheckBox) impactsv[i];
     impactsc[i].setOnClickListener(this);
     }

}// end of onCreate

@Override
public void onClick(View v)
    {
  switch (v.getId()) {
     case (R.id.gas_oil):
        impactsb[0] =! impactsb[0];
        break;
     case (R.id.ghost_fishing):
        impactsb[1] =! impactsb[1];
        break;
     case (R.id.marsh_damage):
        impactsb[2] =! impactsb[2];
        break;
     case (R.id.nav_haz):
        impactsb[3] =! impactsb[3];
        break;
     case (R.id.shell_damage):
        impactsb[4] =! impactsb[4];
        break;
     case (R.id.waste_pollution):
        impactsb[5] =! impactsb[5];
        break;
     case (R.id.wild_entang):
        impactsb[6] =! impactsb[6];
        break;
     case (R.id.other):
        impactsb[7] =! impactsb[7];
        }
    }

String getImpacts ()
    {
    String[] impactsn = new String[length];
    Resources myResources = getResources();
    impactsn = myResources.getStringArray(R.array.impacts);
  StringBuilder impactss = new StringBuilder();
    for (int i = 0; i < length; i ++)
        {
        if (impactsb[i])
            impactss.append(impactsn[i] + " | ");
        }
    if (String.valueOf(impactss) != "")
        impactss.insert(0, "Impacts: ");
    return String.valueOf(impactss);
    }
}// end of Impacts.class
+3
3

() . ...

public class MyActivity extends Activity
    implements View.OnClickListener {

    ...

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case impactsv[0].getId:
                impactsb[0] = !impactsb[0];
                ...
                break;

            // Add other cases here

        }

    }
}

, , , ...

impactsc[i].setOnClickListener(this);

, CheckBox , , ...

onClick(View v)

+3

, , , . , , , , , , chsckbox . , isSelected().

, , .

+1

, :

mainA {   CheckBox [] chbx; apply;

public void OnCreate(Bundle bundle)
{   super.onCreate(bundle);
    setMyChBx();apply=new Button(this);
    apply.setOnClickListener(new View.OnClickListener()
{   public void onClick(View v)
    {   if(chbx[0].isSelected()) setsomthing0();
        if(chbx[1].isSelected()) setsomthing1();
        if(chbx[2].isSelected()) setsomthing2();
        if(chbx[3].isSelected()) setsomthing3(); // and so on
    }
});
}

public void setMyChBx()
{   chbx=new CheckBox[25];
    for(int i=0;i<25;i++)
    {   chbx[i]=new CheckBox(this);
        chbx[i].setTag(new Integer(i));//tags for the checkboxes simply from 0 to 24 (24 included)
        chbx[i].setOnClickListener(new chBxOnclick());
    }

}
/*
 * here you have three options to create one onclick method to all of them or to create many onclicks for each on of them
 * or don't create onclick method for the checkboxes just check the state once it all done like above
 * example for onclick for all of them
 */

public class chBxOnclick implements OnClickListener
{   public void onClick(View v)
{   switch((Integer)v.getTag())   
    {   case 0: dosomething();break;
        case 1: dothis();break;
        case 2: dathat(); break;
        .
        .
        .
        case 24: doFor24();break;
    }
}

}

}

+1

All Articles