2012-04-10 67 views
0

现在,这件事现在已经困扰了我很长一段时间。但不知何故,我仍然无法弄清楚如何在使用自定义适配器构建的listview中保存复选框的状态。这是我的CustomAdapter。任何帮助都感激不尽。谢谢。保存使用自定义适配器构建的listview复选框列表

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

}

+0

做了这些帮助吗? – 2014-08-06 03:47:22

回答

0

试试这个,而不是使用OnCheckedChangeListener的,只是用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; 

    } 
1

你可以试试这个:

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);*** 
     } 
}); 
相关问题