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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:37:47+00:00 2026-05-27T13:37:47+00:00

I am coding a checklist on android, following an example from a textbook. The

  • 0

I am coding a checklist on android, following an example from a textbook.
The ListActivity consists of a listview (R.layout.PCheckList) where each row has a checkbox and a textview locked inside a horizontal layout (R.layout.lchecklist).
There is a line of the code which I totally do NOT understand
The code is below, and the i’ve highlighted the line

public class PChecklist extends ListActivity {

    TextView selection;
    String[] tasks={"Do Laundry",
            "Wash Dishes",
            "Make the bed",
            "Study",
            "Buy Groceries",
            "Mow the lawn",
            "Shave",
            "Iron Clothes",
            "Breathe periodically"};

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.pchecklist);

        this.selection = (TextView) findViewById(R.id.selection2);


        ArrayList<DailyTask> dailyTaskListModel = new ArrayList<DailyTask>();
        for(String t:tasks)
        {
            dailyTaskListModel.add(new DailyTask(t));
        }

        this.setListAdapter(new CheckListAdapter(this,dailyTaskListModel));
    }

    private DailyTask getTaskAt(int position)
    {
        return ((CheckListAdapter)getListAdapter()).getItem(position);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        this.selection.setText(String.format("Selection: %s", getTaskAt(position).toString()));
    }

    class DailyTask
    {
        String task;
        boolean isCompleted = false;

        DailyTask(String task)
        {
            this.task = task;
        }

        public String toString()
        {
            if(this.isCompleted)
                return (task.toUpperCase());
            else return (task);
        }
    }

    class CheckListAdapter extends ArrayAdapter<DailyTask>
    {
        Activity activity;

        CheckListAdapter(Activity context,ArrayList<DailyTask> taskList)
        {
            super(context, R.layout.lchecklist,taskList);

            this.activity = context;
        }


        public View getView(int position, View convertView, ViewGroup parent) {
            View recycledView = convertView;
            CheckListViewAccessor checkListViewAccessor=null;
            CheckBox checkbox;

            if(recycledView==null)
            {
                //if there is no view, we have to make one by inflating a layout.
                LayoutInflater inflater = activity.getLayoutInflater();
                recycledView = inflater.inflate(R.layout.lchecklist, null,false);

                checkListViewAccessor = new CheckListViewAccessor(recycledView);
                recycledView.setTag(recycledView);
                checkbox = checkListViewAccessor.getCheckBox();

                CompoundButton.OnCheckedChangeListener checkStateChangedListener = new CompoundButton.OnCheckedChangeListener(){

                    public void onCheckedChanged(CompoundButton cb, boolean isChecked) {
                        //When the check button is pressed, we want two things to happen.
                        //1. Update the model.
                            //for some reason we have to do this.
                            int positionOfCheckedItem = (Integer) cb.getTag();

                            DailyTask task = getTaskAt(positionOfCheckedItem);
                            task.isCompleted = isChecked;

                        //2. Change the String in the row to upper case.
                            cb.setText(task.toString());
                    }
                };

                checkbox.setOnCheckedChangeListener(checkStateChangedListener);

            }else//if recycledView is not null, then we don't need to add a listener, we just need to get access to the UI components
            {
                checkListViewAccessor = (CheckListViewAccessor) recycledView.getTag();
                checkbox = checkListViewAccessor.getCheckBox();
            }

            DailyTask task = getTaskAt(position);
            checkbox.setTag(new Integer(position));
            **checkbox.setText(task.toString());
                    //^This line I don't get.**
            checkbox.setChecked(task.isCompleted);


            return (recycledView);
        }

    }

    class CheckListViewAccessor
    {
        View checkListView;
        CheckBox checkbox=null;

        CheckListViewAccessor(View checkListView)
        {
            this.checkListView = checkListView;
        }

        CheckBox getCheckBox()
        {
            if(checkbox==null)
                this.checkbox = (CheckBox) findViewById(R.id.checkbox);
            return (checkbox);
        }
    }
}

In the line indicated, how does the checkbox know which TextView’s text to change? When was this relationship established?

  • 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-27T13:37:48+00:00Added an answer on May 27, 2026 at 1:37 pm

    There is no associated TextView with CheckBox. Because CheckBox is a TextView. Take a look at hierarchy here:

    ava.lang.Object
       ↳    android.view.View
           ↳    android.widget.TextView
               ↳    android.widget.Button
                   ↳    android.widget.CompoundButton
                       ↳    android.widget.CheckBox
    

    In other words CheckBox is a TextView with additional state management for checked and unchecked. (In fact CompoundButton is responsible for state management, but those are details).

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

Sidebar

Related Questions

While coding a simple function to remove a particular character from a string, I
When coding up, say, a registration form from scratch, does it make sense to
This coding i got from here. I get error in log cat view like
I coding a PHP script that has to check if exists a GitHub repo
Coding Platform: ASP.NET 4.0 C# Consider the following scenario. I am uploading a file
Coding in C: typedef struct { char *string; int one; int two; } Example;
Coding a calculation tool for my android. On of the inputs is distance in
Coding HTML5 page for use in iPad Mobile Safari that has tag for embedding
http://coding.pressbin.com/18/Display-a-Google-Map-when-you-hover-over-location-text/ I am following this tutorial on how to display google map over a
In coding a WPF application I used a ListView and defined an template as

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.