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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:42:08+00:00 2026-05-26T07:42:08+00:00

I am trying to construct SingleSelect list by extending ListView with customrow like <?xml

  • 0

I am trying to construct SingleSelect list by extending ListView with customrow like

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:descendantFocusability="blocksDescendants"
    >
    <CheckBox
        android:id="@+id/chk"
        android:button="@drawable/q_list_check_box"
        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_gravity="center_vertical"
        android:gravity="center_vertical"
        />
    <TextView
        android:id="@+id/txtChoice"
        android:textColor="@color/white"
        android:layout_gravity="center_vertical|left"
        android:gravity="center_vertical|left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" /> 
</LinearLayout>

and with code for SingleSelectList like

public class SingleList extends ListView {

    int selected;
    List<String> data;

    public SingleList(Context context) {
        super(context);

        this.selected = -1;
        this.data = new ArrayList<String>();
    }

    public SingleList(Context context, AttributeSet attributes) {
        super(context, attributes);

        this.selected = -1;
        this.data = new ArrayList<String>();
    }

    public int getSelectedIndex() {
        return this.selected;
    }

    public void setSelectedIndex(int selected) {
        this.selected = selected;
    }

    public List<String> getData() {
        return this.data;
    }

    public void setData(List<String> data) {
        this.data = data;
        this.selected = -1;
    }

    public String getSelectedValue() {
        if (this.selected > -1)
            return this.data.get(selected);
        return "";
    }

    public void addDataItem(String item) {
        this.data.add(item);
    }

    public void initialize_list(List<String> data) {
        setData(data);
        this.setAdapter(new SingleListAdapter());
    }

    private class SingleListAdapter extends BaseAdapter {

        public SingleListAdapter() {
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return data.get(position).hashCode();
        }

        @Override
        public View getView(final int position, final View convertView,
                final ViewGroup parent) {

            View view = convertView;

            if (view == null) {
                LayoutInflater inflater = (LayoutInflater) getContext()
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.q_list_row_text, null);

            }
            final CheckBox chkChoice = (CheckBox) view.findViewById(R.id.chk);
            view.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    if (selected != -1) {
                        View tempView = SingleListAdapter.this.getView(
                                selected, convertView, parent);
                        CheckBox tempChk = (CheckBox) tempView
                                .findViewById(R.id.chk);
                        tempChk.performClick();
                    }

                    chkChoice.performClick();
                    selected = position;
                }
            });

            chkChoice.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {

                    if (isChecked) {
                        ((View) chkChoice.getParent())
                                .setBackgroundResource(R.drawable.backgroun_checked);
                    } else {
                        ((View) chkChoice.getParent())
                                .setBackgroundResource(R.drawable.background_unchecked);
                    }

                }
            });

            TextView txtChoice = (TextView) view.findViewById(R.id.txtChoice);
            txtChoice.setText(data.get(position));

            return view;
        }
    }

}

// int selected holds current checked item and I want on click row to unchecked that row and checked newone.
Can someone give me idea what I am doing wrong ?

  • 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-26T07:42:08+00:00Added an answer on May 26, 2026 at 7:42 am

    You can use a boolean ArrayList to maintain state of your check boxes. The size of that ArrayList should be equal to the list view items. You can maintain(select/deselect) the state of this ArrayList in onListItemClick() and most importantly, you should use this ArrayList in getView() of Adapter to set the state of checkbox. Where you can use “position” as index of ArrayList. e.g.

    ArrayList<Boolean> lst_arrCheck =  new ArrayList<Boolean>();
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
       for (int i = 0; i < listItems.size(); i++) // Do this before setting the adapter e.g. in OnCreate()
       {
           lst_arrCheck.add(false);
       }
    }
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) 
    {
        super.onListItemClick(l, v, position, id);
        // Get the item that was clicked
    
        lst_arrCheck.set(position, !(lst_arrCheck.get(position)));
    }
    ...
    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
      ...
      CheckBox cb_test = (CheckBox)findViewById(R.id.cb_test);
      cb_test.setChecked(lst_arrCheck.get(position));
     ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to construct a TreeView from a Menu. My Code is like
I'm currently trying to construct a list of bean classes in Java from a
I'm trying to construct the flexible URL routing. So the url like en-US/ctrl/act/1/2 should
I'm trying to construct a database driven VB.Net app that pulls a list of
I'm trying to construct a Link. The resulting Link should look something like this:
I'm trying to construct a little mobile optimized web. This web list a set
I'm trying to construct a page layout that will look something like this: In
Am trying to construct a simple update query in my model class Model_DbTable_Account extends
I'm trying to construct a find command to process a bunch of files in
I'm trying to construct a query that will include a column indicating whether or

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.