Monday, 19 August 2013

implementing checkBox onCheckedChanged in a custom adapter for listView

implementing checkBox onCheckedChanged in a custom adapter for listView

I am having a hard time figuring out how to implement the contextual
actionBar for my checkBox's onCheckedChanged event, part of the reason
being the custom adapter that was built for it.
The layout is as follows:

So here is how the customAdapter looks like:
public class ArrayListAdapter extends BaseAdapter{
public Context mContext;
public LayoutInflater mInflater;
public ArrayList<HashMap<String,String>> mData;
String[] mKeys;
public ArrayListAdapter(Context context,
ArrayList<HashMap<String,String>> data){
mData = data;
this.mContext = context;
mInflater =
(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mData.size();
}
@Override
public Object getItem(int position) {
// 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) {
// 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 OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
Toast.makeText(mContext, "Checked",
Toast.LENGTH_SHORT).show();
startActionMode(mActionCallBack);
}
}
});
return convertView;
}
class ViewHolder{
TextView projectTitle, projectSector;
CheckBox cb;
}
}
The program is calling on an api(uses the AsyncTask; therefore the number
of items in the layout are unsure). The listView is being dynamically
built, and the code for this is :
@Override
protected void onPostExecute(Void result) {
progressDialog.dismiss();
list = getListView();
ArrayListAdapter adapterMap = new
ArrayListAdapter(Allprojects.this,all_list);
list.setAdapter(adapterMap);
}
Can somebody please help me invoke a proper contextual actionBar
implementation for the checkBox's onCheckedChange (where on button check
and uncheck would lead to the CAB being called/finished and the items
selected are displayed e.g. 1 items selected or 2 items selected).

No comments:

Post a Comment