我对此非常陌生(java,android和堆栈溢出),所以请耐心等待。我从数据库中读取了一堆产品,然后将它们存储/添加到产品类型的ArrayList中,并将其输出到Recycleview。我还从GitHub实现了一个名为materialsearchview的searchview库,一切按预期进行。这意味着最初我在列表中有每个产品,而当我搜索时,我会得到一个新列表,其中包含相同或接近相同标题的产品。但是,旋转手机后,该列表将返回到每个产品的默认列表,并且将保留搜索视图中的文本。在此片段中旋转手机后,是否可以保留从搜索中获得的新产品列表?
public class GFragment extends Fragment{
private static final String READ_ALL_PRODUCTS_REQUEST_URL = "****";
RecyclerView recyclerView;
ProductsAdapter adapter;
List<Product> productList;
TextView product_id;
ImageView product_image;
MaterialSearchView searchView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable
ViewGroup container, @Nullable final Bundle savedInstanceState) {
setHasOptionsMenu(true);
View view = inflater.inflate(R.layout.fragment_products, null);
productList = new ArrayList<>();
recyclerView = (RecyclerView) view.findViewById(R.id.list);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
StringRequest stringRequest = new StringRequest(Request.Method.GET,
READ_ALL_PRODUCTS_REQUEST_URL , new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray products = new JSONArray(response);
for (int i = 0; i < products .length(); i++) {
JSONObject productObject = products.getJSONObject(i);
//storing the variables from database
String id = productObject.getString("product_id");
String title=
productObject.getString("product_title");
Product product= new Product(id);
productList.add(product);
}
adapter = new productAdapter(getActivity(), productList);
recyclerView.setAdapter(adapter);
//Search
searchView = (MaterialSearchView)
getActivity().findViewById(R.id.search_view);
searchView.setOnSearchViewListener(new
MaterialSearchView.SearchViewListener() {
@Override
public void onSearchViewShown() {
}
@Override
public void onSearchViewClosed() {
searchView = (MaterialSearchView)
getActivity().findViewById(R.id.search_view);
adapter = new
productAdapter(getActivity(),productList);
recyclerView.setAdapter(adapter);
}
});
searchView.setOnQueryTextListener(new
MaterialSearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
if (query != null && !query.isEmpty()) {
List<Product> currentproductList = new
ArrayList<Product>();
for (Productitem : producteList) {
String title = item.getProduct_title();
if (title.contains(query) ||
title.toLowerCase().contains(query) ||
title.toUpperCase().contains(query)) {
currentproductList.add(item);
}
}
adapter = new VideoGameAdapter(getActivity(),
currentproductList);
recyclerView.setAdapter(adapter);
}
else {
adapter = new productAdapter(getActivity(),
productList);
recyclerView.setAdapter(adapter);
}
//Hide Keyboard
View view =
getActivity().findViewById(android.R.id.content);
if (view != null) {
InputMethodManager imm = (InputMethodManager)
getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.
getWindowToken(), 0);
}
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
if (newText != null && !newText.isEmpty()) {
List<Product> currentproductList = new
ArrayList<Product>();
for (Productitem : productList) {
String title = item.getProduct_title();
if (title.contains(newText) ||
title.toLowerCase().contains(newText) ||
title.toUpperCase().contains(newText)) {
currentproductList.add(item);
}
}
adapter = new productAdapter(getActivity(),
currentproductList);
recyclerView.setAdapter(adapter);
}
else {
adapter = new productAdapter(getActivity(),
productList);
recyclerView.setAdapter(adapter);
}
return true;
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(), "Error",
Toast.LENGTH_SHORT).show();
}
});
Volley.newRequestQueue(getActivity()).add(stringRequest);
return view
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.main, menu);
MaterialSearchView searchView =
(MaterialSearchView)getActivity().findViewById(R.id.search_view);
final MenuItem item = menu.findItem(R.id.action_search);
searchView.setMenuItem(item);
}
}
答案 0 :(得分:0)
我将使用onSaveInstance状态,这将在轮换之间保存您的状态。将JSON结果转换为全局字符串,然后检索结果。它将像这样:
public class GFragment extends Fragment{
private String json;
private final String KEY_JSON = "json";
//..rest of variables
}
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(KEY_JSON, user);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable
ViewGroup container, @Nullable final Bundle savedInstanceState) {
if (savedInstanceState != null) {
json = savedInstanceState.getString(KEY_JSON);
} else {
json = ""
}
//...rest of onCreateView()
if (!TextUtils.isEmpty(json) {
parseJSON();
}
}
private void parseJSON() {
try {
JSONArray products = new JSONArray(response);
for (int i = 0; i < products .length(); i++) {
JSONObject productObject = products.getJSONObject(i);
//storing the variables from database
String id = productObject.getString("product_id");
String title=
productObject.getString("product_title");
Product product= new Product(id);
productList.add(product);
}
adapter = new productAdapter(getActivity(), productList);
recyclerView.setAdapter(adapter);
//..any other parsing
}
catch (JSONException e) {
e.printStackTrace();
}
}