There is a custom ListView. Here is the markup

<LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp"> <TextView android:text="A" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/txtAlphabet" android:background="@drawable/gray_button" android:gravity="center" /> <TextView android:text="TextView" android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/txtWord" android:layout_weight="1" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:gravity="left|center" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@drawable/icon_star_outline_black" android:id="@+id/btnFavorites" android:background="@drawable/white_button" /> </LinearLayout> 

This list displays the words from the database. How to make it so that when you click on a list item, something is done. And if you click on the button in the item from the list, something else was done. I previously had this code for a regular ListView, but it does not work after I customized the list

 list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { //что-то } }); 

Here is such an ejection

 E/AndroidRuntime: FATAL EXCEPTION: main Process: test.proj1, PID: 7062 java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView at test.proj1.MainActivity$3.onItemClick(MainActivity.java:231) at android.widget.AdapterView.performItemClick(AdapterView.java:310) at android.widget.AbsListView.performItemClick(AbsListView.java:1155) at android.widget.AbsListView$PerformClick.run(AbsListView.java:3120) at android.widget.AbsListView.onTouchUp(AbsListView.java:4047) at android.widget.AbsListView.onTouchEvent(AbsListView.java:3806) at android.view.View.dispatchTouchEvent(View.java:9943) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2663) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2344) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358) at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:411) at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1810) at android.app.Activity.dispatchTouchEvent(Activity.java:3061) at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:71) at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:373) at android.view.View.dispatchPointerEvent(View.java:10163) at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4434) at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4302) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3995) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3876) at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4052) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3876) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6210) at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6184) at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6145) at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6313) at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(MessageQueue.java:323) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

On the string CharSequence strCharSequence = ((TextView) view).getText(); swears from the method

 list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Intent intent = new Intent(MainActivity.this, Word.class); CharSequence strCharSequence = ((TextView) view).getText(); String str = strCharSequence.toString().toLowerCase().trim();; String selectedItem = spinner.getSelectedItem().toString(); } }); 

    1 answer 1

    The error is logical: view is the root element, it is LinearLayout , as can be seen from the markup description. To access the TextView inside it, you need to use findViewById() :

     (TextView)view.findViewById(R.id.txtWord) 
    • Understood, and how to implement click on clicking the btnFQ button? separate method to do? - Kolkhoznik
    • as always - hang a lisner on it. but, since this is a ListView, the lisner will have to be hung up in the adapter in onCreateView and then the task will be solved, how this lisner will receive information, to which element it is associated. I would create this lisner in holder, but that's another story. - tse
    • And you can link to some example? - Kolkhoznik
    • And right in the MainActivity you can not somehow implement it? Just by clicking on a button from the ListView, I have to record something in the database - Kolkhoznik