Java 单击搜索按钮后无法刷新更新的数据(通过API获取数据)
当我单击搜索按钮时,我无法更新值,数据被提取,但旧提取的数据没有被丢弃,提取的数据相互重叠 SearchFragment.javaJava 单击搜索按钮后无法刷新更新的数据(通过API获取数据),java,android,Java,Android,当我单击搜索按钮时,我无法更新值,数据被提取,但旧提取的数据没有被丢弃,提取的数据相互重叠 SearchFragment.java package com.example.recipeappandroid.Fragments; import android.os.Bundle; import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.LinearLayoutManager; import andro
package com.example.recipeappandroid.Fragments;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.RetryPolicy;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.recipeappandroid.Adapter.RecipeAdapter;
import com.example.recipeappandroid.Model.Recipe;
import com.example.recipeappandroid.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class SearchFragment extends Fragment {
Button click;
//public static TextView fetchedText;
ImageView searching_logo;
TextView searching_text;
SearchView searchbar;
String query="";
RecyclerView recyclerView;
public static ArrayList<Recipe> recipeList;
public static RecipeAdapter recipeAdapter;
private RequestQueue mRequestQueue;
private String Api_id= "3f335994";
private String Api_key = "8e99e327d1f2130dc6ab3422e26a95e8";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_search, container, false);
click = (Button) view.findViewById(R.id.button1);
//fetchedText = (TextView) view.findViewById(R.id.fetcheddata);
searchbar = (SearchView) view.findViewById(R.id.searchbar);
searching_logo = view.findViewById(R.id.searching_logo);
searching_text = view.findViewById(R.id.searching_text);
recyclerView = view.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
//recipeAdapter = new RecipeAdapter();
recipeList = new ArrayList<>();
//getData();
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
query = searchbar.getQuery().toString();
String url = "https://api.edamam.com/search?q=" + query + "&app_id=" + Api_id + "&app_key=" + Api_key;
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url,null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray hits = response.getJSONArray("hits");
for (int i =0;i<hits.length();i++) {
JSONObject jsonObject = hits.getJSONObject(i);
JSONObject recipe = jsonObject.getJSONObject("recipe");
String recipe_img = recipe.getString("image");
String recipe_title = recipe.getString("label");
String recipe_data = recipe.getString("source");
recipeList.add(new Recipe(recipe_img,recipe_title,recipe_data));
}
recipeAdapter = new RecipeAdapter(getContext(),recipeList);
recyclerView.setAdapter(recipeAdapter);
recipeAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mRequestQueue = Volley.newRequestQueue(getContext());
mRequestQueue.add(jsonObjectRequest);
/*JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
for (int i = 0; i < response.length(); i++) {
JSONObject jsonObject = response.getJSONObject(i);
JSONObject recipes = jsonObject.getJSONObject("recipe");
//Recipe recipe = new Recipe();
String recipe_img = recipes.getString("image");
String recipe_title = recipes.getString("label");
String recipe_data = recipes.getString("source");
recipeList.add(new Recipe(recipe_img,recipe_title,recipe_data));
Log.d("data",recipe_title);
}
//recipeAdapter = new RecipeAdapter(getContext(), recipeList);
//recyclerView.setAdapter(recipeAdapter);
recipeAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Toast.makeText(SearchFragment.this,"Error Occured",Toast.LENGTH_SHORT).show();
error.printStackTrace();
}
});*/
/*jsonArrayRequest.setRetryPolicy(new RetryPolicy() {
@Override
public int getCurrentTimeout() {
return 3000;
}
@Override
public int getCurrentRetryCount() {
return 3000;
}
@Override
public void retry(VolleyError error) throws VolleyError {
}
});*/
/* Log.d("QUEEEERRRYYYY",query);
ApiCall process = new ApiCall(searching_logo,searching_text);
process.execute(query);*/
}
});
return view;
}
}
package com.example.recipeappandroid.Fragments;
导入android.os.Bundle;
导入androidx.fragment.app.fragment;
导入androidx.recyclerview.widget.LinearLayoutManager;
导入androidx.recyclerview.widget.recyclerview;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.Button;
导入android.widget.ImageView;
导入android.widget.SearchView;
导入android.widget.TextView;
导入android.widget.Toast;
导入com.android.volley.Request;
导入com.android.volley.RequestQueue;
导入com.android.volley.Response;
导入com.android.volley.RetryPolicy;
导入com.android.volley.VolleyError;
导入com.android.volley.toolbox.JsonArrayRequest;
导入com.android.volley.toolbox.JsonObjectRequest;
导入com.android.volley.toolbox.volley;
导入com.example.recipeappandroid.Adapter.RecipeAppter;
导入com.example.recipeappandroid.Model.Recipe;
导入com.example.recipeappandroid.R;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.ArrayList;
公共类SearchFragment扩展了片段{
点击按钮;
//公共静态文本视图获取文本;
ImageView搜索_徽标;
文本视图搜索文本;
搜索视图搜索栏;
字符串查询=”;
回收视图回收视图;
公共静态数组列表recipeList;
公共静态RecipeAdapter RecipeAdapter;
私有请求队列mRequestQueue;
私有字符串Api_id=“3f335994”;
专用字符串Api_key=“8e99e327d1f2130dc6ab3422e26a95e8”;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
视图=充气机。充气(R.layout.fragment\u search,container,false);
单击=(按钮)view.findViewById(R.id.button1);
//fetchedText=(TextView)view.findViewById(R.id.fetcheddata);
searchbar=(SearchView)view.findviewbyd(R.id.searchbar);
searching_logo=view.findviewbyd(R.id.searching_logo);
搜索文本=view.findviewbyd(R.id.search\u text);
recyclerView=view.findViewById(R.id.recycler\u视图);
recyclerView.setHasFixedSize(true);
LinearLayoutManager LinearLayoutManager=新的LinearLayoutManager(getContext());
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
//recipeAdapter=新recipeAdapter();
recipeList=新的ArrayList();
//getData();
单击.setOnClickListener(新建视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
query=searchbar.getQuery().toString();
字符串url=”https://api.edamam.com/search?q=“+query+”&app_id=“+Api_id+”&app_key=“+Api_key;
JsonObjectRequest JsonObjectRequest=新的JsonObjectRequest(Request.Method.GET,url,null,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
JSONArray hits=response.getJSONArray(“hits”);
for(int i=0;i看起来您只是在将项添加到ArrayList recipeList;
,因此它们可能会重复
也许可以尝试recipeList.clear();
在onResponse
方法的第一行中使用它
另外,不需要notifyDataSetChanged
,setAdapter
自己做(事实上还有更多)