保存使用自定义适配器内置的ListView复选框名单 [英] Saving the list of checkboxes of listview built using a custom adapter

查看:106
本文介绍了保存使用自定义适配器内置的ListView复选框名单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在这一点是已经困扰了我很长一段时间了。但不知何故,我依然没能弄清楚如何保存使用自定义适配器内置了列表视图复选框的状态。这是我的CustomAdapter。任何帮助将大大AP preciated。谢谢你。


 公共类ListAdapter延伸BaseAdapter {
  布尔[] = itemChecked新的布尔[20];
  公共字符串标题[];
公共字符串描述[];
公共活动背景;
公共LayoutInflater吹气;
HttpClient的HT =新DefaultHttpClient()
公共ListAdapter(活动的背景下,的String []标题的String []描述){
    超();
    的for(int i = 0; I< itemChecked.length;我++)
    {
        itemChecked [I] = FALSE;
    }
    this.context =背景;
    this.title =称号;
    this.description =描述;


  this.inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}公共静态类ViewHolder
{    TextView的txtViewTitle;
    TextView的txtViewDescription;
    复选框CB;
}@覆盖
公众诠释的getCount(){
    // TODO自动生成方法存根
    返回title.length;
}
@覆盖
公共对象的getItem(INT位置){
    // TODO自动生成方法存根
    返回null;
}
@覆盖
众长getItemId(INT位置){
    // TODO自动生成方法存根
    返回0;
}
@覆盖
公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
    // TODO自动生成方法存根
    最后ViewHolder持有人;    LayoutInflater吹气= context.getLayoutInflater();
    如果(convertView == NULL)
    {
        convertView = inflater.inflate(R.layout.custom_list,NULL);
        持有人=新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 =(复选框)convertView.findViewById(R.id.cb);        convertView.setTag(保持器);    }    其他
    {
        支架=(ViewHolder)convertView.getTag();     }    holder.cb.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){            @覆盖
            公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
                // TODO自动生成方法存根                 itemChecked [位置] =器isChecked;
                 如果(itemChecked [位置])
                 {
                     holder.cb.setChecked(真);
                 }
                 其他
                 {
                     holder.cb.setChecked(假);
                 }                布尔子=器isChecked;
        }
            }
        });
布尔项目[] = load()的;
    holder.txtViewTitle.setText(标题[位置]);
    holder.txtViewDescription.setText(说明[位置]);
    holder.cb.setChecked(项目[位置]);
  holder.txtViewDescription.setFocusable(假);
  holder.txtViewTitle.setFocusable(假);
 保存(itemChecked);
返回convertView;}无效认购(列表<&的NameValuePair GT; namevaluepairs中,布尔子)
    {
    如果(子== true)而
    {          尝试{
            subscription.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
            ht.execute(订阅);
        }赶上(UnsupportedEncodingException E1){
            // TODO自动生成catch块
            e1.printStackTrace();
        }赶上(ClientProtocolException E){
            // TODO自动生成catch块
            e.printStackTrace();
        }赶上(IOException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
        }    }
其他
{
    如果(子== FALSE)
    {
        {              尝试{
                unSubscription.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
                尝试{
                    ht.execute(退订);
                }赶上(ClientProtocolException E){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }赶上(IOException异常五){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }
            }赶上(UnsupportedEncodingException E1){
                // TODO自动生成catch块
                e1.printStackTrace();
            }        }
}
}
    }
   私人无效保存(最终布尔[]器isChecked){
    共享preferences共享preferences = context.get preferences(Context.MODE_PRIVATE);
    共享preferences.Editor编辑=共享preferences.edit();
    对于(整数i = 0; I< isChecked.length;我++)
     {
         editor.putBoolean(i.toString(),器isChecked [I]);
     }
    editor.commit();
    }
  公共布尔[]负载(){
    共享preferences共享preferences = context.get preferences(Context.MODE_PRIVATE);
        布尔[] =复查新的布尔[itemChecked.length]
        对于(整数i = 0; I< itemChecked.length;我++)
        {
             复查[I] =共享preferences.getBoolean(i.toString(),FALSE);
        }
        返回复查;
    }

}


解决方案

您可以试试这个:

  holder.cb.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){        @覆盖
        公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
            // TODO自动生成方法存根             itemChecked [位置] =器isChecked;
             如果(itemChecked [位置])
             {
                 holder.cb.setChecked(真);
             }
             其他
             {
                 holder.cb.setChecked(假);
             }             布尔子=器isChecked;             ***保存(itemChecked); ***
       }
});

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

}

解决方案

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

这篇关于保存使用自定义适配器内置的ListView复选框名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆