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

位置の代わりにreal_idを簡単に使用できます。

holder.id = position;

holder.id = getItemId(position);

基礎となるCursorAdapterからこのメソッドをすでに持っています。

于 2012-11-11T16:58:26.783 に答える
1

holder.logoutieのタグを設定できます

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

getView()メソッドでビューを設定している間。

ieからタグ値を取得しButtonます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 に答える