saeed khan saeed khan - 4 months ago 17
Android Question

listview OnItemClick listner not working

I got the error. error and code is below anyone can solve these error.

ListView listView = (ListView) findViewById(R.id.ChatlistView);
ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(RoomListActivity.this,
R.layout.room_row,
R.id.room,
items);
listView.setAdapter(mArrayAdapter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

TextView textView = (TextView) view;
String message = "You clicked # " + position + ", which is string: " + textView.getText().toString();
Toast.makeText(RoomListActivity.this, message, Toast.LENGTH_LONG).show();
}
});


room_row.xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativelay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
android:id="@+id/room"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/img_room"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/img_room"
android:layout_toRightOf="@+id/img_room"
android:layout_toStartOf="@+id/img_room"
android:gravity="left|center_vertical"
android:textSize="16dp" />



<ImageView
android:id="@+id/img_room"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:src="@drawable/chat" />

</RelativeLayout>


LogCat:

03-08 12:53:50.775: E/AndroidRuntime(716): FATAL EXCEPTION: main
03-08 12:53:50.775: E/AndroidRuntime(716): java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
03-08 12:53:50.775: E/AndroidRuntime(716): at com.android.wap2mob.RoomListActivity$1$1.onItemClick(RoomListActivity.java:126)
03-08 12:53:50.775: E/AndroidRuntime(716): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
03-08 12:53:50.775: E/AndroidRuntime(716): at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
03-08 12:53:50.775: E/AndroidRuntime(716): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
03-08 12:53:50.775: E/AndroidRuntime(716): at android.widget.AbsListView$1.run(AbsListView.java:3168)
03-08 12:53:50.775: E/AndroidRuntime(716): at android.os.Handler.handleCallback(Handler.java:605)
03-08 12:53:50.775: E/AndroidRuntime(716): at android.os.Handler.dispatchMessage(Handler.java:92)
03-08 12:53:50.775: E/AndroidRuntime(716): at android.os.Looper.loop(Looper.java:137)
03-08 12:53:50.775: E/AndroidRuntime(716): at android.app.ActivityThread.main(ActivityThread.java:4424)
03-08 12:53:50.775: E/AndroidRuntime(716): at java.lang.reflect.Method.invokeNative(Native Method)
03-08 12:53:50.775: E/AndroidRuntime(716): at java.lang.reflect.Method.invoke(Method.java:511)
03-08 12:53:50.775: E/AndroidRuntime(716): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-08 12:53:50.775: E/AndroidRuntime(716): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-08 12:53:50.775: E/AndroidRuntime(716): at dalvik.system.NativeStart.main(Native Method)

Answer

03-08 12:53:50.775: E/AndroidRuntime(716): FATAL EXCEPTION: main 03-08 12:53:50.775: E/AndroidRuntime(716): java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView 03-08 12:53:50.775: E/AndroidRuntime(716):

A ClassCastException is an Exception that can occur in a Java program when you try to improperly convert a class from one type to another.

Problem in here

  TextView textView = (TextView) view;

Try this way

 TextView textView=(TextView) view.findViewById(R.id.room); // You are missing findViewById(R.id.room);
Comments