英文:
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<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();
}
});
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论