0

现在这是困扰我很久的事情。但不知何故,我仍然无法弄清楚如何在使用自定义适配器构建的列表视图中保存复选框的状态。这是我的自定义适配器。任何帮助都感激不尽。谢谢你。

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

}

4

2 回答 2

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);***
       }
});
于 2012-10-13T18:51:48.790 回答
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;

    }  
于 2012-04-10T12:31:15.743 回答