Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6008231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:47:42+00:00 2026-05-23T01:47:42+00:00

EDIT: this question is mostly closed and the only problems i have with this

  • 0

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[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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:47:43+00:00Added an answer on May 23, 2026 at 1:47 am

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(EDIT: This question is now outdated for my particular issue, as Google Code supports
Edit: This question was written in 2008, which was like 3 internet ages ago.
EDIT: This question is more about language engineering than C++ itself. I used C++
EDIT : This question duplicates How to access the current Subversion build number? (Thanks
I'm using markdown to edit this question right now. In some wikis I used
[EDIT] Hmm. Perhaps this question should be titled what is the default user-input dialog
Edit: This was accidentally posted twice. Original: VB.NET Importing Classes I've seen some code
Edit: This code is fine. I found a logic bug somewhere that doesn't exist
So this is probably a fairly easy question to answer but here goes anyway.
EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.