English 中文(简体)
利用习俗改编者编制的清单核对箱清单
原标题:Saving the list of checkboxes of listview built using a custom adapter

现在,这在很长一段时间里一直困扰着我。 但是,我仍然无法在使用习俗适应者编制的清单中说明如何拯救检查箱的状况。 这里是我的习俗。 任何帮助都将受到高度赞赏。 谢谢。

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

}

问题回答

你可以尝试:

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

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;

    }  




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签