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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:24:14+00:00 2026-06-14T03:24:14+00:00

I am applying a custom SimpleCursorAdapter inside an AlertDialog fragment in order to create

  • 0

I am applying a custom SimpleCursorAdapter inside an AlertDialog fragment in order to create a custom list layout with embedded buttons in the list entries. When I try to get an _id for my ContentResolver in the ClickListener, I am having trouble trying to find the correct id value. I am currently using ‘position’, but that is position within the list, not the underlying id AFAIK.

public class ProfileSelectFragment extends DialogFragment {
    private static final String DEBUG_TAG = "ProfileSelectFragment";
    protected int layout = R.layout.profileselect_dialog;
    final Fragment fragment0 = new LoadedProfilesFragment();

    protected SimpleCursorAdapter listAdapter;
    protected String[] uiBindFrom = { ProfilesColumns.USERNAME, ProfilesColumns.CREATIONDATE };
    protected int[] uiBindTo = { R.id.title, R.id.creationdate };
    protected int entryLayout = R.layout.profileselect_list_item;
    protected final Uri table = ProfileProvider.URI_LOADEDPROFILEVIEW;
    protected String[] projection = { CommonDatabaseHelper._ID, ProfilesColumns.USERNAME, ProfilesColumns.CREATIONDATE };

    static ProfileSelectFragment newInstance() {
        ProfileSelectFragment f = new ProfileSelectFragment();
        return f;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater inflater = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE));
        View customView = inflater.inflate(layout, null, false);
        Cursor c = getActivity().getContentResolver().query(table, projection, null, null, null);
        listAdapter = new SelectProfileAdapter(getActivity().getApplicationContext(), entryLayout, c, uiBindFrom,
                uiBindTo, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

        Dialog myDialog = new AlertDialog.Builder(getActivity()).setView(customView)
                .setAdapter(listAdapter, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.e(DEBUG_TAG, "NEVER CALLED");
                    }
                }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.d(DEBUG_TAG, "cancel");
                    }
                }).create();
        final android.support.v4.app.FragmentManager fm = getActivity().getSupportFragmentManager();
        final android.support.v4.app.FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.fragment, fragment0);
        transaction.commit();
        return myDialog;
    }

    static class ViewHolder {
        public Button name;
        public Button logout;
        public int id;
        public int creationDate;
    }

    public class SelectProfileAdapter extends SimpleCursorAdapter {
        final static String DEBUG_TAG = "SelectProfileAdapter";
        int layout;
        Cursor c;
        private final LayoutInflater mInflater;

        public SelectProfileAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
            super(context, layout, c, from, to, flags);
            Log.d(DEBUG_TAG, "SelectProfileAdapter");
            this.layout = layout;
            this.c = c;
            mInflater = LayoutInflater.from(context);
        }

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

            ViewHolder holder;

            if (convertView == null) {
                convertView = mInflater.inflate(entryLayout, null);
                holder = new ViewHolder();
                holder.name = (Button) convertView.findViewById(R.id.title);
                holder.logout = (Button) convertView.findViewById(R.id.logout);
                holder.id = position;
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            c.moveToPosition(position);         
            final String label = c.getString(label_index);
            holder.name.setText(label);
            ...

            holder.logout.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(final View v) {
                    ContentResolver cr = getActivity().getContentResolver();
                    String[] argument = { "" + position };
                    cr.delete(ProfileProvider.URI_LOADEDPROFILETABLE, CommonDatabaseHelper._ID + "=?", argument);
                }
            });
            return convertView;
        }

        @Override
        public boolean areAllItemsEnabled() {
            return false;
        }

        @Override
        public boolean isEnabled(int position) {
            return false;
        }
    }
}
  • 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-14T03:24:16+00:00Added an answer on June 14, 2026 at 3:24 am

    You can easily use real _id instead of position, replace

    holder.id = position;
    

    with

    holder.id = getItemId(position);
    

    You already have this method from underlying CursorAdapter.

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

Sidebar

Related Questions

I'm using cursor adapter to get a custom list view.I've two xml files. a.xml
Hi im applying custom images to button on different states, for which i create
I'm applying the following example http://jsoup.org/cookbook/extracting-data/example-list-links to list links. package org.jsoup.examples; import org.jsoup.Jsoup; import
I have a problem with an image that I am applying a custom jquery
I'm creating a custom search form and when I try and sort the results
When applying a custom height on a select element, the text in IE8 is
I am having difficulties applying my custom 404 page in Drupal. So first I
I have a question regarding applying a custom function to mysql_fetch_assoc while it is
I have a JInternalFrame that I am applying a custom UI to. The UI
I am making a custom menu. I am applying colors on panels on mouse

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.