SearchView 转为 EditText 搜索

huangapple go评论75阅读模式
英文:

SearchView to Edittext search

问题

我是Android初学者,我有一个搜索MySQL数据库并使用JSON显示结果的代码。我的问题是,我想将SearchView更改为EditText(id)和Button(searchbtn)。当点击搜索按钮时,它应该给我结果。我该如何更改它?以下是您的代码部分:

mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }
    
    @Override
    public boolean onQueryTextChange(String query) {
        // 在这里添加处理EditText和Button的代码
        // 当点击按钮时,执行搜索操作并显示结果
        // 你需要使用query作为搜索关键字
        // 之后,你可以调用searchSpacecraft方法来执行搜索
        // 最后,将结果传递给populateListView方法来显示结果
        return false;
    }
});

请注意,你需要在onQueryTextChange方法中添加处理EditText和Button的代码,以便在点击按钮时执行搜索操作并显示结果。你可以使用query作为搜索关键字,调用searchSpacecraft方法来执行搜索,然后将结果传递给populateListView方法来显示结果。

英文:

i'm android beginner i have code that search MySQL database using json and display results my problen now i want to change SearchView to EditText(id) and Button (searchbtn) when clicks the searchbutton it gives me results how can i change this to do it

 mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }
        @Override
        public boolean onQueryTextChange(String query) {
            final Call<List<Spacecraft>> call = myAPIService.searchSpacecraft(query);
            call.enqueue(new Callback<List<Spacecraft>>() {

                @Override
                public void onResponse(Call<List<Spacecraft>> call, Response<List<Spacecraft>> response) {
                    mProgressBar.setVisibility(View.GONE);
                    populateListView(response.body());
                }
                @Override
                public void onFailure(Call<List<Spacecraft>> call, Throwable throwable) {
					populateListView(new ArrayList<Spacecraft>());
                    mProgressBar.setVisibility(View.GONE);
                    Toast.makeText(MainActivity.this, "ERROR: "+throwable.getMessage(), Toast.LENGTH_LONG).show();
                }
            });
            return false;
        }
    });

答案1

得分: 0

只需通过yourEditText.getText()从EditText中获取文本,然后将该值传递给您的请求...所有这些都在onButtonClickListener中进行。

以下是代码部分:

searchBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // 从编辑框中获取值
        String result = yourEdittext.getText().toString();
        
        // 然后将值传递给您的请求,如下所示
        final Call<List<Spacecraft>> call = myAPIService.searchSpacecraft(result);
        call.enqueue(new Callback<List<Spacecraft>>() {
            @Override
            public void onResponse(Call<List<Spacecraft>> call, Response<List<Spacecraft>> response) {
                mProgressBar.setVisibility(View.GONE);
                populateListView(response.body());
            }
            
            @Override
            public void onFailure(Call<List<Spacecraft>> call, Throwable throwable) {
                populateListView(new ArrayList<Spacecraft>());
                mProgressBar.setVisibility(View.GONE);
                Toast.makeText(MainActivity.this, "ERROR: " + throwable.getMessage(), Toast.LENGTH_LONG).show();
            }
        });
    }
});
英文:

Just get the text from your editText by yourEditText.getText(); and pass the value to your request .. all this in the onButtonClickListener

follow belwo code

 searchBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //Get the value from edit text
                          String result = yourEdittext.getText().toString();
                        //Then pass the value to your request like that 
           final Call&lt;List&lt;Spacecraft&gt;&gt; call = myAPIService.searchSpacecraft(result);
                call.enqueue(new Callback&lt;List&lt;Spacecraft&gt;&gt;() {
    
                    @Override
                    public void onResponse(Call&lt;List&lt;Spacecraft&gt;&gt; call, Response&lt;List&lt;Spacecraft&gt;&gt; response) {
                        mProgressBar.setVisibility(View.GONE);
                        populateListView(response.body());
                    }
                    @Override
                    public void onFailure(Call&lt;List&lt;Spacecraft&gt;&gt; call, Throwable throwable) {
                        populateListView(new ArrayList&lt;Spacecraft&gt;());
                        mProgressBar.setVisibility(View.GONE);
                        Toast.makeText(MainActivity.this, &quot;ERROR: &quot;+throwable.getMessage(), Toast.LENGTH_LONG).show();
                    }
                });
        
                    }
                });

huangapple
  • 本文由 发表于 2020年8月24日 21:32:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63562107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定