I'm trying to type cast data but when I write TextView clickData=(TextView) view under onItemClick(...) logcat shows msg:
java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
I don't know why.
This belongs from main activity:
public class ListViewForAllContact extends AppCompatActivity {
ListView myListView;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.listviewforallcontact);
ContactDatabase onbOfContactDatabase=new ContactDatabase(getBaseContext());
Cursor mCursor= onbOfContactDatabase.phoneName();
String[] fromFileName=new String[]{ContactDatabase.NAME};
int[] toViewId=new int[]{R.id.textView5};
SimpleCursorAdapter simpleCursorAdapter;
simpleCursorAdapter=new SimpleCursorAdapter(this,R.layout.forreading,mCursor,fromFileName,toViewId,0);
myListView=(ListView)findViewById(R.id.listView);
myListView.setAdapter(simpleCursorAdapter);
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView clickData=(TextView) view;//becouse of this line error will come.
Toast.makeText(getBaseContext(), clickData.getText(), Toast.LENGTH_LONG).show();
}
});
}
}
This is xml with ListView:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
This is second XML file with TextView:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView5"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>