2013-06-24 34 views
0

我使用片段,其中我有ListView和全選複選框,選擇所有listview複選框。當我們選擇全選複選框android

片段類..

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

fb_custom_listview = (ListView) selectfriend_login 
       .findViewById(R.id.friend_listview); 

     select_all=(CheckBox) selectfriend_login.findViewById(R.id.select_all_checkbox); 

     select_all.setOnCheckedChangeListener(this); 

} 

現在具有custom adapter class extends Baseadapter 其中IM具有一個ImageView,一個TextView並在layout一個checkbox和膨脹在baseadapter class.everything的getview方法的佈局是確定它顯示圖像,顯示文本,並顯示覆選框也so.so現在當我點擊select_all(fragment class)複選框時,應選中自定義適配器類的所有複選框,並且未選中全部未選中。這裏是適用於自定義適配器的類

public class CustomListAdapter extends BaseAdapter 
{ 
    private Context contxt; 
    private ArrayList<String> user_friend_name; 
    private ArrayList<String> user_friend_pic_url; 
    private LayoutInflater inflater; 
    private int friend_counter=0; 
    private ChangeUIListner mlistner; 
    public static ArrayList<Integer> selected_friend_pos; 
    public static ArrayList<String> selected_friend_uid; 
    public static ArrayList<String> selected_friend_name; 
    private List<Model> list; 
    private ViewHolder viewholder=null; 



    public CustomListAdapter(Context context,ArrayList<String> user_friend_picurl, List<Model> model) { 
     // TODO Auto-generated constructor stub 
     this.contxt=context; 
     this.list=model; 
     user_friend_pic_url=user_friend_picurl; 

     selected_friend_uid=new ArrayList<String>(); 
     selected_friend_name=new ArrayList<String>(); 
     selected_friend_pos=new ArrayList<Integer>(); 
     user_friend_name=GetFriendDetails.user_fb_friend_name; 
     inflater=(LayoutInflater) contxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 


     System.gc(); 


    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return user_friend_pic_url.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 



     if(convertView==null) 
     { 
      viewholder=new ViewHolder(); 

      convertView=inflater.inflate(R.layout.custom_listview_friendlist, null); 

      viewholder.fb_friend_name=(TextView) convertView.findViewById(R.id.friend_nametextview); 

      viewholder.fb_friend_pic=(ImageView) convertView.findViewById(R.id.friend_picview); 

      viewholder.fb_checkbox=(CheckBox) convertView.findViewById(R.id.friendselectcheckBox); 

      viewholder.fb_checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
        // TODO Auto-generated method stub 
        int getposition=(Integer) buttonView.getTag(); 
        Log.e("ITEM POSITION", "."+getposition); 

        list.get(getposition).setSelected(buttonView.isChecked()); 

        if(isChecked==true) 
        { 
         if(selected_friend_pos.contains(getposition)) 
         { 
          //nothing 
         } 
         else 
         { 
          friend_counter++; 
          mlistner.UpdateUi(friend_counter); 
          selected_friend_pos.add(getposition); 
          selected_friend_uid.add(GetFriendDetails.user_fb_friend_id.get(getposition)); 
          selected_friend_name.add(GetFriendDetails.user_fb_friend_name.get(getposition)); 
          Log.e("ARRAY SIZE", "."+selected_friend_uid.size()); 
         } 
        } 
        else 
        { 
         if(selected_friend_pos.contains(getposition)){ 
          friend_counter--; 
          mlistner.UpdateUi(friend_counter); 
          int del_posi=selected_friend_pos.indexOf(getposition); 
          selected_friend_pos.remove(del_posi); 
          selected_friend_name.remove(del_posi); 
          selected_friend_uid.remove(del_posi); 
          Log.e("ARRAYLIST", "REM"+del_posi); 
          Log.e("ARRAY SIZE", "."+selected_friend_uid.size()); 

         } 

        } 

       } 
      }); 

      convertView.setTag(viewholder); 

     } 
     else 
     { 
      viewholder=(ViewHolder) convertView.getTag(); 
      new AsyncDownloadImage(viewholder).execute(user_friend_pic_url.get(position)); 
     } 


     viewholder.fb_checkbox.setTag(position); 

     viewholder.fb_checkbox.setChecked(list.get(position).isSelected()); 

     viewholder.fb_friend_name.setText(user_friend_name.get(position)); 

     viewholder.fb_friend_name.setTag(user_friend_name.get(position)); 

     viewholder.fb_friend_pic.setTag(user_friend_pic_url.get(position)); 

     //new AsyncDownloadImage(viewholder).execute(user_friend_pic_url.get(position)); 


     return convertView; 
    } 


    class ViewHolder 
    { 
     int id; 
     ImageView fb_friend_pic; 
     TextView fb_friend_name; 
     CheckBox fb_checkbox; 
    } 

    public class AsyncDownloadImage extends AsyncTask<String, Object, Bitmap> 
    { 


     private HttpURLConnection connection; 
     private InputStream is; 
     private Bitmap bitmap; 
     private URL url; 
     ViewHolder holder; 


     public AsyncDownloadImage(ViewHolder viewholder1) { 
      // TODO Auto-generated constructor stub 
      holder=viewholder1; 
     } 



     @Override 
     protected Bitmap doInBackground(String... params) { 
      // TODO Auto-generated method stub 

      try { 
       url=new URL(params[0]); 

       try { 
        connection=(HttpURLConnection) url.openConnection(); 
        connection.setDoInput(true); 
        connection.connect(); 
        is=connection.getInputStream(); 
        bitmap=BitmapFactory.decodeStream(is); 
        is.close(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       finally 
       { 
        if(is!=null) 
        { 
         try { 
          is.close(); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
        }if(connection!=null) 
        { 
         connection.disconnect(); 
        } 
       } 

      } catch (MalformedURLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 


      return bitmap; 
     } 

     @Override 
     protected void onPostExecute(Bitmap result) { 
      // TODO Auto-generated method stub 
       if(result!=null) 
       { 
        holder.fb_friend_pic.setImageBitmap(result); 
       } 

     } 
    } 

    public void registerUIupdateListener(ChangeUIListner lisnter) 
    { 
     mlistner=lisnter; 
    } 
} 

如u可以看到我用了單次選擇的事情爲ListView複選框,以便其做工精細,並添加到ArrayList中進一步operation.so我們如何能夠實現全選時,複選框,單擊或選擇。謝謝

回答

1

當你點擊selectAll check按鈕,然後調用selectAllFromList(value)函數。 像

 selectAllButton.setOnClickListener(new OnClickListener() {  
    @Override 
     public void onClick(View v) { 
      //is chkIos checked? 
    if (((CheckBox) v).isChecked()) 
    { 
     selectAllFromList(true); 
    }else 
    { 
     selectAllFromList(false); 
    } 

    } 

下列功能就會從列表中選擇所有複選框:

private void selectAllFromList(boolean b) { 
      int cList = list.getCount(); 
      for(int i=0;i<cList;i++){ 
       View sView = yourMainList.getChildAt(i); 
      if(sView != null){ 
       CheckBox childCheckBox=(CheckBox)sView.findViewById(R.id.urCheckButton); 
        childCheckBox.setChecked(b); 
        childCheckBox.setOnClickListener(new OnClickListener() { 

         @Override 
         public void onClick(View v) { 
          if (((CheckBox) v).isChecked()) 
          { 
           selectAllButton.setChecked(false); 
          } 
         } 
         }); 
       } 
      } 
     } 
     }); 

希望這一個幫助你

+0

的selectAllFromList方法是分段類如提及上述 – blackjack

+0

試試這個代碼在你的類中創建的listView不在適配器類中。不便之處 –

+1

+1 @DhananjayC偉大的答案,你節省了我的時間和搜索..保持.... – Simmant