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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:04:07+00:00 2026-06-14T12:04:07+00:00

I have a custom SimpleCursorAdapter in a DialogFragment, and I am having trouble understanding

  • 0

I have a custom SimpleCursorAdapter in a DialogFragment, and I am having trouble understanding the use of setTag and getTag. From my LogCat output, it seems that I am setting tag on a LinearLayout and trying to retrieve the tag from a Button. How can I target the right component to access the tag in the ClickListener?

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        if (mCursor.moveToPosition(position)) {
            ViewHolder holder;
            final String label;
            final int label_index = mCursor.getColumnIndex(ProfilesColumns.USERNAME);
            label = mCursor.getString(label_index);

            if (convertView == null) {
                convertView = mInflater.inflate(layout, null);

                holder = new ViewHolder();
                holder.name = (Button) convertView.findViewById(R.id.title);
                holder.logout = (Button) convertView.findViewById(R.id.logout);
                holder.id = getItemId(position);
                convertView.setTag(holder);
                Log.d(DEBUG_TAG, "getView view " + convertView);//Returns LinearLayout
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.logout.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(final View v) {
                    ViewHolder holder;
                    if (v == null) {
                        Log.d(DEBUG_TAG, "logout view null ");
                    } else {
                        Log.d(DEBUG_TAG, "logout view " + v);//Returns Button
                        holder = (ViewHolder) v.getTag();
                        if (holder == null) {
                            Log.d(DEBUG_TAG, "logout holder null ");
                        } else {
                            Log.d(DEBUG_TAG, "logout holder.id   " + holder.id);
                            String[] argument = { "" + holder.id };
                            ContentResolver cr = getActivity().getContentResolver();
                            int count = cr.delete(ProfileProvider.URI_LOADEDPROFILETABLE, CommonDatabaseHelper._ID
                                    + "=?", argument);
                            Log.d(DEBUG_TAG, "logout count       " + count);
                        }
                    }
                }
            });
        }
        return convertView;

    }

Here is the the layout, profileselect_list_item:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="0dp" >

<Button
    android:id="@+id/title"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1" />

<Button
    android:id="@+id/logout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/logout" />

</LinearLayout>
  • 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-14T12:04:09+00:00Added an answer on June 14, 2026 at 12:04 pm

    The short answer is that you have not used setTag() on the Buttons, you only used it on its parent the LinearLayout. So inside your OnClickListener change this:

    holder = (ViewHolder) v.getTag();
    

    to:

    holder = (ViewHolder) ((View) v.getParent()).getTag();
    

    Also this line:

    holder.id = getItemId(position);
    

    changes for each row, while the OnClickListener doesn’t. You should move the above line outside of if(convertView == null) and move the OnClickListener inside.


    The longer answer
    You are extending a CursorAdapter, they have three great methods newView(), bindView(), and getView().

    • newView() creates the new Views. Override this method and move all of your if(convertView == null) { ... } code into here.

    • bindView() has direct access to the Cursor. It seems the SimpleCursorAdapter’s default method takes care of this for you…

    • Overriding getView() is not always necessary because of the awesomeness of newView() and bindView().

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

Sidebar

Related Questions

I have custom event that has several different subscribers who will all use the
I have a custom adapter extended from the SimpleCursorAdapter. Using this I'm binding a
I have custom slider that I use the following touch event to respond to:
I have custom checkstyle checks file (called checks.xml), and I'm trying to use that
I have a ListView in AcitivityA that is populated using a custom SimpleCursorAdapter called
I have successfully implemented a custom SimpleCursorAdapter for an AutoCompleteTextView, which suggests entries from
I have custom classes that I currently instantiate within App.xaml as resources. I want
I have custom gallery. Gallery represents items that are frame layout. There are one
I have custom component that I can place in my layout file (XML) for
I have an app that will query a database and attempt to output results

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.