EDIT: this question is mostly closed and the only problems i have with this code are discussed here
For part of my app, I have a page of items that are represented as checkboxes, each with a associated boolean that eventually get collected and stored 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 extremely tedious and did not seem to be a very efficent way to do this since i have 9 or 10 items that users can check or not. also this method means that if they click and unclick something, that item is still in the StringBuilder impacts
and that if they click it again then it is in 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;
}
});
}
unfortunately doing this causes the problem that (as far as i understand it) things within an OnClickListener
have to be final. With the code as written, i
can never be final, so I'm sort of at a standstill.
Should/can I have an array of OnClickListeners
as well? Should I be calling to a method outside of the code I have here?
Also, below is the getter i was planning on using, I think that part will work just fine:
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);
}
EDIT: this is the version of code im running with now:
package com.citsci.mardeb;
import android.app.Activity; import android.content.res.Resources; import android.os.Bundle; import android.view.View; import android.widget.CheckBox; import android.widget.EditText;
public class Impacts extends Activity implements View.OnClickListener { int length = 7; boolean[] impactsb = new boolean[] {false, false, false, false, false, false, false, false}; EditText view;
public void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.impacts);
// for (int i = 0; i < length; i++) // impactsb[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[leng开发者_Python百科th];
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
Don't use anonymous (inline) listeners in this case. Instead have your Activity implement the listener...
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
}
}
}
Then all you need to do to set the listener is...
impactsc[i].setOnClickListener(this);
Then test for which CheckBox has been clicked by using the View which is passed to...
onClick(View v)
first you don't have to have them as final, you can declare them outsid of the function then you won't be asked to declare them as final. second if you need to know the state of the checkboxes as they are changed you can add a listener to each one of them with different task of course, you can do it with a loop, and then this loop will call to another function that will have the tag of this chsckbox, and will react according to this tag. If you don't need to know the state when they are changed you can check the state of them once you are done with this screen, with isSelected() method.
Hope this helps, Best regards.
Here you go a complete code for this just fill between for your needs :
public class mainA extends Activity { CheckBox[] chbx;Button 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's 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;
}
}
}
}
精彩评论