0

I have problem with keeping checkbox state in listview. I have tried many stackoverflow questions regarding this problem. But couldnt find any solution yet. Please somebody help me. Here is my getView method in my custom adapter.

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);



        View rowView = inflater.inflate(R.layout.row_sub_packages, parent,
                false);

        TextView title = (TextView) rowView.findViewById(R.id.textView1);

        TextView desc = (TextView) rowView.findViewById(R.id.textView2);

        TextView sub = (TextView) rowView.findViewById(R.id.TextView01);
        TextView book = (Button) rowView.findViewById(R.id.button2);

        CheckBox fav = (CheckBox) rowView.findViewById(R.id.check1);

        Flag.clear();
        Flag = dh.getData(6, "packages");

        boolean flag = false;

        for (int i = 0; i < Flag.size(); i++) {

            if (Flag.get(i)
                    .equals(SubPackagesFragment.Sub_cat_Id[position])) {

                Log.i("Flag true", "Flag true " + Flag.get(i) + " "
                        + SubPackagesFragment.Sub_cat_Id[position]);

                flag = true;
            }

            else {
                flag = false;
            }

        }

        if (flag == true) {
            fav.setChecked(true);
        }

        else {
            fav.setChecked(false);
        }

        fav.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stub

                if (isChecked == true) {

                    dh.addfav(Title[position], sub_cat_Sub[position],
                            sub_cat_des[position], Images[position],
                            "packages",
                            SubPackagesFragment.Sub_cat_Id[position]);

                    // Toast.makeText(getContext(),
                    // Title[position]+"+"+SubPackagesFragment.Sub_cat_Id[position],
                    // Toast.LENGTH_SHORT).show();

                } else {
                    // Toast.makeText(getContext(), "off",
                    // Toast.LENGTH_SHORT).show();

                    dh.delete(SubPackagesFragment.Sub_cat_Id[position],
                            "packages");

                }

            }
        });


        title.setText(Title[position]);




        return rowView;

}
4

1 回答 1

1

你正在循环ArrayList,问题是当你检测到true你仍然会循环并检测语句false是否true不在末尾。break;所以在检测到后尝试设置true

于 2013-06-26T13:31:07.553 回答