Click Listener + Checkbox - On Android
public class ChooseFavorites extends Activity implements OnItemClickListener { StationManager st; MyCustomAdapter arr; ListView list; @Override protected vo
Solution 1:
You can use this event to get notified about the checkbox being checked/unchecked
cb.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {
@OverridepublicvoidonCheckedChanged(CompoundButton buttonView,boolean isChecked) {
}
}
Solution 2:
Use Checkbox setOnCheckedChangeListener event.
Try like this...
CheckBoxChkBx= ( CheckBox ) findViewById( R.id.checkbox);
ChkBx.setOnCheckedChangeListener(newOnCheckedChangeListener()
{
publicvoidonCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// perform logic
}
}
});
Solution 3:
You can use following:
listView.setOnItemClickListener(newOnItemClickListener() {
@OverridepublicvoidonItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + position, Toast.LENGTH_LONG)
.show();
}
});
Or u can save position as tag to checkbox
and check on checkbox clicklistener
@Overridepublic View getView(int position, View convertView, ViewGroup parent)
{
// ur code...
**cb.setTag(position);**
row.setOnClickListener(this);
return row;
}
@OverridepublicvoidonClick(View v) {
// TODO Auto-generated method stub
**intposition= v.getTag();** //cast & check for Tag not null too.Toasttoast= Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT);
toast.show();
}
Solution 4:
@Overridepublic View getView(finalint position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stubViewrow= convertView;
if(row==null)
{ // Object reuse
inflater = getLayoutInflater();
row = inflater.inflate(R.layout.favorite_row, parent, false);
}
stationName1 = (TextView)row.findViewById(R.id.textFavoriteItemInRow);
stationName1.setText(stationNames[position]);
cb=(CheckBox)row.findViewById(R.id.cbCheck);
cb.setOnCheckedChangeListener(newCompoundButton.
OnCheckedChangeListener(){
@OverridepublicvoidonCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (isChecked)
{
cb.setChecked(isChecked);
Toasttoast= Toast.makeText(getApplicationContext(), "checked" + position, Toast.LENGTH_SHORT);
toast.show();
}
else
{
cb.setChecked(isChecked);
Toasttoast= Toast.makeText(getApplicationContext(), "unChecked" + position, Toast.LENGTH_SHORT);
toast.show();
}
}
});
return row;
}
}
Solution 5:
public View getView(int position, View convertView, ViewGroup parent) {
viewHolder holder;
// Reuse an existing view, if one is supplied, otherwise create a new one// Check to see if this one is created yet ?if (convertView == null){
// Get inflaterLayoutInflaterinflater= (LayoutInflater)
parentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Create a view for it
convertView = inflater.inflate(R.layout.test_reprot_lv_items, parent, false);
// Creates a holder class to contain all objects of a row
holder = newviewHolder();
holder.equipmentID = (TextView) convertView.findViewById(R.id.equipmentID);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++// Set checkBox's Tag with the position of the row in the listview
holder.checkBox.setTag(position);
// Sets the tag associated with this view for later reuse.
convertView.setTag(holder);
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
holder.checkBox.setOnClickListener(newOnClickListener(){
@OverridepublicvoidonClick(View v) {
CheckBoxc= (CheckBox) v;
intlocation= (Integer) c.getTag();
// Set selected state with the location of the row in the listview
itemsList.get(location).setSelectedState(c.isChecked());
}
});
}
else{
// In order to reuse the preallocated memory, get the holder from View's tag, // which is allocated and saved in this view object. When a view requests // to be rendered and it has not been instantiated, we new a holder and save// it to View's 'Tag' for later reuse.
holder = (viewHolder) convertView.getTag();
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++// Set checkBox's Tag with the position of the row in the listview
holder.checkBox.setTag(position);
holder.checkBox.setOnClickListener(newOnClickListener(){
@OverridepublicvoidonClick(View v) {
CheckBoxc= (CheckBox) v;
intlocation= (Integer) c.getTag();
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++// Set selected state with the location of the row in the listview// The method setSelectedState, which provided by your own class,// is called to update the checkbox state.
itemsList.get(location).setSelectedState(c.isChecked());
}
});
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++// Set checkbox state with calling the method getSelectedState which // provied by your own class.
holder.checkBox.setChecked(itemsList.get(position).getSelectedState());
return convertView;
}
Post a Comment for "Click Listener + Checkbox - On Android"