Button的点击事件在listview中没有响应

时间:2013-04-23 05:00:26

标签: android listview button onclicklistener

我有一个列表视图,每行包含文本和按钮。我必须为驻留在行中的每个按钮实现单击侦听器,但我无法执行此操作。我也检查了不同的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> 

以下是我的观点:

enter image description here

任何人都可以告诉我我缺少的地方,或者应该做任何改变,以便至少在列表中的每个按钮上祝酒。 任何形式的帮助将不胜感激。

感谢。

3 个答案:

答案 0 :(得分: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);
        }
    });

希望这会有所帮助......

答案 1 :(得分:0)

更改xml文件,如下所示。

<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:descendantFocusability="blocksDescendants"  
        android:background="@drawable/bg_table_middle" > 

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

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

并将android:descendantFocusability="blocksDescendants"放入listview xml文件中以进行主要布局。并将android:focusable="false"放入listview。

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

答案 2 :(得分:-1)

将按钮代码更改为

<Button
android:id="@+id/btnNotify"
style="@style/button_layout" 
  android:focusable="false"
  android:focusableInTouchMode="false"/>
相关问题