0

我在 AlertDialog 片段中应用了自定义 SimpleCursorAdapter,以便在列表条目中创建带有嵌入按钮的自定义列表布局。当我尝试在 ClickListener 中为我的 ContentResolver 获取 _id 时,我无法尝试找到正确的 id 值。我目前正在使用“位置”,但这是列表中的位置,而不是基础 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;
        }
    }
}
4

2 回答 2

1

holder.logout你可以为ie设置一个标签

holder.logout.setTag(c.getint("_id"));

getView()方法中填充视图时。

并从ieButton中获取标签值onClickListener()

holder.logout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(final View v) {
                int idField = (int) this.getTag();
                System.out.println("Id value - " + idField);
            }
        });
于 2012-11-11T16:49:48.000 回答
1

您可以轻松地使用真实的 _id 而不是位置,替换

holder.id = position;

holder.id = getItemId(position);

您已经从底层 CursorAdapter 获得了此方法。

于 2012-11-11T16:58:26.783 回答