Listview in android with toast message

android, android-listview

Solution

As you can see from the logcat, You are getting class cast exception because you are trying to cast linear layout which is the root view (View v in this case) for your listitem to a TextView.

What you should be doing is as follows:

LV.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View v, int arg2,
                long arg3) {
            //here v is your ListItem's layout.
            TextView tv = (TextView) v.findViewById(R.id.textView1);
            Toast.makeText(getApplicationContext(), tv.getText().toString(), Toast.LENGTH_SHORT).show();
        }
    });

Problem

I am trying to display items in a listview And onclick of list row display a toast message I am having the error I am able to display the elements in listview but on click of list item i am getting the error MainActivity.java ``` public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String Names[]={"Mango","Banana","grapes"}; ListView LV=(ListView) findViewById(R.id.listView1); ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.list_item,R.id.textView1,Names); LV.setAdapter(adapter); LV.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), ((TextView) v).getText(), Toast.LENGTH_SHORT).show(); } }); } } ``` list_item.xml ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:padding="5dp" android:text="TextView" android:textStyle="italic|bold" /> </LinearLayout> ``` activity_main.xml ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/listView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" > </ListView> </LinearLayout> ``` LOG ``` 08-19 18:29:18.621: D/AndroidRuntime(692): Shutting down VM 08-19 18:29:18.621: W/dalvikvm(692): threadid=1: thread exiting with uncaught exception (group=0x40015560) 08-19 18:29:18.921: E/AndroidRuntime(692): FATAL EXCEPTION: main 08-19 18:29:18.921: E/AndroidRuntime(692): java.lang.ClassCastException: android.widget.LinearLayout 08-19 18:29:18.921: E/AndroidRuntime(692): at com.example.listviews.MainActivity$1.onItemClick(MainActivity.java:34) 08-19 18:29:18.921: E/AndroidRuntime(692): at android.widget.AdapterView.performItemClick(AdapterView.java:284) 08-19 18:29:18.921: E/AndroidRuntime(692): at android.widget.ListView.performItemClick(ListView.java:3513) 08-19 18:29:18.921: E/AndroidRuntime(692): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812) 08-19 18:29:18.921: E/AndroidRuntime(692): at android.os.Handler.handleCallback(Handler.java:587) 08-19 18:29:18.921: E/AndroidRuntime(692): at android.os.Handler.dispatchMessage(Handler.java:92) 08-19 18:29:18.921: E/AndroidRuntime(692): at android.os.Looper.loop(Looper.java:123) 08-19 18:29:18.921: E/AndroidRuntime(692): at android.app.ActivityThread.main(ActivityThread.java:3683) 08-19 18:29:18.921: E/AndroidRuntime(692): at java.lang.reflect.Method.invokeNative(Native Method) 08-19 18:29:18.921: E/AndroidRuntime(692): at java.lang.reflect.Method.invoke(Method.java:507) 08-19 18:29:18.921: E/AndroidRuntime(692): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 08-19 18:29:18.921: E/AndroidRuntime(692): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 08-19 18:29:18.921: E/AndroidRuntime(692): at dalvik.system.NativeStart.main(Native Method) 08-19 18:29:23.481: I/Process(692): Sending signal. PID: 692 SIG: 9 08-19 18:35:01.833: D/AndroidRuntime(726): Shutting down VM 08-19 18:35:01.833: W/dalvikvm(726): threadid=1: thread exiting with uncaught exception (group=0x40015560) 08-19 18:35:01.851: E/AndroidRuntime(726): FATAL EXCEPTION: main 08-19 18:35:01.851: E/AndroidRuntime(726): java.lang.ClassCastException: android.widget.LinearLayout 08-19 18:35:01.851: E/AndroidRuntime(726): at com.example.listviews.MainActivity$1.onItemClick(MainActivity.java:34) 08-19 18:35:01.851: E/AndroidRuntime(726): at android.widget.AdapterView.performItemClick(AdapterView.java:284) 08-19 18:35:01.851: E/AndroidRuntime(726): at android.widget.ListView.performItemClick(ListView.java:3513) 08-19 18:35:01.851: E/AndroidRuntime(726): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812) 08-19 18:35:01.851: E/AndroidRuntime(726): at android.os.Handler.handleCallback(Handler.java:587) 08-19 18:35:01.851: E/AndroidRuntime(726): at android.os.Handler.dispatchMessage(Handler.java:92) 08-19 18:35:01.851: E/AndroidRuntime(726): at android.os.Looper.loop(Looper.java:123) 08-19 18:35:01.851: E/AndroidRuntime(726): at android.app.ActivityThread.main(ActivityThread.java:3683) 08-19 18:35:01.851: E/AndroidRuntime(726): at java.lang.reflect.Method.invokeNative(Native Method) 08-19 18:35:01.851: E/AndroidRuntime(726): at java.lang.reflect.Method.invoke(Method.java:507) 08-19 18:35:01.851: E/AndroidRuntime(726): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 08-19 18:35:01.851: E/AndroidRuntime(726): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 08-19 18:35:01.851: E/AndroidRuntime(726): at dalvik.system.NativeStart.main(Native Method) ``` Any ideas on overcoming this

Original source