0

今、これは長い間私を悩ませてきたものです。しかし、どういうわけか、カスタムアダプターを使用して構築されたリストビューにチェックボックスの状態を保存する方法をまだ理解できていません。これが私の CustomAdapter です。どんな助けでも大歓迎です。ありがとうございました。

 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 に答える