V S V S - 4 months ago 22
Android Question

setOnClickListener for ToggleButton/CheckBox in not working in ListView?

I want a check box or toggle button in listView, and set onclicklisterner but it works only for zero position, here is my code

String[] NewTransferItems = { "Spendability", "Accounts", "Budgets",
"I'm keeping" };


and

dashboardEditListView = (ListView) findViewById(R.id.dashboardEditListView);
listAdapter = new DashboardEditListAdapater();
dashboardEditListView.setAdapter(listAdapter);
dashboardEditListView.s


etOnItemClickListener(listClickListner);

My adapter:

class DashboardEditListAdapater extends ArrayAdapter<String> implements OnClickListener{
DashboardEditListAdapater() {
super(DashboardEdit.this, R.layout.generic_list_row,
NewTransferItems);
}

public View getView(int position, View convertView, ViewGroup parent) {

System.out.println("Called Method " + getItem(position)
+ " Position " + position);

View row = null;
EditText editText = null;
CheckBox checkBox = null;

if (position == 0 || position == 1 || position == 2) {
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.generic_list_row_withtoggle,
parent, false);
checkBox = (CheckBox) findViewById(R.id.checkBox);

// init
} else if (position == 3) {
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.generic_list_row_with_edittext,
parent, false);

editText = (EditText) findViewById(R.id.rowRightEditText);

if(editText != null) {
editText.setVisibility(View.VISIBLE);
}
}

LinearLayout rowWrapper = (LinearLayout) row
.findViewById(R.id.rowWrapper);
LinearLayout rowHeadWrapper = (LinearLayout) row
.findViewById(R.id.rowHeadWrapper);
rowHeadWrapper.setVisibility(ViewGroup.GONE);

String item = getItem(position);

if (position == 0) {
if(checkBox != null){
checkBox.setTag(position);
checkBox.setOnClickListener(this);
}

} else if (position == 1) {
if(checkBox != null){
checkBox.setTag(position);
checkBox.setOnClickListener(this);
}

} else if (position == 2) {
if(checkBox != null){
checkBox.setTag(position);
checkBox.setOnClickListener(this);
}
}

TextView rowTitle = (TextView) row.findViewById(R.id.rowTitle);
rowTitle.setText(item);
return row;
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

System.out.println("Clicked");

}

}


Here is my XML for generic_list_row_withtoggle

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/com.mytalk.androidapp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<LinearLayout
android:id="@+id/rowWrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"
android:background="#FFF"
android:orientation="vertical"
android:padding="0dip" >

<LinearLayout
android:id="@+id/rowHeadWrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/cell_header_bg"
android:orientation="vertical"
android:paddingLeft="8dip" >

<TextView
android:id="@+id/rowHead"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Profile"
android:textColor="#000"
android:textSize="18dip"
android:textStyle="bold"
android:visibility="gone" />
</LinearLayout>

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:paddingLeft="8dip" >

<!--
<ImageView
android:id="@+id/rowImage"
android:layout_width="60dp"
android:layout_height="36dp"
android:layout_centerVertical="true"
android:src="@drawable/bahrain_flag"
android:visibility="gone" />
-->

<TextView
android:id="@+id/rowTitle"
android:layout_width="200dip"
android:layout_height="45dip"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dip"
android:layout_toRightOf="@id/rowImage"
android:gravity="center_vertical"
android:text="Current"
android:textColor="#000"
android:textSize="15dip"
android:textStyle="bold" />

<CheckBox
android:id="@+id/checkBox"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dip"
android:layout_marginRight="10dip"
/>
<!--
<ToggleButton
android:id="@+id/togglebutton2"
android:layout_width="60dip"
android:layout_height="40dip"
android:layout_alignParentRight="false"
android:layout_marginLeft="20dip"
android:layout_marginRight="10dip"
android:textOff="OFF"
android:textOn="ON"
android:visibility="visible" />
-->
</RelativeLayout>
</LinearLayout>

</LinearLayout>


I used ToggleButton and CheckBox both, but there is not difference, it works only for zero position, otherwise not calling onClickListner.

I am unable to understand the reason, why its happening.

let me know if anything is not yet clear

Answer

First of all make change here,

 row = inflater.inflate(R.layout.generic_list_row_withtoggle,parent,false);                 
checkBox = (CheckBox)row.findViewById(R.id.checkBox);