I have an array of checkboxes in a listview, one checkbox per row. When any of these checkboxes is checked I want to display a toast message.
This checkbox function is called addListenerToCheckBox();, it's in my onCreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_contact);
etc....
addListenerToCheckBox();
}
As stated the checkbox is in a row in my listview so I inflate that xml, phone_inflate_listview, separately from my main xml, which is activity_new_contact. Here is phone_inflate_listview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://ift.tt/GEGVYd"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="6"
android:layout_margin="5dp"
android:paddingLeft="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:id="@+id/name"
android:layout_marginTop="10dp"
android:text="User name"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_margin="5dp"
android:paddingLeft="5dp"
>
<CheckBox
android:id="@+id/checkBoxContact"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:checked="false"
android:clickable="true"
/>
</LinearLayout>
</LinearLayout>
And in a function outside my onCreate I have addListenerToCheckBox():
public void addListenerToCheckBox() {
//for the checkbox
LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.phone_inflate_listview, null);
final CheckBox checkBox2 = (CheckBox)view.findViewById(R.id.checkBoxContact);
checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{ @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// perform logic
Toast.makeText(NewContact.this, "isChecked - " + checkBox2.isChecked(), Toast.LENGTH_SHORT).show();
}
}
});}
My activity loads ok, but I'm not getting any toast message when checkbox is clicked. Can you tell me why not please? I have spent a good few hours trying to fix this problem.
I also tried stuff like:
public void addListenerToCheckBox() {
//for the checkbox
View view = null;
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.phone_inflate_listview, null);
checkBox2 = (CheckBox)findViewById(R.id.checkBoxContact);
checkBox2.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
SelectPhoneContact data = (SelectPhoneContact) cb.getTag();
//loop through the Matching Contacts
int count = MatchingContactsAsArrayList.size();
for (int i = 0; i < count; i++) {
//for each Matching Contacts row in the listview
LinearLayout itemLayout = (LinearLayout) listView.getChildAt(i); // Find by under LinearLayout
//for each Matching Contacts checkbox in the listview
CheckBox checkbox = (CheckBox) itemLayout.findViewById(R.id.checkBoxContact);
//get the other data related to the selected contact - name and number
SelectPhoneContact data = (SelectPhoneContact) checkbox.getTag();
Toast.makeText(NewContact.this,
"Clicked on Checkbox: " + data.getPhone() +
" is " + cb.isChecked(),
Toast.LENGTH_LONG).show();
data.setSelected(cb.isChecked());
}
}
});
But no joy :(
Aucun commentaire:
Enregistrer un commentaire