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

  • Home
  • SEARCH
  • 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 7843327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:31:25+00:00 2026-06-02T16:31:25+00:00

Recently, I encountered a problem with a ListView. In it there is a list

  • 0

Recently, I encountered a problem with a ListView.
In it there is a list of checkboxes. The checked items are fed to the calling Activity that starts my ListActivity with startActivityForResult. If there were any checked items previous to calling the ListActivity, those items are passed to it so the user can edit his previous selection rather than starting over. First time around everything is fine. But when I want to alter the selection by calling the ListActivity again, the first checkbox in the list looks like it is checked if any of the items in the list are checked. In fact it is not recognized as checked, i.e. the code checking the already selected items doesn’t fire when it processes the first item and after saving the selection again, the first item is not part of the list returned to the parent as long as it has not explicitly been checked (via unchecking and then checking it again).

When I searched for this problem, I found out that the first item of a ListView receives focus, so I tried

listView.setItemsCanFocus(false);

to no effect. Actually I would have been surprised if it had worked, because the item is only checked when there is a non-empty selection. As I found no question that addresses my problem, I boiled it down to an example that still produces the effect described. Sorry for all the code, but I’m at a total loss at what to look for, so I don’t know how I could describe my problem by posting less.

onCreate and onActivityResult of the calling activity:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    listItems = new ArrayList<String>();
    list = (TextView) findViewById(R.id.list);
    list.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Bundle extras = new Bundle();
            extras.putStringArrayList("item_list", listItems);
            Intent i = new Intent(Main.this, CheckBoxListActivity.class);
            i.putExtras(extras);
            startActivityForResult(i,1);
        }
    });

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    Bundle extras = new Bundle();
    extras = intent.getExtras();
    String[] strings = extras.getStringArray("item_list");
    String text = "";

    listItems.clear();
    for ( String item : strings ) {
        text = text + item + ", ";
        listItems.add(item);
    }
    if ( text.length() != 0 ) {
        text = text.substring(0, text.length() - 2);
    } else {
        text = "(nothing here)";
    }
    list.setText(text);
}

onCreate() and finish() in the ListActivity:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.checkbox_list);
    listView = getListView();

    save = (Button) findViewById(R.id.save_button);
    save.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.i(LOG_TAG, "save button clicked");
            finish();
        }
    });

    ArrayList<String> itemList = new ArrayList<String>();
    itemList.add( "foo" );
    itemList.add( "bar" );
    itemList.add( "baz" );
    itemList.add( "qux" );
    itemList.add( "quux" );

    checkedItems = getIntent().getExtras().getStringArrayList("item_list");
    ArrayAdapter<String> arrayAdapter = new ItemsAdapter(this, R.layout.checkbox_list, itemList);
    listView.setAdapter(arrayAdapter);

}

@Override
public void finish() {
    Bundle extras = new Bundle();
    String[] stringArray = new String[checkedItems.size()];
    for ( int i = 0; i < checkedItems.size(); i++) {
        stringArray[i] = checkedItems.get(i);
    }

    extras.putStringArray("item_list", stringArray);
    Intent mIntent = new Intent();
    mIntent.putExtras(extras);
    setResult(RESULT_OK, mIntent);
    super.finish();
}

And the ArrayAdapter:

private class ItemsAdapter extends ArrayAdapter<String> {

    public ItemsAdapter(Context context, int textViewResourceId, List<String> items) {
        super(context, textViewResourceId, items);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.checkbox_list_row, null);
        }
        String entry = getItem(position);
        v.setTag(entry);
        fillText(v, R.id.checkbox, entry);
        return v;
    }

    private void fillText(View v, int id, String text) {
        Log.i(LOG_TAG, "this is fillText, our text is: " + text);

        CheckBox checkBox = (CheckBox) v.findViewById(id);
        if ( checkedItems.contains(text)) {
            Log.i(LOG_TAG, "item has to be checked");
            checkBox.setChecked(true);
        }
        checkBox.setText(text == null ? "" : text);
        checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton cb, boolean isChecked) {
                String tag = (String) cb.getTag();
                if (cb.isChecked()) {
                    // add item to set of checkedItems
                    if ( !checkedItems.contains(tag)) {
                        checkedItems.add(tag);
                        Log.i(LOG_TAG, "checkedItems: " + checkedItems.toString());
                    }
                } else {
                    // remove item from set of checkedItems
                    if ( checkedItems.contains(tag)) {
                        checkedItems.remove((String) cb.getTag());
                        Log.i(LOG_TAG, "checkedItems: " + checkedItems.toString()); 
                    }
                }
            }
        }); 
    }
}
  • 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-06-02T16:31:27+00:00Added an answer on June 2, 2026 at 4:31 pm

    You need to be aware that the checkbox from a convert view might be checked. You need to uncheck it if the text is not in the list.

    private void fillText(View v, int id, String text) {
            Log.i(LOG_TAG, "this is fillText, our text is: " + text);
    
            CheckBox checkBox = (CheckBox) v.findViewById(id);
            if ( checkedItems.contains(text)) {
                Log.i(LOG_TAG, "item has to be checked");
                checkBox.setChecked(true);
            } else {
                checkBox.setChecked(false);     
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently I've encountered a problem. I have an app that talks to the server.
I recently encountered a problem where a value was null if accessed with Request.Form
I recently encountered a problem with my Profile provider: it wouldn't retrieve profiles correctly
I recently encountered an odd problem with RSACryptoServiceProvider.VerifyHash . I have a web application
Developing a heavily XML-based Java-application, I recently encountered an interesting problem on Ubuntu Linux.
I've recently begun learning C# but have encountered an annoying problem. Every variable I
I recently encountered a situation in some code I am working on that doesn't
I recently encountered an index in a database I maintain that was of the
We recently encountered a problem where a merge somehow led to all the changes
I recently encountered a problem in my production database due to MySQL bug. The

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.