如何从适配器获取复选框计数器?

时间:2014-09-15 12:11:03

标签: android

我的应用程序中间有一个ListView,我在底部添加了一个透明的RelativeLayout,它包含一个TextView和一个按钮。在ListView中,每行都有一个复选框。我的问题是,我想在单击项目时使用列表中的已检查项目填充TextView。有没有办法做到这一点? 这是我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView
        android:text="@string/compare_information"
        android:textColor="#8F8F8F"
        android:textSize="20sp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@android:color/transparent"
        android:layout_marginBottom="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/informationText" />
    <ListView
        android:id="@+id/lvCompare"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@null"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_below="@+id/informationText">

    </ListView>
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="75dp"
        android:layout_alignParentBottom="true"
        android:background="#90303030" >
        <TextView
            android:id="@+id/checkedItems"
            android:text="0 of 10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#EEEEEE"
            android:textSize="14sp"
            android:gravity="center_vertical|left"
            />
        <Button
            android:id="@+id/compare"
            android:background="@drawable/filter_button_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical|right"
            android:text="@string/compare"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            />
    </RelativeLayout>
</RelativeLayout>

这是我的适配器:

    public class CompareListAdapter extends ArrayAdapter<Restaurant>{
        private ArrayList<Restaurant> list = new ArrayList<Restaurant>();
        private Context context;
        private static int checkBoxCounter = 0;
        private static int checkBoxInitialized = 0;
        private int layoutResourceId;
        private TextView cCheckedItems;
        private final String INFORMATION_TEXT = "Select six companies to compare";

        public CompareListAdapter(Context context, int resource,
                List<Restaurant> objects,TextView textViewToEdit ) {
            super(context, resource, objects);
            this.context = context;
            this.layoutResourceId = resource;
            checkBoxCounter = 0;
            cCheckedItems = textViewToEdit;
            // We put the strings from the array in to the hash map for storing
            for (int i = 0; i < objects.size(); ++i) {
                list.add(objects.get(i));
            }
        }

        public View getView( final int position, View convertView, ViewGroup parent) {
            View view = null;
            if(convertView==null){
                LayoutInflater inflater=(LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
                view=inflater.inflate(layoutResourceId, parent, false);
                final ViewHolder viewHolder = new ViewHolder();
                viewHolder.cName =(TextView) view.findViewById(R.id.rest_name_textView1);
                viewHolder.addres1 =(TextView) view.findViewById(R.id.address1_textView2);
                viewHolder.addres2 =(TextView) view.findViewById(R.id.address2_textView3);
                viewHolder.openingTime =(TextView) view.findViewById(R.id.opening_hour_textView4);
                viewHolder.distance =(TextView) view.findViewById(R.id.distance_user_textView5);
                viewHolder.openImage =(ImageView)view.findViewById(R.id.clock_imageView2);
                viewHolder.checkBox = (CheckBox) view.findViewById(R.id.checkBox1);
                viewHolder.cbBackground = (RelativeLayout) view.findViewById(R.id.checkbox_layout);
                viewHolder.rating = (RatingBar) view.findViewById(R.id.restaurant_ratingBar1);
                viewHolder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if(isChecked && checkBoxCounter>=2){
                            list.get(position).setChecked(false);
                            viewHolder.checkBox.setChecked(false);
                            viewHolder.cbBackground.setBackgroundResource(R.drawable.checkbox_background);
                        }else {
                            Restaurant element = (Restaurant) viewHolder.checkBox.getTag();
                            element.setChecked(buttonView.isChecked());
                            if (isChecked) {
                                viewHolder.cbBackground.setBackgroundResource(R.drawable.checkbox_background_checked);
                                checkBoxCounter++;
                            } else {
                                viewHolder.cbBackground.setBackgroundResource(R.drawable.checkbox_background);
                                checkBoxCounter--;
                            }                            
                        }
                    Log.d("MYLOG INSIDE getView", String.valueOf(cCheckedItems));
                    cCheckedItems.setText(checkBoxCounter + " of 6");
                    Toast.makeText(context, "there are " + checkBoxCounter + " items selected", Toast.LENGTH_LONG).show();}});
}});
                view.setTag(viewHolder);
                viewHolder.checkBox.setTag(list.get(position));
            }else{
                view = convertView;
                ((ViewHolder) view.getTag()).checkBox.setTag(list.get(position));
            }
            ViewHolder viewHolder = (ViewHolder) view.getTag();
            Restaurant obj=list.get(position);
            viewHolder.rating.setRating((float) obj.getRating());
            viewHolder.cName.setText(obj.getRestaurantName());
            viewHolder.addres1.setText(obj.getAddress1());
            viewHolder.addres2.setText(obj.getAddress2());
            viewHolder.openingTime.setText(obj.getDayOfWeek()+" "+obj.getOpeningHour());
            viewHolder.distance.setText(obj.getDistance());
            viewHolder.checkBox.setChecked(obj.isChecked());
            if(obj.isChecked()) {
                viewHolder.cbBackground.setBackgroundResource(R.drawable.checkbox_background_checked);
            }else{
                viewHolder.cbBackground.setBackgroundResource(R.drawable.checkbox_background);
            }
            boolean isOpen=checkForOpen(obj.getDayOfWeek(),obj.getOpeningHour());
            if(isOpen)
                viewHolder.openImage.setImageResource(R.drawable.opened_logo);
            else viewHolder.openImage.setImageResource(R.drawable.closed_logo);
            return view;
        }
        private boolean checkForOpen(String weekDay,String openingTimeRange) {
            Calendar cal = Calendar.getInstance(); 


            int currentMinute=cal.get(Calendar.HOUR_OF_DAY)*60+cal.get(Calendar.MINUTE);;//today.MINUTE;
            int dayNo=cal.get(Calendar.DAY_OF_WEEK);
            String []timeRange=openingTimeRange.split("-");

            int openingMn=Integer.parseInt(timeRange[0].substring(0,2))*60+Integer.parseInt(timeRange[0].substring(3,5));

            int closingMn=Integer.parseInt(timeRange[1].substring(0,2))*60+Integer.parseInt(timeRange[1].substring(3,5));

            if(weekDay.equals(getDayOfWeekFromDayNo(dayNo))&& currentMinute>=openingMn  &&currentMinute<=closingMn)
                return true;


            return false;
        }
        public String getDayOfWeekFromDayNo(int dayNo)
        {
            String dayName="";
            switch (dayNo) {
            case 1:
                dayName="SUNDAY";
                break;
            case 2:
                dayName="MONDAY";
                break;
            case 3:
                dayName="TUESDAY";
                break;
            case 4:
                dayName="WEDNESDAY";
                break;
            case 5:
                dayName="THURSDAY";
                break;
            case 6:
                dayName="FRIDAY";
                break;
            case 7:
                dayName="SATERDAY";
                break;

            default:
                break;
            }
            return dayName;              
        }
        static class ViewHolder{
            RatingBar rating;
            CheckBox checkBox;
            RelativeLayout cbBackground;
            TextView cName, addres1, addres2, openingTime, distance;
            ImageView openImage;


        }
    }

更新:搞定了。添加了我现在在IDE上工作的内容。我在构造函数中创建了TextView要求,并将TextView保存在适配器中,然后在需要时调用它。如果TextView存在,则设置日志以进行测试。感谢@SagarPilkhwal指导解决方案。

1 个答案:

答案 0 :(得分:0)

private TextView cCheckedItems;
....
cCheckedItems =(TextView) view.findViewById(R.id.checkedItems);
....
    viewHolder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked && checkBoxCounter>=2){
            list.get(position).setChecked(false);
            viewHolder.checkBox.setChecked(false);
            viewHolder.cbBackground.setBackgroundResource(R.drawable.checkbox_background);
        }else {
            Restaurant element = (Restaurant) viewHolder.checkBox.getTag();
            element.setChecked(buttonView.isChecked());
            if (isChecked) {
                viewHolder.cbBackground.setBackgroundResource(R.drawable.checkbox_background_checked);
                checkBoxCounter++;              
            } else {
                viewHolder.cbBackground.setBackgroundResource(R.drawable.checkbox_background);
                checkBoxCounter--;
            }
            cCheckedItems.setText(checkBoxCounter); //<-- ADD THIS
            Toast.makeText(context, "there are " + checkBoxCounter + " items selected", Toast.LENGTH_LONG).show();
        }}});