2

I got runtime error exception:

java.lang.ClassCastException: android.widget.TwoLineListItem cannot be cast to android.widget.TextView

My Activity extends Activity NOT ListActivity and here is my layout construction:

<LinearLayout ...> <ListView ...></ListView> </LinearLayout>

Java:

   ListView lv1 = (ListView) findViewById(R.id.listViewXMLdata);
    ArrayAdapter<String> arrAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2,
        android.R.id.text2, getResources().getStringArray(R.array.countries));

    lv1.setAdapter(arrAdapter);

    lv1.setOnItemClickListener(new OnItemClickListener() {          
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
            lView.getItemAtPosition(position);

            String itemSelected = ((TextView) view).getText().toString();
            Toast.makeText(getApplicationContext(), "Clicked Position Number " + position + ": " + itemSelected ,
                    Toast.LENGTH_SHORT)
                    .show();


        }
    });

My only main concern is just to get the string (item selected) on my list.

NOTE 1: I am not working on any database.

NOTE 2: I already tried casting it to CharSequence itemSelected = ((TextView) view).getText().toString() but still got runtime error.

The runtime error occurs only when I started to select item on the list.

3 Answers 3

9

The android.R.layout.simple_list_item_2 has as its root a TwoLineListItem widget with two children(two TextViews), the two rows of text. Casting this widget to a TextView will fail with the exception you see in the logcat. Search for the desired TextView with findViewById:

String itemSelected = ((TextView) view.findViewById(android.R.id.text2)).getText().toString();
Sign up to request clarification or add additional context in comments.

Comments

1

Your ListView is being loaded with TwoLineListItem types and you are tring to cast the View into a TextView in the following line:

String itemSelected = ((TextView) view).getText().toString();

Changing this to something like:

String itemSelected = ((TwoLineListItem) view).getText1().getText();

should work.

Edit:

The Android documentation at: http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html says:

public abstract void onItemClick (AdapterView parent, View view, int position, long id)

Since: API Level 1 Callback method to be invoked when an item in this AdapterView has been clicked.

Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.

Parameters

parent The AdapterView where the click happened.

view The view within the AdapterView that was clicked (this will be a view provided by the adapter)

position The position of the view in the adapter.

id The row id of the item that was clicked.

So, the view passed as parameter to the method is actually a TwoLineListItem, the type you should cast the view to.

Once you get the reference to TwoLineListItem, you can access any of its public members including both TextViews with:

public TextView getText1 ()

Since: API Level 1 Returns a handle to the item with ID text1.

Returns A handle to the item with ID text1.

public TextView getText2 ()

Since: API Level 1 Returns a handle to the item with ID text2.

Returns A handle to the item with ID text2.

1 Comment

THanks..your solution helps a lot but this will only get the position (index) of the selected item.
0

In addition, you can look into android.R.layout.simple_list_item_2, then you will find the layout xml as follow :

    <TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/listPreferredItemHeight"
        android:mode="twoLine"
        android:paddingStart="?attr/listPreferredItemPaddingStart"
        android:paddingEnd="?attr/listPreferredItemPaddingEnd">

        <TextView android:id="@id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:textAppearance="?attr/textAppearanceListItem" />

        <TextView android:id="@id/text2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/text1"
        android:layout_alignStart="@id/text1"
        android:textAppearance="?attr/textAppearanceListItemSecondary" />

    </TwoLineListItem>

The TwoLineListItem has two TextViews. Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.