ListView中的CheckBox由Adapter填充

时间:2014-11-13 11:43:01

标签: java android checkbox

我已经完成了有关此问题的阅读,但似乎无法弄明白。

我尝试设置可聚焦的复选框,并将焦点设置为触摸为false,在根布局块后代中,但仍然没有。我没有设法点击复选框启动活动。

以下是此事件:

 mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox);
                if (checkBox.isChecked())
                    Toast.makeText(view.getContext(), "TEST", Toast.LENGTH_SHORT).show();
            }
        });

我膨胀的布局:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ECF0F1">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/category_picture"
                android:layout_width="70dp"
                android:layout_height="60dp"
                android:background="@drawable/sm_profile"/>

            <TextView
                android:id="@+id/filter_category"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@+id/category_picture"
                android:padding="10dp"
                android:text="Party"
                android:textColor="@color/enloop_dark_gray"
                android:textSize="18dp"/>

            <CheckBox
                android:id="@+id/checkbox"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"/>

        </RelativeLayout>

    </RelativeLayout>

</RelativeLayout>

适配器类:

public class FilterAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<String> mFilters;
    private ArrayList<Integer> mPictures;
    private Typeface Bebas, DroidSans;


    public FilterAdapter(Context context, ArrayList<String> filters, ArrayList<Integer> pictures) {
        this.mContext = context;
        this.mFilters = filters;
        this.mPictures = pictures;
    }

    @Override
    public int getCount() {
        return mFilters.size();
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater)
                    mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.category_filter_item, null);
        }

        DroidSans = Typeface.createFromAsset(mContext.getAssets(), "fonts/DroidSans.ttf");

        ImageView filter_img = (ImageView) convertView.findViewById(R.id.category_picture);
        TextView filter_category = (TextView) convertView.findViewById(R.id.filter_category);
        CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkboxes);

        filter_category.setTypeface(DroidSans);
        filter_category.setText(mFilters.get(position));
        filter_img.setBackgroundResource(mPictures.get(position));


        return convertView;
    }

片段的主要XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context="com.enloop.Fragments.DrawerFragments.Filter.CheckinFilters">

    <RelativeLayout
        android:id="@+id/select_category_action_bar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/action_color">

        <RelativeLayout
            android:id="@+id/left_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">


            <ImageView
                android:id="@+id/menu_btn_from_maps"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:background="@drawable/ab_icon_menu"/>

            <ImageView
                android:id="@+id/enloop_logo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/menu_btn_from_maps"
                android:background="@drawable/ab_logo"/>
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/center_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/right_layout"
            android:layout_toRightOf="@+id/left_layout">

            <SearchView
                android:id="@+id/filter_categories"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:background="@android:color/transparent"
                android:queryHint="Search"/>

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/right_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true">

            <RelativeLayout
                android:id="@+id/check_in_lay"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <ImageButton
                    android:id="@+id/check_in_image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/ab_icon_checkins"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignRight="@id/check_in_image"
                    android:layout_alignTop="@+id/check_in_image"
                    android:layout_marginRight="3dp"
                    android:layout_marginTop="5dp"
                    android:background="@drawable/notifications_status"
                    android:gravity="center"
                    android:text="3"
                    android:textColor="@color/common_signin_btn_dark_text_default"
                    android:textSize="6dp"/>
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/message_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/check_in_lay">

                <ImageButton
                    android:id="@+id/message_image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/ab_icon_message"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignRight="@id/message_image"
                    android:layout_alignTop="@+id/message_image"
                    android:layout_marginRight="3dp"
                    android:layout_marginTop="5dp"
                    android:background="@drawable/notifications_status"
                    android:gravity="center"
                    android:text="2"
                    android:textColor="@color/common_signin_btn_dark_text_default"
                    android:textSize="6dp"/>
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/notification_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/message_layout">

                <ImageButton
                    android:id="@+id/notification_image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/ab_icon_notification"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignRight="@id/notification_image"
                    android:layout_alignTop="@+id/notification_image"
                    android:layout_marginRight="3dp"
                    android:layout_marginTop="5dp"
                    android:background="@drawable/notifications_status"
                    android:gravity="center"
                    android:text="17"
                    android:textColor="@color/common_signin_btn_dark_text_default"
                    android:textSize="6dp"/>
            </RelativeLayout>
        </RelativeLayout>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/filter_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/select_category_action_bar">

        <TextView
            android:id="@+id/filters_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:gravity="start|center_vertical"
            android:padding="20dp"
            android:text="CHECK-IN FILTERS"
            android:textColor="@color/enloop_dark_gray"
            android:textSize="30dp"
            android:textStyle="bold"/>

    </RelativeLayout>

    <ListView
        android:id="@+id/filter_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/filter_layout"
        android:background="#ECF0F1"/>


</RelativeLayout>

将适配器设置为ListView:

mListView = (ListView) view.findViewById(R.id.filter_list);
        filterAdapter = new FilterAdapter(view.getContext(), l, drawables);
        mListView.setAdapter(filterAdapter);

4 个答案:

答案 0 :(得分:0)

尝试在适配器的onClickListener方法中的复选框本身上添加getView,如下所示:

CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox);
checkBox .setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

             if (checkBox.isChecked())
                Toast.makeText(view.getContext(), "TEST", Toast.LENGTH_SHORT).show();


        }
    });

编辑:

您的getView方法应该是这样的:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater)
                mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.category_filter_item, null);
    }

    DroidSans = Typeface.createFromAsset(mContext.getAssets(), "fonts/DroidSans.ttf");

    ImageView filter_img = (ImageView) convertView.findViewById(R.id.category_picture);
    TextView filter_category = (TextView) convertView.findViewById(R.id.filter_category);
    CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);

    checkBox .setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

             if (checkBox.isChecked())
                Toast.makeText(view.getContext(), "TEST", Toast.LENGTH_SHORT).show();


        }
    });

    filter_category.setTypeface(DroidSans);
    filter_category.setText(mFilters.get(position));
    filter_img.setBackgroundResource(mPictures.get(position));


    return convertView;
}

答案 1 :(得分:0)

尝试实现onCheckChange Listener而不是Item Click Listener:

checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
     @Override
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked){
           Toast.makeText(view.getContext(),"Test", Toast.LENGTH_SHORT).show();
        }
     }
});

带有ViewHolder模式的Formmated适配器代码:

public class FilterAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<String> mFilters;
    private ArrayList<Integer> mPictures;


    public FilterAdapter(Context context, ArrayList<String> filters, ArrayList<Integer> pictures) {
        this.mContext = context;
        this.mFilters = filters;
        this.mPictures = pictures;
    }

    @Override
    public int getCount() {
        return mFilters.size();
    }

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.category_filter_item, null);
            holder.filter_img = (ImageView) convertView.findViewById(R.id.category_picture);
            holder.filter_category = (TextView) convertView.findViewById(R.id.filter_category);
            holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);

            holder.filter_category.setTypeface(Typeface.createFromAsset(mContext.getAssets(), "fonts/DroidSans.ttf"));
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        holder.filter_category.setText(mFilters.get(position));
        holder.filter_img.setBackgroundResource(mPictures.get(position));
        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    Toast.makeText(view.getContext(), "Item at " + position + " is Checked", Toast.LENGTH_SHORT).show();
                }
            }
        });


        return convertView;
    }
    class ViewHolder{
        ImageView filter_img;
        TextView filter_category;
        CheckBox checkBox;
    }
}

答案 2 :(得分:0)

问题是当您在inflated方法

中使用时,Checkbox视图的checkbox ID为getView()
 CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkboxes);// this is wrong.

所以你需要通过

进行更改
 CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);

getView()方法中,或仅在category_filter_item.xml Checkbox checkboxes到{{1}}的文件更改ID中。

答案 3 :(得分:0)

这不在Adapter类中。请参阅此处的ID。

CheckBox checkBox = (CheckBox) view.findViewById(R.id.check_box);
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "TEST", Toast.LENGTH_SHORT).show();
            }
        });
过滤器类中的

ID:

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater)
                    mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.category_filter_item, null);
        }

        DroidSans = Typeface.createFromAsset(mContext.getAssets(), "fonts/DroidSans.ttf");

        ImageView filter_img = (ImageView) convertView.findViewById(R.id.category_picture);
        TextView filter_category = (TextView) convertView.findViewById(R.id.filter_category);
        CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.check_box);

错误:

java.lang.NullPointerException
            at com.enloop.Fragments.DrawerFragments.Filter.CheckinFilters.onCreateView(CheckinFilters.java:104)

这正是我设置活动的地方。