简体   繁体   English

如何在没有错误的情况下实现 SearchView

[英]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.我在 Android Studio 中遇到了 ListView 的问题。 I have created a simple activity with a simple Listview.我用一个简单的 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. Listview 包含位置,当用户单击某个项目时,应用程序将打开谷歌地图并将用户带到该位置。 The problem occurred when I implemented a SearchView.当我实现 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: .这就是你编码它的方式,你有if(position==0)所以无论第一个位置是什么,你都会打开相同的geo: you should check WHAT is on first position when clicked, so inside onItemClick put:你应该检查点击时第一个位置是什么,所以在onItemClick里面放:

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现在将positionInArray用于您的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但这是一个快速修复,你应该有一些模型,你的自定义类有两个变量, String nameString geoUri或两个long s 用于 lat 和 lng

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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