2012-10-26 49 views
1

可能重複:
Getting an issue while checking the dynamically generated checkbox through list view列表視圖用複選框狀態改變時scoll

我的ListView與checkboxes.Bydefault所有複選框都checked.After一些複選框取消選中並sroll這些複選框是自動檢查。但我想在滾動列表視圖後檢查這些複選框。 我的代碼是:

public class FriendListAdapter extends BaseAdapter { 

    private LayoutInflater mInflater; 
    DrunkMessages friendsList; 
    boolean[] checkBoxState; 

    public FriendListAdapter(DrunkMessages friendsList) { 
     this.friendsList = friendsList; 
     if (Utility.model == null) { 
      Utility.model = new FriendsGetProfilePics(); 
     } 
     Utility.model.setListener(this); 
     mInflater = LayoutInflater.from(friendsList.getBaseContext()); 

     checkBoxState=new boolean[jsonArray.length()]; 
    } 


    public int getCount() { 
     return jsonArray.length(); 
    } 


    public Object getItem(int position) { 
     return null; 
    } 


    public long getItemId(int position) { 
     return 0; 
    } 

    public View getView(final int position, View convertView, ViewGroup parent) { 

      View hView = getLayoutInflater().inflate(R.layout.friendslist, null); 

      ImageView profile_pic = (ImageView) hView.findViewById(R.id.profile_pic); 
      TextView name = (TextView) hView.findViewById(R.id.name); 

      final CheckBox checkbox=(CheckBox)hView.findViewById(R.id.checkbox); 

     profile_pic.setImageBitmap(Utility.model.getImage(friendid[position], pictures[position])); 
     name.setText(names[position]); 

     checkbox.setOnClickListener(new View.OnClickListener() { 

       public void onClick(View v) { 
       if(((CheckBox)v).isChecked()){ 

       checkBoxState[position]=true; 
       status[position]="1"; 

      Log.e("checked","checked"); 
      Log.e("Checked",status[position]); 
       } 
       else{ 
       checkBoxState[position]=false; 
       status[position]="0"; 

       Log.e("checked","unchecked"); 
      Log.e("Checked",status[position]); 

       } 
       } 
       }); 

     return hView; 
} 
+0

,而不是膨脹的複選框另一個XML,你可以爲你的代碼使用simple_list_item_multiple_choice屬性進行列表視圖。 這樣的 adapter = new ArrayAdapter (this,android.R.layout.simple_list_item_multiple_choice,objectOfList); – sachit

+0

[Link1]可能的重複(http://stackoverflow.com/questions/5538867/customlistview-with-checkbox-while-scrolling-check-state-is-interchanged),[Link2](http://stackoverflow.com/questions/6100518/checkbox-auto-call-oncheckedchange-when-listview-scroll) – AppMobiGurmeet

+1

http://www.lalit3686.blogspot.in/2012/06/today-i-am-going-to-show-how- to-deal.html –

回答

3

convertview可能會給你以前生成的視圖,所以不要依賴它。只是存儲您的複選框的狀態在一個布爾數組&更改,因此取決於這裏checkchangelistener回調是我只是修改你的代碼一切的getView方法保持原樣

public View getView(final int position, View convertView, ViewGroup parent) { 

       View hView = getLayoutInflater().inflate(R.layout.friendslist, null); 

       ImageView profile_pic = (ImageView) hView.findViewById(R.id.profile_pic); 
       TextView name = (TextView) hView.findViewById(R.id.name); 

       final CheckBox checkbox=(CheckBox)hView.findViewById(R.id.checkbox); 

       checkbox.setTag(new Integer(position)); 
      profile_pic.setImageBitmap(Utility.model.getImage(friendid[position], pictures[position])); 
      name.setText(names[position]); 

      checkbox.setOnCheckedChangeListener(null); 
      if(checkBoxState[position]) 
       checkbox.setChecked(true); 
      else 
       checkbox.setChecked(false); 

      checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
        Integer pos = (Integer)buttonView.getTag();  
        if(isChecked){ 

        checkBoxState[pos.intValue()]=true; 
        } 
        else{ 
        checkBoxState[pos.intValue()]=false; 
        Log.e("checked","unchecked"); 

        } 
        } 
        }); 

      return hView; 
    } 
+0

請給我發送代碼 – rams

+0

更新我的回答請現在檢查 –

+0

我得到空指針異常在行:checkBoxState [pos.intValue()] = true; – rams

0

添加以下代碼在你getview方法

if(checkBoxState.length>0) 
checkbox.setChecked(checkBoxState[position]); 
+0

我會建議簡單的checkbox.setChecked(checkBoxState [position]); – sandrstar

+0

也得到相同的問題 – rams

+0

只是看到值checkBoxState [位置]通過日誌貓 – koti

0

,而不是誇大另一個XML的複選框,你可以拿simple_list_item_multiple_choice屬性的列表視圖中。像這樣

adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_multiple_choice,obje‌​ctOfList); 
+0

我用自定義listview.How可以嗎? – rams

相關問題