2015-09-29 66 views
0

我可以存儲使用共享偏好複選框的值,但一旦我滾動列表視圖中,選中複選框再次變得聽之任之。我無法理解是什麼導致了這一點。很想聽聽你的建議。項目的複選框取消選中獲得滾動時的ListView

這裏是我的代碼:

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; 
    } 



} 

回答

0

這些的方法添加到您的適配器。我希望這能幫到您。

@Override 

    public int getViewTypeCount() { 

    return getCount(); 
    } 

    @Override 

    public int getItemViewType(int position) { 

    return position; 
    } 
+0

是的,它的工作!非常感謝:) – Sri

+0

@Sri,我希望你可以upvote並將其標記爲答案。 :) –

+0

你從我身邊有+1:d –

相關問題