Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在带有适配器的ListView中的复选框上使用McClickListener_Java_Android_Checkbox - Fatal编程技术网

Java 在带有适配器的ListView中的复选框上使用McClickListener

Java 在带有适配器的ListView中的复选框上使用McClickListener,java,android,checkbox,Java,Android,Checkbox,我有一行,它由一个图像、一些文本和一个复选框表示。每当我尝试对行使用OnItemClickListener时,单击复选框时不会触发事件 我还尝试了checkbox.onCheckedChangedListener,但它在findViewByID上给了我空指针。我查过了,我要找的身份证没问题,没有打字错误 我想利用这个监听器,以便稍后我可以使用复选框。有什么想法吗 代码: 适配器: public class FilterAdapter extends BaseAdapter { priv

我有一行,它由一个图像、一些文本和一个复选框表示。每当我尝试对行使用OnItemClickListener时,单击复选框时不会触发事件

我还尝试了checkbox.onCheckedChangedListener,但它在findViewByID上给了我空指针。我查过了,我要找的身份证没问题,没有打字错误

我想利用这个监听器,以便稍后我可以使用复选框。有什么想法吗

代码:

适配器:

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.check_box);



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


        return convertView;
    }
}
在使用适配器的位置初始化:

public class CheckinFilters extends Fragment {

    private ListView mListView;
    private FilterAdapter filterAdapter;
    private ArrayList<String> l = new ArrayList<String>();
    private ArrayList<Integer> drawables = new ArrayList<Integer>();
    private Typeface Bebas;
    private ArrayList<Integer> checkboxes = new ArrayList<Integer>();
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;


    public CheckinFilters() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        final View view = inflater.inflate(R.layout.fragment_checkin_filters, container, false);


        l.add("Chill");
        l.add("Eat");
        l.add("Explore");
        l.add("Move");
        l.add("Party");
        l.add("Whatever");

        drawables.add(R.drawable.category_chill);
        drawables.add(R.drawable.category_eat);
        drawables.add(R.drawable.category_explore);
        drawables.add(R.drawable.category_move);
        drawables.add(R.drawable.category_party);
        drawables.add(R.drawable.category_whatever);

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


        sharedPreferences = getActivity().getPreferences(Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();

        Bebas = Typeface.createFromAsset(view.getContext().getAssets(), "fonts/BebasNeue.otf");

        TextView mainHeader = (TextView) view.findViewById(R.id.filters_text);
        mainHeader.setTypeface(Bebas);

        SearchView filter_categories = (SearchView) view.findViewById(R.id.filter_categories);

        int searchImgID = getResources().getIdentifier("android:id/search_button", null, null);
        ImageView searchViewHint = (ImageView) filter_categories.findViewById(searchImgID);
        searchViewHint.setImageResource(R.drawable.ab_icon_search);

        int searchPlateID = filter_categories.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
        View searchPlateView = filter_categories.findViewById(searchPlateID);
        if (searchPlateView != null) {
            searchPlateView.setBackgroundResource(R.drawable.search_location_shape);
        }

        int searchViewID = filter_categories.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
        TextView textView = (TextView) filter_categories.findViewById(searchViewID);
        textView.setTextColor(Color.GRAY);

        CheckBox checkBox = (CheckBox) view.findViewById(R.id.check_box);

        return view;
    }

    private void save() {
        sharedPreferences = getActivity().getPreferences(Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
        editor.putInt("first", checkboxes.size());

        for (int i = 0; i < checkboxes.size(); i++) {
            editor.remove("Status_" + i);
            editor.putInt("Status_" + i, checkboxes.get(i));
        }

        editor.commit();
    }

    private void load() {
        sharedPreferences = getActivity().getPreferences(Context.MODE_PRIVATE);
        int size = sharedPreferences.getInt("first", 0);

        for (int i = 0; i < size; i++) {
            checkboxes.add(sharedPreferences.getInt("Status_" + i, 0));
        }

    }

    @Override
    public void onPause() {
        super.onPause();
        save();
    }

    @Override
    public void onResume() {
        super.onResume();
        load();

    }
}
我正在为适配器充气的布局:

<?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">

        <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/check_box"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:padding="10dp"/>

    </RelativeLayout>

</RelativeLayout>
您应该在FilterAdapter的getView方法中添加该代码,而不是在CheckinFilters.java中使用OnItemClickListener。实际上,您正在为每一行添加复选框,但“所有”复选框的id相同,因此无法为每个复选框应用自定义代码

为了能够使用所有复选框,您可以将复选框代码放在getView方法中,因为它还包含一个名为position的参数。因此,您只需获得位置并将McClickListener应用于每个复选框。这样就行了

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.check_box);



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

    if(position == 0) {
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked)

            else {

            }

        }
    });
    }

    //Similarly add OnClickListener to other checkboxes.


    return convertView;
}
}

请分享你编辑我的问题的代码,请看一看:你在哪里打电话?在您的适配器中还是在活动中?@Darkie在片段中,这就是我希望能够使用onItemClickListener的地方,因为复选框是扩展的一部分row@PopAlex-克里斯蒂安,你必须在adapterOK中调用checkbox的click listener,但McClick listener实际上是如何工作的呢?如果我单击复选框以外的任何位置,它将触发事件。一定有一种方法可以按照我的方式来做。@PopAlex Cristian我已经发布了代码,你只需要检查位置并将listener添加到该位置的复选框中。