简体   繁体   中英

How do I implement SearchView with no errors

I am a beginner and new to coding. I have a problem with ListView in Android Studio. I have created a simple activity with a simple Listview. The Listview contains locations, and when the user clicks on an item the app will open google maps and takes the user to that location. The problem occurred when I implemented a SearchView. When search is applied, whatever result is filtered it will always open the first location. So could you please help me with that. Thanks. This is my code and sorry for the mess.

MainActivity.java

import com.example.myapplicationsecond.R;

public class MainActivity9 extends AppCompatActivity {

    ListView listView;
    String[] name = {"First Location","Second Location","Third Location","Fourth Location",};

    ArrayAdapter<String> arrayAdapter;

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

        View view = getLayoutInflater().inflate(R.layout.abs_layout, null);
        ActionBar.LayoutParams params = new ActionBar.LayoutParams(
                ActionBar.LayoutParams.WRAP_CONTENT,
                ActionBar.LayoutParams.MATCH_PARENT,
                Gravity.CENTER);

        TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
        Title.setText("Search Here");

        getSupportActionBar().setCustomView(view,params);
        getSupportActionBar().setDisplayShowCustomEnabled(true); //show custom title
        getSupportActionBar().setDisplayShowTitleEnabled(false); //hide the default title


        getSupportActionBar().setTitle("Search Here");


        listView = findViewById(R.id.listview);

        arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,name);
        listView.setAdapter(arrayAdapter);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position==0){

                    Intent intent = new Intent(Intent.ACTION_VIEW , Uri.parse("geo: 21.422458, 39.826213"));
                    startActivity(intent);

                }
                if(position==1){

                    Intent intent = new Intent(Intent.ACTION_VIEW , Uri.parse("geo: 24.467275, 39.610629"));
                    startActivity(intent);

                }
                if(position==2){

                    Intent intent = new Intent(Intent.ACTION_VIEW , Uri.parse("geo: 25.173059, 45.142079"));
                    startActivity(intent);

                }
                if(position==3){

                    Intent intent = new Intent(Intent.ACTION_VIEW , Uri.parse("geo: 26.348400, 43.766664"));
                    startActivity(intent);


            }

            }

        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {


        getMenuInflater().inflate(R.menu.menu,menu);


        MenuItem menuItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) menuItem.getActionView();
        searchView.setQueryHint("Search Here");

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {


                arrayAdapter.getFilter().filter(newText);



                return false;
            }
        });

        return super.onCreateOptionsMenu(menu);
    }




}


MainActivity.Xml

<?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=".MainActivity9">


    <ListView
        android:id="@+id/listview"
        android:textDirection="locale"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

thats how you coded it, you have if(position==0) so no matter what will be on first position you will open same geo: . you should check WHAT is on first position when clicked, so inside onItemClick put:

String clickedText = arrayAdapter.getItem(position);

then find position of this item in all-items array

int positionInArray = java.util.Arrays.asList(name).indexOf(clickedText);

and now use positionInArray for your is else

but thats a quick fix, you should have some model, your custom class with two variables, String name and String geoUri or two long s for lat and lng

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM