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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:27:46+00:00 2026-05-29T23:27:46+00:00

I have what I consider to be a strange dilemma, although YMMV. I’m using

  • 0

I have what I consider to be a strange dilemma, although YMMV.

I’m using a layout file that describes each line/row in a ListView (nothing too exotic about that). I have an id assigned to each one, such as:

android:id=”@+id/checkBox1″
android:id=”@+id/checkBox2″
android:id=”@+id/checkBox3″
android:id=”@+id/contactLabel” // a TextView

Now this doesn’t seem to make sense, as these ids should be unique, so what is the id of the second
row? That is, if “row 1” honors the specified ids of checkbox1, checkbox2, checkbox3, and contactLabel, what would the “row 2” ids be?

I’m curious, but also I need to know because I want to save the values of the checkboxes to a SharedPreferences object.

Who has a clue about how to get around this?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Update

The first thing I need to solve is how to respond to a click on the ListView. This is my current conundrum related to all of this:

ListView doesn’t know it’s been clicked, or won’t tell me

I’ve added this event handler to my ListActivity:

  @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        String item = (String) getListAdapter().getItem(position);
        Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
    }

…but it’s not getting called. I click on the Contacts that display, but no go.

I also tried it this way:

lv.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
        Toast.makeText(getApplicationContext(),
            "Click ListItem Number " + position, Toast.LENGTH_LONG)
            .show();
    }
});

…still no joy…I put breakpoints on both “Toast” lines, and they never get reached.

I read here:

http://www.vogella.de/articles/AndroidListView/article.html#listsactivity_layout

…that, “In case you need more the just a ListView in your Activity, you can use you own layout for
ListActivity.”

…which I do, because I add a header and a footer in addition to the listview.

It goes on to say, “In this case your layout must have an ListView element with the android:id
attribute set to @android:id/list.”

So I added this to my layout:

…but it makes no difference one way or the other.

  • 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-29T23:27:49+00:00Added an answer on May 29, 2026 at 11:27 pm

    The ID’s for the items within the ListView widget are referenced through their parent view when you inflate it in your getView() method.

    To elaborate, you would have something like this is you ListView adapter.

    public View getView(int position, View convertView, ViewGroup viewGroup) {
            if (convertView == null) {
                convertView = LayoutInflater.from(ctx).inflate(R.layout.list_view_item, null);
                }
    

    Nown, a new view instance exists as the convertView. You can access your widgets using covertView.findViewById(R.id.checkBox1), convertView.findViewById(R.id.checkBox2), etc.

    Each of these views is a child of your ListView. You can reference each individual view from your ListView using the getChildCount() and getChildAt() methods from the ListView object.
    However, since it is recommended to use the convertView view to recycle views, in that case you will only have reference to the views on screen at a time.

    Also, with regards to the SharedPreferences, all the views in your ListView are populated by an Adapter subclass which would be the actual object that puts the values in the Checkbox and TextView widgets. This Adapter has a dataset that you provide it. Why not reference the values from the dataset directly, instead of trying to find them from the list items which are populated from the dataset in any case ? You can write to the dataset from the ListView when someone clicks a CheckBox so you have an easy ordered reference to all the items in the ListView.


    UPDATE: Added dummy source code

    OK. Let’s start with a hypothical list. We want to display say five items on the list. For simplicity, I’ll assume each has a TextView and a Checkbox. So my container class is:

    class Item {
        String textView;
        boolean checked;
    }
    

    Now in my Activity where I want to display this list, I put an ArrayList of items (you can use just about any datastructure) as a class variable. Then I get the ListView reference and assign it an adapter.

    class MyActivity extends Activity {
        ArrayList<Item> listItems;
        .....
        onCreate(Bundle icicle) {
            .....
            ListView listView = (ListView) findViewById(R.id.listView1); // this will be you list view
            MyAdapter listAdapter = new MyAdapter();
            listView.setAdapter(listAdapter);
            ....
            // Rest of your Activity
            ....
            MyAdapter extends BaseAdapter {
    
                int getItemCount() {
                    return listItems.size();
                }
    
                Item getItem(int position) {
                    return listItems.get(position);
                }
    
                View getView(int position, View convertView, ViewGroup parent) {
                    // Here's the important part
                    Item currentItem = listItems.getItem(position); // Since the array is a class variable, you can do either get or getItem
    
                    ..... // do the standard individual item inflating ....
    
                    checkbox = convertView.findViewById(R.id.checkbox);
                    checkbox.OnItemSelectedListener( new OnItemSelectedListener() { // or whatever listener there should be... I didn't check
                        ... // do whatever...
                        currentItem.setChecked(true);
                }
    

    When you want to retrieve what items were clicked, just iterate through the Item class and find which ones are true or you perform whatever action you want within the Listener since you have a reference identifying individual members of the ListView dataset (here listItems ArrayList).

    Apologies for any errors. Didn’t do any checking.

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

Sidebar

Related Questions

Let's consider we have QWidget that contains QTableWidget (only). So we want to resize
I'm experiencing what I would consider somewhat strange behavior. Specifically if I have a
I have noticed a strange behaviour when intercepting the preUpdate row hook in Propel
I have a strange problem in matching a pattern. Consider the Perl code below
I am getting some strange behavior involving database queries that I have never seen
I have discovered a strange problem when using UIActionSheet on the iPhone (iOS 4.2).
I am writing a program that will open an image file, but strange thing
Consider have this line of code: List<HtmlGenericControl> listOfDivs = this.clientGrid.Controls.OfType<HtmlGenericControl>() .ToList<HtmlGenericControl>(); The OfType<>(); isn't
I am getting strange error when i run Service.svc file : I have used
I have a client that gave me a .psd file that contains the entire

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.