滚动列表视图时,复选框项目未选中

时间:2015-09-29 06:23:19

标签: android listview checkbox

我可以使用共享首选项存储复选框的值,但是一旦滚动列表视图,复选框就会再次取消选中。我无法理解造成这种情况的原因。很想听听你的建议。

这是我的代码:

ListViewAdapter.java

package com.example.logintest;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ListViewAdapter extends BaseAdapter{

    private Context _context;
    private List<String[]> list;
    ViewHolder holder=null;
    View row=null;
    String cbstate; 

    public  ListViewAdapter(Context _context,List<String[]> list)
    {
        this._context=_context;
        this.list=list;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return list.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    private static class ViewHolder
    {
        public TextView textView;
        public ImageView imageview;
        public CheckBox cb;

        public ViewHolder(View row) {
            // TODO Auto-generated constructor stub
             textView = (TextView) row.findViewById(R.id.textView);
             cb = (CheckBox) row.findViewById(R.id.cb);  
             imageview = (ImageView)row.findViewById(R.id.imageView);
        }

    }


    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        // TODO Auto-generated method stub


        SharedPreferences state = _context.getSharedPreferences("data",Context.MODE_PRIVATE);

        row=convertView;
        holder=null;

           try{


               if(row==null)
               {
                   LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    row = inflater.inflate(R.layout.row, viewGroup, false);
                   holder=new ViewHolder(row);
                     row.setTag(holder);
                     Log.d("newrow", "New row");
        ////////////////////////////////////////////////////////////////////////


            holder.textView.setText(list.get(position)[0].toString());


            //images entered here0

            if(position==0)
            {
                holder.imageview.setImageDrawable( _context.getResources().getDrawable(R.drawable.one))  ;
            }
            else if(position==1)
            {
                holder.imageview.setImageDrawable( _context.getResources().getDrawable(R.drawable.two))  ;

            }
            else if(position==2)
            {
                holder.imageview.setImageDrawable( _context.getResources().getDrawable(R.drawable.three))  ;
            }
            else if(position==3)
            {
                holder.imageview.setImageDrawable( _context.getResources().getDrawable(R.drawable.four))  ;
            }
             else if(position==4)
            {
                 holder.imageview.setImageDrawable( _context.getResources().getDrawable(R.drawable.five))  ;
            }
            else if(position==5)
            {
                holder.imageview.setImageDrawable( _context.getResources().getDrawable(R.drawable.six))  ;
            }
            else if(position==6)
            {
                holder.imageview.setImageDrawable( _context.getResources().getDrawable(R.drawable.seven))  ;
            }
            else if(position==7)
            {
                holder.imageview.setImageDrawable( _context.getResources().getDrawable(R.drawable.eight))  ;
            }
            else if(position==8)
            {
                holder.imageview.setImageDrawable( _context.getResources().getDrawable(R.drawable.nine))  ;
            }
            else if(position==9)
            {
                holder.imageview.setImageDrawable( _context.getResources().getDrawable(R.drawable.ten))  ;
            }
            else if(position==10)
            {
                holder.imageview.setImageDrawable( _context.getResources().getDrawable(R.drawable.eleven))  ;
            }
            else if(position==11)
            {
                holder.imageview.setImageDrawable( _context.getResources().getDrawable(R.drawable.tweleve))  ;
            }
            else if(position==12)
            {
                holder.imageview.setImageDrawable( _context.getResources().getDrawable(R.drawable.thirteen))  ;
            }
            else if(position==13)
            {
                holder.imageview.setImageDrawable( _context.getResources().getDrawable(R.drawable.fourteen))  ;
            }
            else if(position==14)
            {
                holder.imageview.setImageDrawable( _context.getResources().getDrawable(R.drawable.fifteen))  ;
            }
            else if(position==15)
            {
                holder.imageview.setImageDrawable( _context.getResources().getDrawable(R.drawable.sixteen))  ;
            }
            else if(position==16)
            {
                holder.imageview.setImageDrawable( _context.getResources().getDrawable(R.drawable.seventeen))  ;
            }
            else if(position==17)
            {
                holder.imageview.setImageDrawable( _context.getResources().getDrawable(R.drawable.eighteen))  ;
            }
            else if(position==18)
            {
                holder.imageview.setImageDrawable( _context.getResources().getDrawable(R.drawable.nineteen))  ;
            }
            else if(position==19)
            {
                holder.imageview.setImageDrawable( _context.getResources().getDrawable(R.drawable.twenty))  ;
            }

            else
            {
                holder.imageview.setImageDrawable( _context.getResources().getDrawable(R.drawable.ic_launcher))  ;
            }



            //checkbox functions




        //Here we can do changes to the convertView, such as set a text on a TextView 
        //or an image on an ImageView.
        //return convertView;

       }

               else
               {

                    holder= (ViewHolder) row.getTag();
                    Log.d("recycle", "Recycling stuff");

               }

               final SharedPreferences.Editor editor = state.edit();
                holder.cb.setChecked(state.getBoolean("CheckValue"+position,holder.cb.isChecked() ));


               holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {



                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            //Handle your conditions here

                        editor.putBoolean("CheckValue"+position, holder.cb.isChecked());
                        editor.commit();

                        Toast.makeText(_context,"checkbox:"+position,Toast.LENGTH_LONG).show();

                    }




                    });


                   holder.cb.setTag(position);

             //changing color of items alternatively
                if (position % 2 == 0) {
                    row.setBackgroundColor(Color.parseColor("#58D3F7"));  
                } else {
                    row.setBackgroundColor(Color.parseColor("#F5F6CE"));  
                }

           }
           catch(Exception e)
            {
                e.printStackTrace();

            }
        return row;
    }



}

1 个答案:

答案 0 :(得分:0)

将这些添加到适配器的方法中。我希望这会对你有所帮助。

@Override

  public int getViewTypeCount() {

   return getCount();
  }

  @Override

  public int getItemViewType(int position) {

   return position;
  }