嗨,这是你需要的:
public class MusicTrack extends Activity {
private ArrayAdapter<CardDetails> listAdapter ;
ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();
CardDetailsAdapter mListAdapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ArrayList<CardDetails> cardResults = GetCardDetails();
for (int i = 0; i < cardResults.size(); i++) {
System.out.println("card redsults: " + cardResults.get(i).isSelected());
}
final ListView welcomelist = (ListView) findViewById(R.id.welcomelistID);
listAdapter = new CardDetailsAdapter(this, cardResults);
welcomelist.setAdapter(listAdapter );
final Button saveBtn = (Button) findViewById(R.id.saveID);
saveBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// get the checkbox status and save related details into database
// Intent intent = new Intent(MusicTrack.this, TransactionSummaryActivity.class);
itemChecked = mListAdapter.itemChecked1;
System.out.println("item checked: " + itemChecked);
for (int i = 0; i < cardResults.size(); i++) {
System.out.println("cardResults and checked status: " + cardResults.get(i).getText());
System.out.println("cardResults and checked status111: " + cardResults.get(i).isSelected());
}
}
});
}
private ArrayList<CardDetails> GetCardDetails() {
ArrayList<CardDetails> carddetailresults = new ArrayList<CardDetails>();
CardDetails sr1 = new CardDetails();
sr1.setText("Text1");
sr1.setSelected(false);
carddetailresults.add(sr1);
sr1 = new CardDetails();
sr1.setText("Text2");
sr1.setSelected(true);
carddetailresults.add(sr1);
sr1 = new CardDetails();
sr1.setText("Text3");
sr1.setSelected(true);
carddetailresults.add(sr1);
sr1 = new CardDetails();
sr1.setText("Text4");
sr1.setSelected(true);
carddetailresults.add(sr1);
sr1 = new CardDetails();
sr1.setText("Text5");
sr1.setSelected(false);
carddetailresults.add(sr1);
return carddetailresults;
}}
class CardDetails{
String text;
Boolean selected;
public String getText() {
return text;
}
public void setText(String bankName) {
text = bankName;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}}
class CardDetailsAdapter extends ArrayAdapter<CardDetails> {
private static ArrayList<CardDetails> searchArrayList;
public static ArrayList<Integer> alSelCards = new ArrayList<Integer>();
public static ArrayList<Boolean> itemChecked1 = null;
private LayoutInflater mInflater;
protected Context getContext;
public CardDetailsAdapter( Context context, ArrayList<CardDetails> results) {
super( context, R.layout.webview, R.id.textID, results );
// Cache the LayoutInflate to avoid asking for a new one each time.
searchArrayList = results;
getContext = context;
mInflater = LayoutInflater.from(context) ;
itemChecked1 = new ArrayList<Boolean>();
for (int i = 0; i < results.size(); i++) {
itemChecked1.add(i, results.get(i).isSelected()); // initializes all items value with
}
}
public int getCount() {
if (searchArrayList != null)
return searchArrayList.size();
return 0;
}
public CardDetails getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView,
final ViewGroup parent) {
CardDetails planet = (CardDetails) this.getItem(position);
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.webview, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.textID);
holder.chkbox = (ToggleButton) convertView.findViewById(R.id.togID);
convertView.setTag(holder);
holder.text.setText(searchArrayList.get(position).getText());
holder.chkbox.setTag(planet);
holder.chkbox.setChecked(planet.isSelected());
} else {
holder = (ViewHolder) convertView.getTag();
ViewHolder viewHolder = (ViewHolder) convertView.getTag();
holder.chkbox = viewHolder.getCheckBox();
holder.text = viewHolder.getText();
holder.text.setText(searchArrayList.get(position).getText());
holder.chkbox.setTag(planet);
holder.chkbox.setChecked(planet.isSelected());
}
holder.chkbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ToggleButton cb = (ToggleButton) v;
CardDetails planet = (CardDetails) cb.getTag();
//planet.setSelected(cb.isChecked());
if (holder.chkbox.isChecked()) {
itemChecked1.set(position, true);
holder.chkbox.setChecked(true);
planet.setSelected(true);
} else if (!holder.chkbox.isChecked()) {
itemChecked1.set(position, false);
holder.chkbox.setChecked(false);
planet.setSelected(false);
}
}
});
return convertView;
}
public static class ViewHolder {
private ToggleButton chkbox;
TextView text;
public ViewHolder() {
}
public ViewHolder(TextView text, ToggleButton chkbox) {
this.chkbox = chkbox;
this.text = text;
}
public ToggleButton getCheckBox() {
return chkbox;
}
public void setCheckBox(CheckBox checkBox) {
this.chkbox = chkbox;
}
public TextView getText() {
return text;
}
public void setText(TextView text) {
this.text = text;
}
}}
导入需要的东西