我有一個自定義列表視圖與一些元素和複選框。當我點擊一個按鈕。我想知道已經檢查過的元素的位置。 下面的下面是我的代碼知道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+"");
}
}
}
當我點擊一個項目,我得到真正的複選框選中日誌。但是當我點擊所有元素都被選中下圖所示的按鈕。