1

各行にテキストとボタンで構成されるリストビューがあります。行にあるすべてのボタンにクリックリスナーを実装する必要がありますが、実装できません。SOとは別のチェックもしましたが、望ましい結果が得られませんでした。そのために、カスタムアダプターを次のように実装しました。

public class CategoryAdapter extends BaseAdapter {

    Context mContext;
    LayoutInflater mInflater;
    String[] strings = { "jjacj", "kpomkn", "jjbjbj nk", "njbhvh",
            "mknkn", "mnknlhn", "lknkn", "mknkn"};
    Holder holder = null;

    public CategoryAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return strings.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @SuppressWarnings("static-access")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            mInflater = (LayoutInflater) mContext
                    .getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
            holder = new Holder();
            convertView = mInflater.inflate(R.layout.layout_child_category,
                    null);
            holder.mTxtTitle = (TextView) convertView
                    .findViewById(R.id.txt_notification);
            holder.mButtonTag = (Button) convertView
                    .findViewById(R.id.btnNotify);
            holder.mLayoutMain = (RelativeLayout) convertView
                    .findViewById(R.id.layout_main);
            // convertView.setOnClickListener(callback);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

        holder.mTxtTitle.setText(strings[position]);
        holder.mButtonTag.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                btnSelection();
            }
        });

        if (position == 0) {
            holder.mLayoutMain.setBackgroundResource(R.drawable.bg_table_top);
        } else if (position == getCount() - 1) {
            holder.mLayoutMain
                    .setBackgroundResource(R.drawable.bg_table_bottom);
        } else {
            holder.mLayoutMain
                    .setBackgroundResource(R.drawable.bg_table_middle);
        }

        return convertView;
    }

    OnClickListener callback = new OnClickListener() {

        @Override
        public void onClick(View v) {
            btnSelection();
        }
    };

    private void btnSelection() {
        if (holder.mButtonTag.getTag().equals("no")) {
            holder.mButtonTag.setTag("yes");
            holder.mButtonTag.setText("NEI");
            holder.mButtonTag.setPadding(20, 0, 0, 0);
            holder.mButtonTag.setBackgroundResource(R.drawable.ic_btn_nei);
            holder.mButtonTag.setTextColor(Color.BLACK);
        } else {
            holder.mButtonTag.setTag("no");
            holder.mButtonTag.setText("JA");
            holder.mButtonTag.setPadding(0, 0, 20, 0);
            holder.mButtonTag.setBackgroundResource(R.drawable.ic_btn_ja);
            holder.mButtonTag.setTextColor(Color.WHITE);
        }
    }

    static class Holder {
        Button mButtonTag;
        TextView mTxtTitle;
        RelativeLayout mLayoutMain;
    }
}

子レイアウトのリスト:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_main"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:background="@drawable/bg_table_middle" >

    <TextView
        android:id="@+id/txt_notification"
        style="@style/textview_layout"
        android:textColor="@android:color/black" />

    <Button
        android:id="@+id/btnNotify"
        style="@style/button_layout" />

</RelativeLayout> 

これが私の見解です:

ここに画像の説明を入力

リストにある各ボタンで少なくともトーストを取得するには、どこが欠けているか、または変更を行う必要があることを誰かに教えてください。あらゆる種類の助けをいただければ幸いです。

ありがとう。

4

3 に答える 3

1

このようにビューを関数の引数として渡します

private void btnSelection(View button) {
    if (button.getTag().equals("no")) {
        button.setTag("yes");
        ((Button)button).setText("NEI");
        button.setPadding(20, 0, 0, 0);
        button.setBackgroundResource(R.drawable.ic_btn_nei);
        ((Button)button).setTextColor(Color.BLACK);
    } else {
        button.setTag("no");
        ((Button)button).setText("JA");
        button.setPadding(0, 0, 20, 0);
        button.setBackgroundResource(R.drawable.ic_btn_ja);
        ((Button)button).setTextColor(Color.WHITE);
    }
}

メソッドでは、このようにgetView変更OnClickListenerします


クリックリスナーの前に追加getView

if (holder.mButtonTag.getTag() == null)
  holder.mButtonTag.setTag("");

holder.mButtonTag.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            btnSelection(v);
        }
    });

お役に立てれば...

于 2013-04-23T05:47:22.103 に答える
-1

ボタンのコードを次のように変更します

<Button
android:id="@+id/btnNotify"
style="@style/button_layout" 
  android:focusable="false"
  android:focusableInTouchMode="false"/>
于 2013-04-23T05:22:34.890 に答える