0
votes

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.

5
Are you sure that rLayout.getChildAt(1); gets you the ToogleButton? It probably is rLayout.getChildAt(2);... - miselking

5 Answers

0
votes

it is clearly stating that you are trying to cast textview in toggle button so please check first the toggle position in your layout . There is textview on index 1 here rLayout.getChildAt(1); .

0
votes

Instead of using static index to get ToggleButton from selected row layout use instanceof to check is View is ToggleButton or TextView:

RelativeLayout rLayout = (RelativeLayout) arg1;
for (int index = 0; index < rLayout.getChildCount(); index++) {
     View view=rLayout.getChildAt(index);
     if(view instanceof ToggleButton){      
        ToggleButton tgl = (ToggleButton)view;       
         //... do task here...
        break;
     }
}
0
votes

The Error says "TextView cannot be cast to android.widget.ToggleButton", so, the method " rLayout.getChildAt(1)" clearly not working for you. you could set id in XML, then find it from your "rLayout".

0
votes

Change following line

ToggleButton tgl = (ToggleButton) rLayout.getChildAt(1);

to

ToggleButton tgl = (ToggleButton) rLayout.getChildAt(2);

Note : If not works once clean your project and try again.

Hope this will helps you.

0
votes

You are casting a TextView into a ToggleButton. The problem is in this line:

ToggleButton tgl = (ToggleButton) rLayout.getChildAt(1);

For some reason the child at 1 is a TextView and not the expected ToggleButton.

I encourage you to use findViewById instead of getChildAt, so you'll find them by id and not position in layout. It will avoid these kind of bugs related to order of widgets.