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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:48:44+00:00 2026-06-04T10:48:44+00:00

I am making a custom ListView of rows containing a CheckBox and a TextView

  • 0

I am making a custom ListView of rows containing a CheckBox and a TextView. Before I used custom ListViews with SimpleCursorAdapter, my onListItemClick() worked fine.

I’ve read I have to add an onClickListener to my TextViews but WHERE? And WHY?

I am still extending ListActivity and passing an Adapter to setListAdapter(listedPuzzleAdapter);, am I not?

public class PuzzleListActivity extends ListActivity {

    private PuzzlesDbAdapter mDbHelper;
    private Cursor puzzlesCursor;

    private ArrayList<ListedPuzzle> listedPuzzles = null;
    private ListedPuzzleAdapter listedPuzzleAdapter;

    private class ListedPuzzleAdapter extends ArrayAdapter<ListedPuzzle> {

        private ArrayList<ListedPuzzle> items;

        public ListedPuzzleAdapter(Context context, int textViewResourceId,
                ArrayList<ListedPuzzle> items) {
            super(context, textViewResourceId, items);
            this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.puzzles_row, null);
            }
            ListedPuzzle lp = items.get(position);
            if (lp != null) {
                TextView title = (TextView) v.findViewById(R.id.listTitles);
                title.setText(lp.getTitle());
                CheckBox star = (CheckBox) v.findViewById(R.id.star_listed);
                star.setChecked(lp.isStarred());
            }
            return v;
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 

        setContentView(R.layout.puzzles_list);

        // Create database helper to open connection
        mDbHelper = new PuzzlesDbAdapter(this);
        mDbHelper.open();

        fetchData();
    }   

    private void fetchData() {
        puzzlesCursor = mDbHelper.fetchAllPuzzles();
        startManagingCursor(puzzlesCursor);

        listedPuzzles = new ArrayList<ListedPuzzle>();
        ListedPuzzle lp;

        puzzlesCursor.moveToFirst();
        while (!puzzlesCursor.isAfterLast()) {
            lp = new ListedPuzzle();
            lp.setTitle(puzzlesCursor.getString(puzzlesCursor
                    .getColumnIndex(PuzzlesDbAdapter.KEY_TITLE)));
            lp.setStarred(puzzlesCursor.getInt(puzzlesCursor
                    .getColumnIndex(PuzzlesDbAdapter.KEY_STARRED)) > 0);
            listedPuzzles.add(lp);
            puzzlesCursor.moveToNext();
        }

        listedPuzzleAdapter = new ListedPuzzleAdapter(this,
                R.layout.puzzles_row, listedPuzzles);
        setListAdapter(listedPuzzleAdapter);

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this, PuzzleQuestionActivity.class);
        i.putExtra(PuzzlesDbAdapter.KEY_ROWID, id);
        startActivity(i);
    }

EDIT: My question was towards making the whole item of a custom ListView clickable so I found the best answer was the one given by @Luksprog. The onListItemClick from my ListActivity was enough. I just needed to set the android:focusable='false' to make it work.

Now, the CheckBox on each item of the ListView should “star” that item, which means, accesing the DB.

public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.puzzles_row, null);
            }
            ListedPuzzle lp = items.get(position);
            if (lp != null) {
                TextView title = (TextView) v.findViewById(R.id.listTitles);
                title.setText(lp.getTitle());
                CheckBox star = (CheckBox) v.findViewById(R.id.star_listed);
                star.setChecked(lp.isStarred());

                star.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {
                        Integer realPosition = (Integer) v.getTag();
                        ListedPuzzle obj = items.get(realPosition);
                        obj.getId();

                    }

                });
            }
            return v;

        }

But the v.getTag() refers to a non-final variable and if I change it the v = vi.inflate(R.layout.puzzles_row, null) cannot be assigned.
What’s the best way to solve this? I never really understood the whole final deal.

  • 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-04T10:48:46+00:00Added an answer on June 4, 2026 at 10:48 am

    If you want to add a special action for when you click the TextView or/and CheckBox from any of the rows in your ListView then add a OnCLickListener for those Views in the getView method of your custom Adapter:

     @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.puzzles_row, null);
                }
                ListedPuzzle lp = items.get(position);
                if (lp != null) {
                    TextView title = (TextView) v.findViewById(R.id.listTitles);
                    //set as the tag the position parameter 
                    title.setTag(new Integer(position));                    
                    title.setOnclickListener(new OnCLickListener(){
    
                    @Override 
                    public void onClick(View v) {
                        // Do the stuff you want for the case when the row TextView is clicked
                        // you may want to set as the tag for the TextView the position paremeter of the `getView` method and then retrieve it here
                        Integer realPosition = (Integer) v.getTag();
                        // using realPosition , now you know the row where this TextView was clicked
                    }
                }); 
                    title.setText(lp.getTitle());
                    CheckBox star = (CheckBox) v.findViewById(R.id.star_listed);
                    star.setChecked(lp.isStarred());
                }
                return v;
            }
    

    If you want to do an action when a row is clicked(no matter what View from that row was clicked(if one was clicked)) just use the OnItemClickListener on your ListView(or the callback onListItemClick in the case of a ListActivity).

    Also, I hope you set android:focusable="false" for the CheckBox(in R.layout.puzzles_row) because I don’t think onListItemClick will work otherwise.

    Edit :

    You start the new Activity in the onListItemClick(in the case of the ListActivity) callback if you want to start the new activity no matter where the user clicks a row:

    @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {          
            Intent i = new Intent(this, PuzzleQuestionActivity.class);
            i.putExtra(PuzzlesDbAdapter.KEY_ROWID, id);
            startActivity(i);
        }
    

    If, for some reason, you want to start the new Activity when the user clicks only(for example) the TextView in a ListView row then start the new activity in the onClick method from my code above:

    //...
    title.setOnclickListener(new OnCLickListener(){
    
                        @Override 
                        public void onClick(View v) {
                            Integer realPosition = (Integer) v.getTag();
                            ListedPuzzle obj = items.get(realPosition);
                            Intent i = new Intent(this, PuzzleQuestionActivity.class);
                            i.putExtra(PuzzlesDbAdapter.KEY_ROWID, obj.getTheId());//see below
                            startActivity(i);
                        }
    //...
    

    For this to work you’ll have to modify ListedPuzzle to also add the PuzzlesDbAdapter.KEY_ROWID column from the puzzlesCursor cursor in the fetchData() method:

    //...
    while (!puzzlesCursor.isAfterLast()) {
                lp = new ListedPuzzle();
                lp.setTitle(puzzlesCursor.getString(puzzlesCursor
                        .getColumnIndex(PuzzlesDbAdapter.KEY_TITLE)));
                lp.setStarred(puzzlesCursor.getInt(puzzlesCursor
                        .getColumnIndex(PuzzlesDbAdapter.KEY_STARRED)) > 0);
                lp.setTheId(puzzlesCursor.getLong(puzzlesCursor
                        .getColumnIndex(PuzzlesDbAdapter.KEY_ROWID)));
                listedPuzzles.add(lp);
    //...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a listview with custom rows and that extends SimpleAdapter. Each row consist
I am making a ListView with TextView and 1 delete button for each row.
I have a ListView backed by SimpleCursorAdapter and custom ViewBinder. I want to make
I am making a custom listview, in that data is dynamically changing after sometime.
I making a custom button, and I need to add a PreviewKeyDown event, whenever
I am making a custom form object in Django which has an overrided __init__
I'm making a custom event calendar with PHP. I am trying to get the
I'm making a custom control that can be dragged around and it is semi
I'm making a custom error dialog in my WPF app and I want to
I am making a custom control I need to add some default keybindings, microsoft

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.