Saving the list of checkboxes of listview built using a custom adapter

感情迁移 提交于 2019-12-12 03:31:44

问题


Now this is something that has been bothering me for a long time now. But somehow I still am not able to figure out how to save the state of checkboxes in a listview built using a custom adapter. Here is my CustomAdapter. Any help will be much appreciated. Thank you.

 public class ListAdapter  extends BaseAdapter{
  boolean[] itemChecked=new boolean[20];
  public String title[];  
public String description[];  
public Activity context;  
public LayoutInflater inflater;
HttpClient ht = new DefaultHttpClient() 
public ListAdapter(Activity context,String[] title, String[] description) {  
    super();  
    for(int i=0;i<itemChecked.length;i++)
    {
        itemChecked[i]=false;
    }
    this.context = context;  
    this.title = title;  
    this.description = description;  
    this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
}  

public static class ViewHolder  
{  

    TextView txtViewTitle;  
    TextView txtViewDescription;  
    CheckBox cb;
}  

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return title.length; 
}
@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}
@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    final ViewHolder holder;  



    LayoutInflater inflater =  context.getLayoutInflater();  
    if(convertView==null)  
    {  
        convertView = inflater.inflate(R.layout.custom_list, null);
        holder = new ViewHolder();  
        holder.txtViewTitle = (TextView) convertView.findViewById(R.id.title_text);  
        holder.txtViewTitle.setTextColor(Color.parseColor("#008ab5"));
        holder.txtViewDescription = (TextView) convertView.findViewById(R.id.description_text);
        holder.txtViewDescription.setTextColor(Color.parseColor("#008ab5"));
        holder.cb=(CheckBox) convertView.findViewById(R.id.cb);

        convertView.setTag(holder);  

    }  

    else  
    {
        holder=(ViewHolder)convertView.getTag();  

     }  

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

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

                 itemChecked[position] = isChecked;
                 if(itemChecked[position])
                 {
                     holder.cb.setChecked(true);
                 }
                 else
                 {
                     holder.cb.setChecked(false);
                 }

                boolean sub=isChecked;


        }
            }        
        });
boolean item[]=load();
    holder.txtViewTitle.setText(title[position]);  
    holder.txtViewDescription.setText(description[position]);  
    holder.cb.setChecked( item[position]);
  holder.txtViewDescription.setFocusable(false);
  holder.txtViewTitle.setFocusable(false);
 save(itemChecked);
return convertView;  

}  

void subscribe(List<NameValuePair> nameValuePairs,boolean sub)
    {
    if(sub==true)
    {

          try {
            subscription.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            ht.execute(subscription);
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   

    }
else
{
    if(sub==false)
    {
        {

              try {
                unSubscription.setEntity(new     UrlEncodedFormEntity(nameValuePairs));
                try {
                    ht.execute(unSubscription);
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } catch (UnsupportedEncodingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }   

        }
}


}   
    }
   private void save(final boolean[] isChecked) {
    SharedPreferences sharedPreferences = context.getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    for(Integer i=0;i<isChecked.length;i++)
     {
         editor.putBoolean(i.toString(), isChecked[i]);
     }
    editor.commit();
    }
  public boolean[] load() {
    SharedPreferences sharedPreferences = context.getPreferences(Context.MODE_PRIVATE);
        boolean [] reChecked = new boolean[itemChecked.length];
        for(Integer i = 0; i < itemChecked.length; i++)
        {
             reChecked[i] = sharedPreferences.getBoolean(i.toString(), false);
        }
        return reChecked;
    }

}


回答1:


You can try this:

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

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

             itemChecked[position] = isChecked;
             if(itemChecked[position])
             {
                 holder.cb.setChecked(true);
             }
             else
             {
                 holder.cb.setChecked(false);
             }

             boolean sub=isChecked;

             ***save(itemChecked);***
       }
});



回答2:


Try this, Instead of using OnCheckedChangeListener, just used OnClickListener :

   @Override
    public View getView(final int position, View convertView, ViewGroup arg2) {
        // TODO Auto-generated method stub
        final ViewHolder holder;  
        LayoutInflater inflater =  ((Activity) context).getLayoutInflater();  
        if(convertView==null)  
        {  
            convertView = inflater.inflate(R.layout.custom_list, null);
            holder = new ViewHolder();  

             holder.txtViewTitle = (TextView) convertView.findViewById(R.id.title_text);  

             holder.txtViewDescription = (TextView) convertView.findViewById(R.id.description_text);

            holder.cb=(CheckBox) convertView.findViewById(R.id.cb);

            convertView.setTag(holder);  

        }  

        else  
        {
            holder=(ViewHolder)convertView.getTag();  

         }  

        holder.txtViewTitle.setTextColor(Color.parseColor("#008ab5"));
        holder.txtViewTitle..setText(title[position]);
        holder.txtViewDescription.setTextColor(Color.parseColor("#008ab5"));
        holder.txtViewDescription.setText(description[position]);
        if (itemChecked[position])
              holder.cb.setChecked(true);
         else
              holder.cb.setChecked(false);

        holder.cb.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if (holder.cb.isChecked())
                        itemChecked[position] = true;
                     else
                         itemChecked[position] = false;
                }        
        });
        return convertView;

    }  


来源:https://stackoverflow.com/questions/10088464/saving-the-list-of-checkboxes-of-listview-built-using-a-custom-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!