skip to Main Content

I have developed a basic program where it has list(recycler view) and you can search from it. But, the issue i encountered is when i tried to integrate a search feature. When trying to find the id of the search view, my app crashes. I also tried to use EditText, but it encountered same problem. The line searchViewHx = findViewById(R.id.searchHx) is the one that error

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SearchView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    //RecyclerAdapter recyclerAdapter;
    ArrayList<String> soilList = new ArrayList<>();
    ArrayList<String> remarkList = new ArrayList<>();
    ImageView deleteAll;
    AlertDialog.Builder deleteAllBuilder;
    private SearchView searchViewHx;
    //EditText searchEdit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        deleteAll = findViewById(R.id.deleteAll);
        searchViewHx = findViewById(R.id.searchHx); //THE ONE THAT CAUSES ERROR
        //searchViewHx.clearFocus();
        /*searchViewHx.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
            @Override
            public boolean onQueryTextSubmit(String query){
                return false;
            }
            @Override
            public boolean onQueryTextChange(String newText){
                filterList(newText);
                return true;
            }
        });*/

        soilList.add("Soil 1");
        soilList.add("Soil 2");
        soilList.add("Soil 3");
        soilList.add("Soil 4");
        soilList.add("Soil 5");

        remarkList.add("Passed");
        remarkList.add("Failed");
        remarkList.add("Passed");
        remarkList.add("Failed");
        remarkList.add("Failed");
        initRecyclerView();
        deleteAllBuilder = new AlertDialog.Builder(this); //CHANGE THIS
        deleteAll.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                deleteAllBuilder.setTitle("Alert")
                        .setMessage("Do you want to delete all?")
                        .setCancelable(true)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //method that deletes all hx from list and cardview
                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .show();
            }
        });
    }

    /*private void filterList(String text) {
        List<String> filteredList = new ArrayList<>();
        filteredList.addAll(soilList);
        for(String item : filteredList){
            if(item.toLowerCase().contains(text.toLowerCase())){
                filteredList.add(item);
            }
            if(filteredList.isEmpty()){
                Toast.makeText(this, "No data found", Toast.LENGTH_SHORT).show();
            }else{
                recyclerAdapter.setFilteredList(filteredList);
            }
        }
    }*/

    private void initRecyclerView(){
        RecyclerAdapter recyclerAdapter = new RecyclerAdapter(soilList, remarkList);
        recyclerView.setAdapter(recyclerAdapter);
    }
    
}

XML FILE:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout2"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        

        <ImageView
            android:id="@+id/deleteAll"
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:src="@drawable/delete_all"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.appcompat.widget.SearchView
            android:id="@+id/searchHx"
            android:layout_width="293dp"
            android:layout_height="match_parent"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:background="@drawable/search_bg"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:queryHint="Search"
            app:iconifiedByDefault="false"/>
            <!--android:visibility="gone"/>-->

        <EditText
            android:id="@+id/searchEdit"
            android:layout_width="293dp"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:visibility="gone"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="60dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/constraintLayout2"
        app:layout_constraintVertical_bias="0.0">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

I made sure, the search view is on the correct xml layout file and made sure Im using the right id of the search view. I imported the needed class and tried using edit text, to see if it has same problem.

2

Answers


  1. What kind of error you’re getting?

    Most likely like this:

    androidx.appcompat.widget.SearchView cannot be cast to android.widget.SearchView

    If so, it’s because you used androidx.appcompat.widget.SearchView in your XML layout but imported android.widget.SearchView. Change the import in your class and it should work.

    Login or Signup to reply.
  2. <SearchView
    android:id="@+id/searchHx"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:queryHint="Search..."/>
    
    import androidx.appcompat.widget.SearchView;
    Rebuild project and check dependencies too, All the best!
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search