In my android application I am storing all the details of contact list in mobile to a listview and a checkbox is added to it for selecting a particular contact.. but there is some problem in selecting checkbox, app is crashing while selecting I am giving the code below.. if anyone can help please help
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int count = listView.getCount();
for(int i=0;i<count;i++) {
ViewGroup item = (ViewGroup) listView.getChildAt(i);
checkBox = ((CheckBox) item.findViewById(R.id.selected));
if(checkBox.isChecked()) {
Toast.makeText(SendMessagesActivity.this, "How are u", Toast.LENGTH_LONG)
.show();
}
}
}
});
ContactsAdapter
public class ContactsAdapter extends BaseAdapter
{
private Context context;
private ArrayList<Contact> contacts;
SparseBooleanArray sba=new SparseBooleanArray();
public ContactsAdapter(Context context, ArrayList<Contact> contacts)
{
this.context = context;
this.contacts = contacts;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
final ViewHolder mHolder;
if (convertView == null)
{
// gridView = new View(context);
// get layout from mobile.xml
//gridView = inflater.inflate(R.layout.contact, null);
convertView = inflater.inflate(R.layout.contact, null);
mHolder = new ViewHolder();
mHolder.textName =(TextView) convertView.findViewById(R.id.name);
mHolder.textMobile =(TextView) convertView.findViewById(R.id.mobile);
mHolder.textSelector =(CheckBox) convertView.findViewById(R.id.selected);
convertView.setTag(mHolder);
/* TextView textName = (TextView) gridView.findViewById(R.id.name);
textName.setSelected(true);
textName.setText(contacts.get(position).getName());*/
// cname = String.valueOf(contacts.get(position).getName());
/* TextView textMobile = (TextView) gridView.findViewById(R.id.mobile);
textMobile.setText(contacts.get(position).getMobile());*/
// cmob = String.valueOf(contacts.get(position).getMobile());
}
else
{
//gridView = convertView;
mHolder = (ViewHolder) convertView.getTag();
}
mHolder.textMobile.setText(contacts.get(position).getMobile());
mHolder.textName.setText(contacts.get(position).getName());
mHolder.textName.setSelected(true);
mHolder.textSelector.setChecked(sba.get(position));
mHolder.textSelector.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
if(mHolder.textSelector.isChecked())
{
sba.put(position, true);
}
else
{
sba.put(position, false);
}
}
});
// return gridView;
return convertView;
}
private class ViewHolder
{
private TextView textMobile,textName;
private CheckBox textSelector;
}
@Override
public int getCount()
{
return contacts.size();
}
@Override
public Object getItem(int position)
{
return null;
}
@Override
public long getItemId(int position)
{
return 0;
}
}
Aucun commentaire:
Enregistrer un commentaire