通过列表视图中的复选框选中时,不会删除最后一个元

时间:2014-07-16 08:51:18

标签: android listview checkbox

我在列表视图中选中了两个三元素,复选框也选中了最后一个元素 但是所有其他元素都被删除但最后一个元素保持不变。我无法解决这个问题 过去2天的问题,但我无法解决这个问题,请帮我解决这个问题。请检查我的getView。

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    ViewHolder viewHolder = null;
    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        convertView = inflator.inflate(R.layout.logitem1, null);
        viewHolder = new ViewHolder();
        viewHolder.text = (TextView) convertView.findViewById(R.id.id_skills);
        viewHolder.datetime = (TextView) convertView.findViewById(R.id.id_datetime);
        viewHolder.weather = (TextView) convertView.findViewById(R.id.id_weather);
        viewHolder.duration=(TextView)  convertView.findViewById(R.id.totminutes);
        viewHolder.supervisor=(TextView)  convertView.findViewById(R.id.conditions);
        viewHolder.day_night_icon=(TextView)  convertView.findViewById(R.id.day_night_icon);
        viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.id_chkDelete);
        viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.

                if(SelectedBox.size()-1==0)
                {
                    menu.setVisible(false);
                    addlog.setVisible(true);
                }else
                {
                    addlog.setVisible(false);
                }


                if(isChecked)
                {
                    SelectedBox.add(position);
                    menu.setVisible(true);
                    addlog.setVisible(false);


                }else /*if(!isChecked)*/
                {
                SelectedBox.remove(SelectedBox.indexOf(position));

                }
            }
        });





        convertView.setTag(viewHolder);
        convertView.setTag(R.id.id_skills, viewHolder.text);
        convertView.setTag(R.id.id_chkDelete, viewHolder.checkbox);
        convertView.setTag(R.id.id_datetime,viewHolder.datetime);
        convertView.setTag(R.id.id_weather,viewHolder.weather);
        convertView.setTag(R.id.totminutes,viewHolder.duration);
        convertView.setTag(R.id.conditions,viewHolder.supervisor);
        convertView.setTag(R.id.day_night_icon,viewHolder.day_night_icon);



        } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    viewHolder.checkbox.setTag(position); // This line is important.

    viewHolder.text.setText(list.get(position).getName());
    viewHolder.datetime.setText(list.get(position).getDatetime());
    viewHolder.weather.setText(list.get(position).getWeather());
    viewHolder.checkbox.setChecked(list.get(position).isSelected());
    if(!list.get(position).getDay_minutes().toString().equalsIgnoreCase("0"))
    {
        viewHolder.duration.setText(list.get(position).getDay_minutes());
        viewHolder.day_night_icon.setBackgroundResource(R.drawable.sun);
    }else
    {
        viewHolder.duration.setText(list.get(position).getNight_minutes());
        viewHolder.day_night_icon.setBackgroundResource(R.drawable.moon);

    }
    if(list.get(position).getSupervisor().equals("No supervisor"))
    {
        viewHolder.supervisor.setBackgroundResource(R.drawable.pending);
    }else
    {
        viewHolder.supervisor.setBackgroundResource(R.drawable.approve);
    }
    String fontPath = "fonts/Roboto-Light.ttf";
    Typeface tf = Typeface.createFromAsset(context.getAssets(), fontPath);
    viewHolder.datetime.setTypeface(tf);
    viewHolder.duration.setTypeface(tf);
    viewHolder.text.setTypeface(tf);
    viewHolder.weather.setTypeface(tf);


    menu.setOnMenuItemClickListener(new OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            // TODO Auto-generated method stub

            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

            // set title
            alertDialogBuilder.setTitle("Student Driving Practice Log");

            // set dialog message
            alertDialogBuilder
            .setMessage("Are you sure want to Delete Record!")
            .setCancelable(false)
            .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {

                    try
                    {
                        NewNewDataHelper db=new NewNewDataHelper(context);

                        if(!SelectedBox.isEmpty())
                        {
                            for(int i=0;i<SelectedBox.size();i++)
                            {

                            //  resultp=data.get(SelectedBox.get(i));
                                String str[]=list.get(i).getDatetime().split("  ");

                                Log.d("Checked Element",str[0]+"\n"+str[1]+"\n"+list.get(i).getName());


                                db.DeleteSingleLog(list.get(i).getName(),str[0],str[1]);

                                /*resultp=data.get(SelectedBox.get(i));

                                String str[]=resultp.get("date_time").split(" ");
                                db.DeleteSingleLog(resultp.get("Skill"),str[0],str[1]);*/

                                    Toast.makeText(context,"Record Deleted", Toast.LENGTH_LONG).show();


                            }

                            Log.d("LISTSTSTSTST", SelectedBox.toString());


                            Intent intent = new Intent(context,ManagePracticeLogActivity.class);
                            intent.putExtra("s11", "delete");
                            context.startActivity(intent);
                        }
                    }catch(Exception e)
                    {

                    }


                }
            })
            .setNegativeButton("No",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, just close
                    // the dialog box and do nothing
                    dialog.cancel();

                }
            });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();



            return false;
        }
    });
    convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub




            String str1 = null;
            String str[]=list.get(position).getDatetime().split("  ");


            Log.d("PARTICULAR SKILLLLL",str[1]);
            str1=str[0]+"~"+list.get(position).getName()+"~"+str[1];
            Log.d("PARTICULAR SKILLLLL", str1);
                Intent intent = new Intent(context,LogEdit.class);
                intent.putExtra("s11","Update Practice");
                intent.putExtra("dataupdate",str1);
                context.startActivity(intent);
        }
    });

    return convertView;
}

0 个答案:

没有答案