Tuesday, 3 September 2013

Contextual Action Bar and Checkbox problems

Contextual Action Bar and Checkbox problems

So I got as far to as this, please keep in mind that the listView is
dynamic as it is called from an API.
An Adapter for the ListView.
Contextual ActionBar Implementation.
What is now needed.
For the Contextual ActionBar and CheckBox items to sync (like if all the
icons are unchecked, the action bar is to be destroyed)
What I tried:
@Override
public View getView(int position, View convertView, ViewGroup
parent) {
// TODO Auto-generated method stub
ViewHolder vh;
if(convertView == null){
vh = new ViewHolder();
convertView =
mInflater.inflate(R.layout.projectlist_frame, null);
vh.projectTitle =
(TextView)convertView.findViewById(R.id.projecttitle);
vh.projectSector =
(TextView)convertView.findViewById(R.id.projectsector);
vh.cb = (CheckBox)convertView.findViewById(R.id.checkBox1);
convertView.setTag(vh);
} else{
vh = (ViewHolder)convertView.getTag();
}
vh.projectTitle.setText(mData.get(position).get("title").toString());
vh.projectSector.setText(mData.get(position).get("sector").toString());
vh.cb.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
if(mMode==null){
mMode = startActionMode(mActionCallBack);
}
}
else{
if(mMode!=null){
mMode.finish();
}
}
}
});
return convertView;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return super.getViewTypeCount();
}
class ViewHolder{
TextView projectTitle, projectSector;
CheckBox cb;
}
}
For in Contextuals ActionBar
private ActionMode.Callback mActionCallBack = new ActionMode.Callback() {
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
for (int i = 0; i < list.getAdapter().getCount(); i++)
list.setItemChecked(i, false);
if (mode == mMode) {
mMode = null;
}
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
mode.setTitle("Options");
mode.getMenuInflater().inflate(R.menu.contextual_menu_add_project,
menu);
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
long[] selected = list.getCheckedItemIds();
if (selected.length > 0) {
for (long id: selected) {
// Do something with the selected item
}
}
mode.finish();
return false;
}
};
Can somebody please help me track the onCheck events and coordinate its
actions with contextual actionBar.

No comments:

Post a Comment