我通过使用它提取的PHP Web服务在Android中获取数据但是当添加新数据时它无法获取?

时间:2017-08-07 09:29:17

标签: android

通过使用以下代码,我将获取数据。当我第一次运行它时会显示所有数据。当我插入新数据时,它将成功插入,但在转到报告页面并单击它显示以前的数据而不是新的或更新的数据。

public class Fragment_Emp_Report extends Fragment {

    List<Get_EmpNameAdapter> get_empNameAdapter1;
    RecyclerView recyclerView;

    ProgressBar progressBar;
    RecyclerView.LayoutManager recyclerViewlayoutManager;
    RecyclerView.Adapter recyclerViewadapter;

    String url = "http://ghawadediilip.000webhostapp.com/Services/jsonFetchName.php";
    String JSON_NAME = "emp_name";

    JsonArrayRequest jsonArrayRequest;
    RequestQueue  requestQueue;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_emp_report,container,false);

        fetchEmpName();

        return view;

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        get_empNameAdapter1 = new ArrayList<>();
        recyclerView = (RecyclerView)view.findViewById(R.id.report_recyclerview);



        recyclerView.setHasFixedSize(true);

        recyclerViewlayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());

        recyclerView.setLayoutManager(recyclerViewlayoutManager);
    }
    public void fetchEmpName()
    {
        jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                jsonFetch(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
        requestQueue.add(jsonArrayRequest);
        RequestQueue.RequestFinishedListener listener = new RequestQueue.RequestFinishedListener() {
            @Override
            public void onRequestFinished(Request request) {
                recyclerViewadapter.notifyDataSetChanged();
            }
        };
        requestQueue.addRequestFinishedListener(listener);

    }
    public void jsonFetch(JSONArray array)
    {
        for (int i=0;i<array.length();i++)
        {
            Get_EmpNameAdapter get_empNameAdapter2 = new Get_EmpNameAdapter();
            JSONObject json=null;
            try {
                json = array.getJSONObject(i);
                get_empNameAdapter2.setEmpname(json.getString(JSON_NAME));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            recyclerView.setAdapter(recyclerViewadapter);
            get_empNameAdapter1.add(get_empNameAdapter2);
        }
        recyclerViewadapter = new Report_Recyclerview_Adapter(get_empNameAdapter1,getContext());
        recyclerView.setAdapter(recyclerViewadapter);
    }
}

1 个答案:

答案 0 :(得分:0)

你的fetchEmpName();调用onCreateView时只调用一次,如果你想从另一个片段返回时重新获取日期,那么你必须再次调用你的方法,你可以通过在onResume中调用它来实现它。