2012-12-29 69 views
0

我製造具有checkbox.The問題的自定義列表視圖是,當我滾動列表視圖,它不檢查檢查成爲了automatically.This項目是我使用的代碼:如何在滾動列表時自動檢查自定義列表視圖中的複選框?

public class ContactAdapterSetRule extends ArrayAdapter<String> { 

public static ArrayList<String> s_itemsOfAdapterArrayList; 
Context m_context; 
public static ArrayList<String> s_checkedIds = new ArrayList<String>(); 

public ContactAdapterSetRule(Context context, int textViewResourceId, ArrayList<String> s_contactNameArrayListSplash) { 
    super(context, textViewResourceId, s_contactNameArrayListSplash); 
    this.s_itemsOfAdapterArrayList = s_contactNameArrayListSplash; 
    this.m_context = context; 
    s_checkedIds.clear(); //Clearing the list of Checked Ids 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View l_view = convertView; 
    if (l_view == null) 
    { 
     LayoutInflater l_vi = (LayoutInflater)m_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     l_view = l_vi.inflate(R.layout.showthecontacts, null); 

     ViewHolder viewHolder = new ViewHolder(); 
     viewHolder.l_nameCheckBox = (CheckBox) l_view.findViewById(R.id.checkBox);; 

     l_view.setTag(viewHolder); 
    } 


    String l_nameOfContact = s_itemsOfAdapterArrayList.get(position); 
    ViewHolder holder = (ViewHolder) l_view.getTag(); 

    if (l_nameOfContact != null) 
    { 
     if (holder.l_nameCheckBox != null) 
     { 
      holder.l_nameCheckBox.setText(l_nameOfContact); 
     } 

     holder.l_nameCheckBox.setOnClickListener(new OnItemClickListener(position,holder.l_nameCheckBox.getText(),holder.l_nameCheckBox));   
    } 
    return l_view; 
} 

private class ViewHolder 
{ 
    CheckBox l_nameCheckBox; 
} 

private class OnItemClickListener implements OnClickListener 
{   
    private int l_positionOfItemClicked; 
    private CharSequence l_text; 
    private CheckBox l_checkBox; 
    OnItemClickListener(int position, CharSequence text,CheckBox checkBox) 
    { 
     this.l_positionOfItemClicked = position; 
     this.l_text = text; 
     this.l_checkBox = checkBox; 
    } 
    @Override 
    public void onClick(View arg0) 
    { 
     if(l_checkBox.isChecked()) 
     { 
      Log.v("","Checkbox is checked"); 
      for(int i = 0;i < s_checkedIds.size();i++) 
       Log.v("",s_checkedIds.get(i)); 

      if(!s_checkedIds.contains(l_text.toString())) 
      { 
       s_checkedIds.add(l_text.toString()); 
       Log.e("",l_text.toString()+" is added to s_checkedIds"); 
      } 
     } 
     else 
     { 
      Log.v("","Checkbox is Unchecked"); 

      for(int i = 0;i < s_checkedIds.size();i++) 
       Log.v("",s_checkedIds.get(i)); 
      if(s_checkedIds.contains(l_text.toString())) 
      { 
       s_checkedIds.remove(l_text.toString()); 
       Log.e("",l_text.toString()+" is removed from s_checkedIds"); 
      } 
     } 
    } 

請幫助我不知道如何去做。提前感謝。

+0

沒有必要限制滾動此。只需爲您創建觀察者複選框 –

+1

這是重複使用視圖。您需要跟蹤檢查的項目。將它們存儲在列表中,並在getview方法中取消選中不在列表中的列表。 –

+0

http://stackoverflow.com/questions/12427262/get-checked-listitems-from-listview-and-pass-that-to-another-activity/12427720#12427720。我認爲這會更容易。也不要使用'ArrayList'來存儲選中的項目。最好使用'SparseBooleanArray',因爲他們阻止自動裝箱 –

回答

3

This鏈接幫助我解決我的問題..

編碼愉快:)

+1

哎呀!它的我的博客鏈接;) –

+0

@ Lalit Poptani和@ Mitesh Aggarwal非常感謝你們倆。 – user1726619

+1

感謝@LalitPoptani – moDev

相關問題