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 8216115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:01:12+00:00 2026-06-07T12:01:12+00:00

I am working on a sample app to learn android. It is supposed to

  • 0

I am working on a sample app to learn android. It is supposed to display a multiple select list and when the user clicks on the button it should display the items selected in a TextView at the top. It seems that when I try to retrieve the selected items from the ListView an empty array is always being returned. Can anyone explain to me why this is the case and what I am missing to make it work correctly? (FYI the code is a modified program from Beginning Android 4 by Grant Allen)

Here is the layout xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView
        android:id="@+id/selection"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/getSelections"
        android:text="Show Selections"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="listSelected" />
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false"
        android:choiceMode="multipleChoice" />
</LinearLayout>

And here is the associated Java:

package com.commonsware.android.checklist;

import android.os.Bundle;
import android.app.ListActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.view.*;

public class ChecklistDemo extends ListActivity {
    private static final String[] items={"lorem", "ipsum", "dolor",
                    "sit", "amet",
                    "consectetuer", "adipiscing", "elit", "morbi", "vel",
                    "ligula", "vitae", "arcu", "aliquet", "mollis",
                    "etiam", "vel", "erat", "placerat", "ante",
                    "porttitor", "sodales", "pellentesque", "augue", "purus"};

    private TextView selection;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        setListAdapter(new ArrayAdapter<String>(this,
                                                android.R.layout.simple_list_item_multiple_choice,
                                                items));
        selection = (TextView)findViewById(R.id.selection);

    }

    public void listSelected(View view) {
        long[] chosenOnes = getListView().getCheckedItemIds();
        selection.setText("Items selected: ");
        selection.setText(Integer.toString(chosenOnes.length));
        for (long x:chosenOnes) {
            int i = Long.valueOf(x).intValue();
            selection.setText(selection.getText() + ", " + items[i]);           
        }

    }

}
  • 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-07T12:01:13+00:00Added an answer on June 7, 2026 at 12:01 pm

    See the doc, the method getCheckedItemIds() returns a valid result only if your adapter has stable ids. Instead you can use

    SparseBooleanArray array = this.getListView().getCheckedItemPositions();

    Hope this will help you =)

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

Sidebar

Related Questions

I was working on a simple bubblewrap app in order to learn the android
I'm working on a pretty simple Java app in order to learn more about
I am working on a sample map kit app for iOS. I have everything
I am a beginner to the iOS app development and working on a sample
I'm working on a simple XML phonebook app to learn JQuery, and I can't
I am working on a sample app with primefaces 3 , using datatables incell
I am working on one sample app in which I need to use Google
I'm a complete newb to Django working on a sample app. It appears that
I am working with Bluetooth Chat sample code. In sample app the Device opens
I'm working on a simple app to learn Cocoa and I've encountered a little

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.