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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:29:47+00:00 2026-05-25T18:29:47+00:00

I am trying to display a list with checkboxes using a preference activity. The

  • 0

I am trying to display a list with checkboxes using a preference activity. The list is supplied with 121 entries and by debugging I have confirmed that the complete list is passed into the ListPreference. However the scrollable list ends up with the first 4 entries repeated to the size of the entries i.e. 121. I found code on here which is as follows. Stepping through with the debugger it seems that CustomHolder(View row, int position) is supplied with position as first 0 then 2 and so on to 3 at which point it appears as 0 again. I cant see where and why this is happening so.

First a preference.xml

<?xml version="1.0" encoding="utf-8"?>


<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
 <PreferenceCategory
            android:title="Your Title">

<com.Applitastic.CustomListPreference
    android:key="multipref"
    android:id="@+id/multiselectlist"
    android:title="Apps to exclude" android:summary="Specify exclusion"
    android:dialogTitle="Apps to exclude" android:defaultValue="1"/>

</PreferenceCategory>
</PreferenceScreen>

Now an xml for a tablelayout

<?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"
android:paddingBottom="8dip"
android:paddingTop="8dip"
android:paddingLeft="10dip"
android:paddingRight="10dip">

<TableLayout
    android:id="@+id/custom_list_view_row_table_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="0">

    <TableRow
        android:id="@+id/custom_list_view_row_table_row"
        android:gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/custom_list_view_row_text_view"
            android:textSize="22sp"
            android:textColor="#000000"  
            android:gravity="center_vertical"
            android:layout_width="160dip" 
            android:layout_height="40dip" />

        <RadioButton
            android:checked="false"
            android:id="@+id/custom_list_view_row_radio_button"/>
    </TableRow>
</TableLayout>

Now the ListPreference code.

import java.util.ArrayList;

import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.preference.ListPreference;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TextView;
import android.app.AlertDialog.Builder;
 import android.app.Dialog;

public class CustomListPreference extends ListPreference{
CustomListPreferenceAdapter customListPreferenceAdapter = null;
Context mContext;
private LayoutInflater mInflater;
CharSequence[] entries;
CharSequence[] entryValues;
ArrayList<RadioButton> rButtonList;
SharedPreferences prefs;
SharedPreferences.Editor editor;

public CustomListPreference(Context context, AttributeSet attrs)
{
    super(context, attrs);
    mContext = context;
    mInflater = LayoutInflater.from(context);
    rButtonList = new ArrayList<RadioButton>();
    prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
    editor = prefs.edit();
}

@Override
protected void onPrepareDialogBuilder(Builder builder)
{
    entries = getEntries();
    entryValues = getEntryValues();

    if (entries == null || entryValues == null || entries.length != entryValues.length       
)
    {
        throw new IllegalStateException(
                "ListPreference requires an entries array and an entryValues array
which are both the same length");     
    }

    customListPreferenceAdapter = new CustomListPreferenceAdapter(mContext);

    builder.setAdapter(customListPreferenceAdapter, new   
DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int which)
        {

        }
    });
}

private class CustomListPreferenceAdapter extends BaseAdapter
{        
    public CustomListPreferenceAdapter(Context context)
    {

    }

    public int getCount()
    {
        return entries.length;
    }

    public Object getItem(int position)
    {
        return position;
    }

    public long getItemId(int position)
    {
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent)
    {  
        View row = convertView;
        CustomHolder holder = null;

        if(row == null)
        {                                                                   
            row = mInflater.inflate(R.layout.excludeapps, parent, false);
            holder = new CustomHolder(row, position);
            row.setTag(holder);

            // do whatever you need here, for me I wanted the last item to be greyed 
out and unclickable
           // if(position != 3)
            //{
                row.setClickable(true);
                row.setOnClickListener(new View.OnClickListener()
                {
                    public void onClick(View v)
                    {
                        for(RadioButton rb : rButtonList)
                        {
                            if(rb.getId() != position)
                                rb.setChecked(false);
                        }

                        int index = position;
                       // int value = Integer.valueOf((String) entryValues[index]);
                        //editor.putInt("yourPref", value);

                        Dialog mDialog = getDialog();
                        mDialog.dismiss();
                    }
                });
           // }
        } else {
               holder = (CustomHolder) convertView.getTag();
        }

        return row;
    }

    class CustomHolder
    {
        private TextView text = null;
        private RadioButton rButton = null;

        CustomHolder(View row, int position)
        {    
            text = (TextView)row.findViewById(R.id.custom_list_view_row_text_view);
            text.setText(entries[position]);
            rButton =  
(RadioButton)row.findViewById(R.id.custom_list_view_row_radio_button);
            rButton.setId(position);

            // again do whatever you need to, for me I wanted this item to be greyed 
out and unclickable
            //if(position == 3)
            //{
             //   text.setTextColor(Color.LTGRAY);
              //  rButton.setClickable(false);
            //}

            // also need to do something to check your preference and set the right 
button as checked

            rButtonList.add(rButton);
            rButton.setOnCheckedChangeListener(new 
CompoundButton.OnCheckedChangeListener()
            {
                public void onCheckedChanged(CompoundButton buttonView, boolean 
isChecked)
                {
                    if(isChecked)
                    {
                        for(RadioButton rb : rButtonList)
                        {
                            if(rb != buttonView)
                                rb.setChecked(false);
                        }

                        int index = buttonView.getId();
                        //int value = Integer.valueOf((String) entryValues[index]);
                        //editor.putInt("yourPref", value);

                        Dialog mDialog = getDialog();
                        mDialog.dismiss();
                    }
                }
            });
        }
    }
}
}
  • 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-25T18:29:47+00:00Added an answer on May 25, 2026 at 6:29 pm

    Well, look at getView. Your last action for a recycled view is:

    // ...
    } else {
        holder = (CustomHolder) convertView.getTag();
    
        // Edit: how to update the holder (see explanation further below)
        holder.updateText(position);
    }
    
    return row;
    

    You have to fill and updated that holder with the new data for that position. It your case it just stays the same (so the data which was set in the if section).

    Edit: How to update the holder?

    Suppose you would implement this method in CustomHolder, then you could update it the way showed obove:

    public void updateText(int position) {
        text.setText(entries[position]);
    }
    

    Notice: the common pattern is not to implement the update itself in the holder. My holder is usually a class with public members like holder.title (and no methods, just a container for references), so I can call holder.title.setTitle(...) from getView directly.

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

Sidebar

Related Questions

I have been trying to display a list that I build using PySide. It
I am just trying to display a list from an array that I have
im trying to write an app that will display a list off lines from
I'm trying to create a view that contains a list of checkboxes that is
trying to retrieve the id column of records that have their checkboxes selected in
I'm trying to display a list of all the Input Methods that are currently
I'm trying to display a list of items that do not already exist in
I am trying to display a list of cookbooks which have been tagged with
I am trying to display a list of all files found in the selected
I am trying to display a list of items in a datagrid from an

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.