0

DialogFragmentにカスタムSimpleCursorAdapterがあり、setTagとgetTagの使用法を理解するのに問題があります。LogCatの出力から、LinearLayoutにタグを設定し、ボタンからタグを取得しようとしているようです。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;

    }

レイアウトは次のとおりです。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>
4

1 に答える 1

3

簡単に言うとsetTag()、ボタンでは使用しておらず、親であるLinearLayoutでのみ使用しています。したがって、OnClickListener内でこれを変更します。

holder = (ViewHolder) v.getTag();

に:

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

また、この行:

holder.id = getItemId(position);

OnClickListenerは変更しませんが、行ごとに変更します。上記の行を外側if(convertView == null)移動し、OnClickListenerを内側に移動する必要があります。


長い答え
あなたはCursorAdapterを拡張しています、それらには3つの素晴らしいメソッド、、、がnewView()ありbindView()ますgetView()

  • newView()新しいビューを作成します。このメソッドをオーバーライドして、すべてのif(convertView == null) { ... }コードをここに移動します。

  • bindView()カーソルに直接アクセスできます。SimpleCursorAdapterのデフォルトのメソッドがこれを処理しているようです...

  • との素晴らしさのため、オーバーライドgetView()は必ずしも必要ではありませnewView()bindView()

于 2012-11-14T18:20:19.560 に答える