I'm having requirement where i need to get the checkbox value count when i press a button. If the count is greater than 0 then i need to do some operations. I'm trying to do as below. checkbox is in listview which extends baseadapter.
Below is my adpater which extends baseapdater
public class PreferencesSyncAdapter extends BaseAdapter {
private Context context;
private LayoutInflater mInflater;
public PreferencesSyncAdapter(Context context) {
this.context = context;
// Cache the LayoutInflate to avoid asking for a new one each time.
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return syncListItems.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SyncPreferenceItem syncCatItem = syncListItems.get(position);
ISyncCategoryEnum test = syncCatItem.getSyncCategory();
boolean isChecked = test.isSyncEnabled(this.context);
// A ViewHolder keeps references to children views to avoid unneccessary calls
// to findViewById() on each row.
ViewHolder holder;
// When convertView is not null, we can reuse it directly, there is no need
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_preferences_sync, null);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.syncItemTV = (TextView) convertView.findViewById(R.id.syncItemTV);
holder.syncStatusTV = (TextView) convertView.findViewById(R.id.syncStatusTV);
holder.syncItemIV = (ImageView) convertView.findViewById(R.id.syncItemIV);
holder.syncCB = (CheckBox) convertView.findViewById(R.id.syncCB);
holder.progressBar = (ProgressBar) convertView.findViewById(R.id.progress_spinner);
holder.syncCB.setChecked(isChecked);
setCheckBox(holder, isChecked);
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
holder.syncCB.setChecked(isChecked);
setCheckBox(holder, isChecked);
}
if (convertView == null) return convertView;
//CheckBox cb = (CheckBox) convertView.findViewById(R.id.custCB);
holder.syncCB.setOnClickListener(new DefaultOnClickListener(this.context, syncCatItem, holder));
// Bind the data efficiently with the holder.
//holder.custNameTV.setText(ReturnCustomerFilter.mAttributesArray[position]);
holder.syncItemTV.setText(syncCatItem.getStrRes());
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm");
/*Date lastSyncDate = syncCatItem.getSyncCategory().getLastSyncTime();
Calendar cal = Calendar.getInstance();
cal.setTime(lastSyncDate);
//sdf.setCalendar(cal);
Calendar syncTimeCal = DateUtil.convertTimeToLocal(TimeZone.getDefault(), cal);
sdf.setCalendar(syncTimeCal);*/
holder.syncStatusTV.setText(this.context.getResources().getString(R.string.sync_progress_last_synchronized) +
" : " + sdf.format(new Date(syncCatItem.getSyncCategory().getLastSyncTime(PreferencesSync.this.mContext))));
holder.syncItemIV.setImageResource(syncCatItem.getImageRes());
holder.syncStatusTV.setTextColor(isChecked ? 0xff72bf44 : 0xffc43131);
return convertView;
}
private void setCheckBox(ViewHolder holder, boolean isChecked) {
holder.syncCB.setChecked(isChecked);
if (SynchronizationManager.getInstance().isSyncAllCategoryGroupActive()) {
holder.syncCB.setVisibility(View.GONE);
if (isChecked) {
holder.progressBar.setVisibility(View.VISIBLE);
} else {
holder.progressBar.setVisibility(View.GONE);
}
} else {
holder.syncCB.setVisibility(View.VISIBLE);
holder.progressBar.setVisibility(View.GONE);
}
}
class DefaultOnClickListener implements OnClickListener {
private Context context;
private SyncPreferenceItem syncCategoryItem;
private ViewHolder holder;
public DefaultOnClickListener(Context context,
SyncPreferenceItem syncCategoryItem,
ViewHolder holder) {
this.context = context;
this.syncCategoryItem = syncCategoryItem;
this.holder = holder;
}
@Override
public void onClick(View view) {
if (view.getId() == holder.syncCB.getId()) {
syncCategoryItem.getSyncCategory().setIsSyncEnabled(this.context, holder.syncCB.isChecked());
PreferencesSync.this.refreshList();
}
}
}
class ViewHolder {
ProgressBar progressBar;
TextView syncItemTV;
TextView syncStatusTV;
ImageView syncItemIV;
CheckBox syncCB;
}
}
I'm using the sparseboolean array as below to get all the checkeditem position. But it always returns null.
SparseBooleanArray checkedPositions = xmlSyncItemsListView.getCheckedItemPositions();
Log.i("sha","checkedPositions: " + checkedPositions.size());
if (checkedPositions != null)
{
int count = mXmlPreferencesSyncAdpt.getCount();
for ( int i=0;i<count;i++)
{
Log.i("","Selected items: " + checkedPositions.get(i));
}
}
Please help me to solve the issue.
Aucun commentaire:
Enregistrer un commentaire