Click ListView Item after filter the list

masbayu :

I do some project for my study, I want to create listview search using edittext, and it is work perfectly. The problem is when the listview filtered and i try to click the result it goes to wrong action.

Then i realize that it is caused I used listview position on click action, so I tried to change to be list view values, but it is quite difficult to me since I always facing error on my code below.

@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
    TextView textView = (TextView) view;
    String parameterNilai = String.valueOf(textView.getText());

    if (parameterNilai.equals("One")) {
        try {
            Intent intent = new Intent(MurotalActivity.this, com.ummi.myapplication.speaker.MurotalActivityDetail.class);
            intent.putExtra("TAG_SID", "229");
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            closeKeyboard();
            finish();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    } else if (parameterNilai.equals("Two")) {
        try {
            Intent intent = new Intent(MurotalActivity.this, com.ummi.myapplication.speaker.MurotalActivityDetail.class);
            intent.putExtra("TAG_SID", "230");
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            closeKeyboard();
            finish();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    } else if (parameterNilai.equals("Three")) {
        try {
            Intent intent = new Intent(MurotalActivity.this, com.ummi.myapplication.speaker.MurotalActivityDetail.class);
            intent.putExtra("TAG_SID", "231");
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            closeKeyboard();
            finish();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }

and for the list Array as below :

<string-array name="listsurah">
    <item>One</item>
    <item>Two</item>
    <item>Three</item>
</string-array>

My Layout XML :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MurotalActivity"
xmlns:tools="http://schemas.android.com/tools">
<RelativeLayout
    android:id="@+id/judul1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="#A6E0BA"
    android:gravity="center_vertical">
    <EditText android:id="@+id/inputSearch"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Cari Surat.."
        android:inputType="textVisiblePassword"/>
</RelativeLayout>
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/judul1"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ListView
                android:id="@+id/listview"
                android:layout_width="match_parent"
                android:layout_height="9600dp"
                android:layout_marginBottom="20dp"
                android:choiceMode="singleChoice"
                android:listSelector="#F0BCBC">
            </ListView>
        </RelativeLayout>
    </ScrollView>

The error i got from my logcat :

2020-03-19 06:14:37.593 30929-30929/com.mby.mbaca E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mby.mbaca, PID: 30929
java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
    at com.ummi.myapplication.MurotalActivity.onItemClick(MurotalActivity.java:109)
    at android.widget.AdapterView.performItemClick(AdapterView.java:318)
    at android.widget.AbsListView.performItemClick(AbsListView.java:1159)
    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3136)
    at android.widget.AbsListView$3.run(AbsListView.java:4052)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

based on above logcat the problem is on the line

TextView textView = (TextView) view;

please need anyone help, how i can click ListItem based on the ListItem Text value instead of position

Kiryl Tkach :

Your item element is LinearLayout, not TextView. Properly you should get your collection element by position (for this you have position variable) and from it get text. Something like

String text = adapter.getItem(position)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=321523&siteId=1