我想在搜索后没有找到结果的时候展示吐司,我做了一个简单的吐司,但是吐司留了很长时间,我想在短时间后它就消失了
这是代码
public boolean onQueryTextSubmit(String query) {
final String[] dose = getResources().getStringArray(R.array.doses);
final String[] use = getResources().getStringArray(R.array.uses);
final String[] composition = getResources().getStringArray(R.array.composition);
final TypedArray img = getResources().obtainTypedArray(R.array.img);
for (String search : suggestions)
{
if (query.equals(search)){
for (int i =0; i<suggestions.length; i++){
if (query.equals(suggestions[i])){
Intent intent = new Intent(MainActivity.this,productDetails.class);
int im = img.getResourceId(i,-1);
intent.putExtra("img",im);
intent.putExtra("dose",dose[i]);
intent.putExtra("use",use[i]);
intent.putExtra("composition",composition[i]);
intent.putExtra("title",suggestions[i].toUpperCase());
startActivity(intent);
}
}
}
else if (query!= search){
Toast.makeText(MainActivity.this, "No Results Found", Toast.LENGTH_SHORT).show();
}
}
return false;
}
您的toast显示在for循环中,他被添加到队列中多次,将其从循环中删除