构建标签式布局,我有3个片段类,所有这些类都将值下载为JSON
并填充为RecyclerView
。
现在问题是:当我在一个片段上,并切换到另一个并返回到旧片段时,我看到第一个片段的副本,如果我在片段中有一张卡片,当我回来时,我得到两张相同的卡片(复制品)。
以下是代码:
public class ReceiptFragment extends Fragment
{
private static String URL_FEE;
private List<FeePojo> fee = new ArrayList<FeePojo>();
private RecyclerView feeList;
CustomFeeAdapter feeAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.receipt, container, false);
feeAdapter = new CustomFeeAdapter(fee);
SharedPreferences pref = this.getActivity().getSharedPreferences("ActivitySession", Context.MODE_PRIVATE);
URL_FEE = " https://api.eduknow.info/mobile/fees/buttercup/"+pref.getString("MOB","");
Log.w("CHECK::",""+URL_FEE);
feeList = (RecyclerView)rootView.findViewById(R.id.rv_fee);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
feeList.setLayoutManager(llm);
feeList.setAdapter(feeAdapter);
JsonArrayRequest feeReq = new JsonArrayRequest(URL_FEE,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d("EDUKNOW::FEE", response.toString());
//hidePDialog();
SharedPreferences Tempx = getActivity().getSharedPreferences("ActivitySession", Context.MODE_PRIVATE);
SharedPreferences.Editor edx = Tempx.edit();
edx.putString("GSON_FEE", response.toString());
edx.apply();
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonArray jArray = parser.parse(Tempx.getString("GSON_FEE","")).getAsJsonArray();
//ArrayList<MainPojo> uid = new ArrayList<MainPojo>();
for(JsonElement obj : jArray )
{
FeePojo cse = gson.fromJson( obj , FeePojo.class);
fee.add(cse);
}
Log.w("EDUKNO::CHECK",""+fee.get(0).getSchoolName());
feeAdapter.notifyDataSetChanged();
// Parsing json
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error)
{
SharedPreferences Tempx = getActivity().getSharedPreferences("ActivitySession", Context.MODE_PRIVATE);
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonArray jArray = parser.parse(Tempx.getString("GSON_FEE","")).getAsJsonArray();
//ArrayList<MainPojo> uid = new ArrayList<MainPojo>();
for(JsonElement obj : jArray )
{
FeePojo cse = gson.fromJson( obj , FeePojo.class);
fee.add(cse);
feeAdapter.notifyDataSetChanged();
}
VolleyLog.d("EDUKNOW::::", "Error: " + error.getMessage());
//hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(feeReq);
return rootView;
}
}
可能可能造成这种情况?最新的解决办法是什么?谢谢!
PS:Logcat中没有错误/警告
答案 0 :(得分:1)
可能导致这种情况的原因是什么?什么是最好的选择 解决这个问题?
由于private List<FeePojo> fee = new ArrayList<FeePojo>();
行,因为fee
对象在从其他片段返回时包含旧项目。
在切换到下一个片段时清除fee
中的所有数据或在fee
ArrayList.Like中添加新项目之前调用clear:
fee.clear(); //<<<<
for(JsonElement obj : jArray )
{
FeePojo cse = gson.fromJson( obj , FeePojo.class);
fee.add(cse);
}
修改强>
最好是当ArrayList是数据时再次避免再次调用,否则每当用户从其他片段返回时请求它发生:
if(fee.isEmpty()){
AppController.getInstance().addToRequestQueue(feeReq);
}
答案 1 :(得分:0)
如果你转移初始化:
fee = new ArrayList<FeePojo>();
前面的onCreateView
feeAdapter = new CustomFeeAdapter(fee);
它应该自动清除旧列表