I have created the listview with two textview as a name and phone no. and infront of this one toggle button. i should get name when i click on the toggle button. but it is giving error as
'02-17 06:49:53.170: E/AndroidRuntime(1425): java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.ToggleButton 02-17 06:49:53.170: E/AndroidRuntime(1425): at com.example.comparearray.MainActivity$1.onItemClick(MainActivity.java:85) 02-17 06:49:53.170: E/AndroidRuntime(1425): at android.widget.AdapterView.performItemClick(AdapterView.java:299) 02-17 06:49:53.170: E/AndroidRuntime(1425): at android.widget.AbsListView.performItemClick(AbsListView.java:1113) 02-17 06:49:53.170: E/AndroidRuntime(1425): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904) 02-17 06:49:53.170: E/AndroidRuntime(1425): at android.widget.AbsListView$3.run(AbsListView.java:3638) 02-17 06:49:53.170: E/AndroidRuntime(1425): at android.os.Handler.handleCallback(Handler.java:733) 02-17 06:49:53.170: E/AndroidRuntime(1425): at android.os.Handler.dispatchMessage(Handler.java:95) 02-17 06:49:53.170: E/AndroidRuntime(1425): at android.os.Looper.loop(Looper.java:136) 02-17 06:49:53.170: E/AndroidRuntime(1425): at android.app.ActivityThread.main(ActivityThread.java:5017) 02-17 06:49:53.170: E/AndroidRuntime(1425): at java.lang.reflect.Method.invokeNative(Native Method) 02-17 06:49:53.170: E/AndroidRuntime(1425): at java.lang.reflect.Method.invoke(Method.java:515) 02-17 06:49:53.170: E/AndroidRuntime(1425): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 02-17 06:49:53.170: E/AndroidRuntime(1425): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 02-17 06:49:53.170: E/AndroidRuntime(1425): at dalvik.system.NativeStart.main(Native Method)
the below is my code
lvCountries = (ListView) findViewById(R.id.lv_countries);
OnItemClickListener itemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> lv, View item, int position, long id) {
ListView lView = (ListView) lv;
SimpleAdapter adapter = (SimpleAdapter) lView.getAdapter();
HashMap<String,Object> hm = (HashMap) adapter.getItem(position);
/** The clicked Item in the ListView */
RelativeLayout rLayout = (RelativeLayout) item;
/** Getting the toggle button corresponding to the clicked item */
ToggleButton tgl = (ToggleButton) rLayout.getChildAt(1);
String strStatus = "";
if(tgl.isChecked()){
tgl.setChecked(false);
strStatus = "Off";
status[position]=false;
}else{
tgl.setChecked(true);
strStatus = "On";
status[position]=true;
}
Toast.makeText(getBaseContext(), (String) hm.get("txt") + " : " + strStatus, Toast.LENGTH_SHORT).show();
}
};
lvCountries.setOnItemClickListener(itemClickListener);
please help me,i am stuck here.
rLayout.getChildAt(1);gets you theToogleButton? It probably isrLayout.getChildAt(2);... - miselking