Reputation: 23344
I have a custom layout for the ListView
which is as follows -
<?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"
android:background="@drawable/plain" >
<TextView
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="6dip"
android:textSize="16sp"
android:textColor="#000000"
android:paddingRight="10dip"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
When I click the item in a listview, I am getting error -
02-28 17:37:38.240: E/AndroidRuntime(20869): FATAL EXCEPTION: main 02-28 17:37:38.240: E/AndroidRuntime(20869): java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
This is because now I am using custom listview layout as above which is wrapped in a linearlayout.
The error line is-
class ListItemClickedonPaired implements OnItemClickListener
{
@SuppressLint("NewApi")
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
final String info = ((TextView) view).getText().toString(); //ERROR HERE
}
}
My question is-
How to get the text of the clicked item (technically a textview) when we have these as a custom items of a listview defined with above xml ?
Upvotes: 0
Views: 1103
Reputation: 7214
Instead of writing a text view inside a Linear layout make the TextView as the root Tag so that the view argument in the OnItemClick() returns the reference of text view, Now you can directly typecast the view object to the TextView.
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="6dip"
android:textSize="16sp"
android:textColor="#000000"
android:paddingRight="10dip"
android:textAppearance="?android:attr/textAppearanceLarge" />
Upvotes: 1
Reputation: 10622
Try this :
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
switch(view.getId()) {
case R.id.text1:
final String info = ((TextView) view).getText().toString();
break;
}
}
Upvotes: 0
Reputation: 133560
Try
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
TextView tv = (TextView)view.findViewById(android.R.id.text1);
String info = tv.getText().toString();
}
You inflate a LinearLayout
for each row. What you are doing is casting LinearLayout
to TextView
.
You can also use setTag
and getTag
You have used a id from the android framework. So it should be android.R.id.text1
not R.id.text1
coz you have android:id="@android:id/text1"
. It is a mistake which i have edited.
http://developer.android.com/reference/android/R.id.html
public static final int text1
Added in API level 1
Constant Value: 16908308 (0x01020014
Upvotes: 4