2013-12-19 59 views
0

我有一個自定義列表視圖與一些元素和複選框。當我點擊一個按鈕。我想知道已經檢查過的元素的位置。 下面的下面是我的代碼知道ListView中單擊的複選框項目?

public class Results extends ListActivity implements OnClickListener{ 
    String[] donorName,donorPhone; 
    int totNumber; 
    Button callBut; 
    ListView listView; 
    List<RowItem> rowItems; 
    public static void main(String[] args) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.results); 
     Intent intent = getIntent(); 
      donorName = intent.getStringArrayExtra("name"); 
      donorPhone = intent.getStringArrayExtra("phone"); 
      totNumber = intent.getExtras().getInt("totDonors"); 
      callBut = (Button)findViewById(R.id.callBut); 
      callBut.setOnClickListener(this); 
      rowItems = new ArrayList<RowItem>(); 
      for (int i = 0; i < totNumber; i++) { 
       RowItem item = new RowItem(donorName[i], donorPhone[i]); 
       rowItems.add(item); 
      } 

      ListAdapter adapter = new MySimpleArrayAdapter(this, 
        R.layout.list_item, rowItems); 
      setListAdapter(adapter); 


    }; 
    /////////////////////////////////////////////////////////////////////////////////////////// 
    public static class MySimpleArrayAdapter extends ArrayAdapter<RowItem> implements OnCheckedChangeListener { 


      Context context; 
      static List<RowItem> donorList = new ArrayList<RowItem>(); 

      public MySimpleArrayAdapter(Context context, int resourceId, 
        List<RowItem> donorList) { 
       super(context, resourceId, donorList); 
       this.context = context; 
       this.donorList = donorList; 
      } 
      private class ViewHolder { 
       Button donorCall,exp; 
       TextView donorName; 
       TextView donorPhone; 
       CheckBox chkBox; 

      } 
      @Override 
      public View getView(final int position, View convertView, ViewGroup parent) { 
       ViewHolder holder = null; 
       final RowItem rowItem = getItem(position); 
      LayoutInflater mInflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      if (convertView == null) { 
       convertView = mInflater.inflate(R.layout.list_item, null); 
       holder = new ViewHolder(); 
       holder.donorName = (TextView) convertView.findViewById(R.id.donorName); 
       holder.donorPhone = (TextView) convertView.findViewById(R.id.donorPhone); 
       holder.donorCall = (Button) convertView.findViewById(R.id.donorCall); 
       holder.chkBox = (CheckBox) convertView.findViewById(R.id.chkBox); 
       convertView.setTag(holder); 

      } 

      else 
       holder = (ViewHolder) convertView.getTag(); 

      holder.donorPhone.setText(rowItem.getdonorPhoneS()); 
      holder.donorName.setText(rowItem.getdonorNameS()); 
      holder.chkBox.setTag(position); 
      holder.chkBox.setOnCheckedChangeListener(this); 
      holder.donorCall.setOnClickListener(new View.OnClickListener(){ 
       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        Log.d("Button Clicked",position+""); 
        Intent startCall = new Intent(Intent.ACTION_CALL); 

        startCall.setData(Uri.parse("tel:" + rowItem.getdonorPhoneS())); 
        context.startActivity(startCall); 
       } 

      }); 
      return convertView; 
      } 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      // TODO Auto-generated method stub 
      int position = (Integer) buttonView.getTag(); 
       if (isChecked) { 
        donorList.get(position).setSelected(true); 
        Log.d("Tag",donorList.get(position).isSelected()+""); 
       } else { 
        buttonView.setSelected(false); 
        Log.d("Unchecked",isChecked+""); 
       } 
       notifyDataSetChanged(); 
     } 

    } 
////////////////////////////////////////////////////////////////////////////////////////////////  
    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     String msgRecipient; 
     Log.d("MSg","Button Clicked"); 
     for (int x = 0; x<totNumber;x++){ 
      if(MySimpleArrayAdapter.donorList.get(x).isSelected()){ 
       Log.d("position Checked",x+""); 
      } 
      else 
       Log.d("position UnChecked",x+""); 
     } 
    } 
} 

當我點擊一個項目,我得到真正的複選框選中日誌。但是當我點擊所有元素都被選中下圖所示的按鈕。

回答

2

您忘記了在getView中設置複選框的選中狀態,因此如果向下滾動/向上滾動,將會顯示舊複選框而不更新。

你需要做的是有一組整數(或一個sparseIntArray,這是更好的),複選框被選中時添加項目的位置,當他們沒有選中時刪除。

爲了讓所有的檢查checkboxed的,只是用這個整數集...

0

你可以試試這個就知道適配器選擇的項目位置。

   for(int i=0;i<MySimpleArrayAdapter.mCheckStates.size();i++) 
       { 
        if(MySimpleArrayAdapter.mCheckStates.get(i)==true) 
        { 
          // i is the position of a checked items 

        } 

       } 
0

這裏可能會遇到一些問題。首先,你要確保你的ListView(ListActivity內部)的setChoiceMode具有默認值以外的其他值。即:

listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

詳情參見:Losing current selection when calling adapter.notifyDataSetChanged()

此外,您來notifyDataSetChanged()調用可能會導致在此描述您的選擇重置:Losing current selection of list item after updating the listview items using notifyDataSetChanged

相關問題