Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在片段中向后按时,列表加倍_Android_Android Fragments - Fatal编程技术网

Android 在片段中向后按时,列表加倍

Android 在片段中向后按时,列表加倍,android,android-fragments,Android,Android Fragments,这是我在导航视图中的片段,当我在导航视图中单击一个项目并返回到此片段时,该片段默认打开。列表中的项目加倍。例如,在列表中,当我按back时,它有两个苹果 public class AllProductsActivity extends Fragment { private static final String TAG = MainActivity.class.getSimpleName(); private String category_id; // Movies json url pr

这是我在导航视图中的
片段
,当我在导航视图中单击一个项目并返回到此片段时,该片段默认打开。列表中的项目加倍。例如,在列表中,当我按back时,它有两个苹果

public class AllProductsActivity extends Fragment {
private static final String TAG = MainActivity.class.getSimpleName();
private  String category_id;
 // Movies json url
private  String url ;
private ProgressDialog pDialog;
private List<Movie> movieList=new ArrayList<Movie>() ;
private ListView listView;
private CustomListAdapter adapter;

 public AllProductsActivity() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Bundle bundle=getArguments();
    category_id = bundle.getString("IDS");
    View rootView = inflater.inflate(R.layout.category_layout, container, false);
    listView = (ListView) rootView.findViewById(R.id.categories_list);

    adapter = new CustomListAdapter(getActivity(), movieList);
    listView.setAdapter(adapter);

    category_id = bundle.getString("IDS");
    VerifyURL();
    return rootView;
}

private void VerifyURL() {

    if (category_id.equals("no_product")) {
        url = "http://192.168.43.149/Newfolder/gap.php";
        DownloadJSON();
    }else{
        url = "http://192.168.43.149/Newfolder/gap.php?category_id=" + category_id;
        DownloadJSON();

    }
}

private void DownloadJSON(){

    pDialog=new
            ProgressDialog(getActivity());
    // Showing progress dialog before making http request
    pDialog.setMessage("Loading...");
    pDialog.show();



    // Creating volley request obj
    JsonArrayRequest movieReq = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                    hidePDialog();

                    // Parsing json
                    for (int i = 0; i < response.length(); i++) {
                        try {

                            JSONObject obj = response.getJSONObject(i);
                            Movie movie = new Movie();
                            movie.setTitle(obj.getString("name"));
                            movie.setThumbnailUrl(obj.getString("image"));
                            movie.setRating(obj.getString("price"));
                            movie.setYear(obj.getString("description"));



                            // adding movie to movies array
                            movieList.add(movie);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                    // notifying list adapter about data changes
                    // so that it renders the list view with updated data
                    adapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            hidePDialog();

        }
    });

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(movieReq);
}

@Override
public void onDestroy() {
    super.onDestroy();
    hidePDialog();
}

private void hidePDialog() {
    if (pDialog != null) {
        pDialog.dismiss();
        pDialog = null;
    } }}
在onCreateView()方法中,清除列表

movieList.clear();

如果您没有多次调用api来使用分页,请调用
movieList.clear()
movieList=new ArrayList()之前。
因此,您的代码将是:-

// Checking of movieList is not null
if(movieList != null) {
    movieList.clear();
} else {
    movieList = new ArrayList<>();
}                   
for (int i = 0; i < response.length(); i++) {
   try {
      JSONObject obj = response.getJSONObject(i);
      Movie movie = new Movie();
      movie.setTitle(obj.getString("name"));
      movie.setThumbnailUrl(obj.getString("image"));
      movie.setRating(obj.getString("price"));
      movie.setYear(obj.getString("description"));

      // adding movie to movies array
      movieList.add(movie);

   } catch (JSONException e) {
        e.printStackTrace();
   }
}
//检查movieList不为空
if(movieList!=null){
movieList.clear();
}否则{
movieList=newarraylist();
}                   
对于(int i=0;i
使用以下方法:

 if (adapter==null) {
            prod = new ArrayList<>();
            adapter = new ProductsAdapter(prod, getActivity());

            if (Constants.isNetworkAvailable(getActivity())){

                recyclerView.setLayoutManager(GlayoutManager);
                recyclerView.setAdapter(adapter);

            }else {

                Constants.showToast(getActivity(),"No Internet!!");
                recyclerView.setLayoutManager(GlayoutManager);
                recyclerView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }

        }else{

            recyclerView.setLayoutManager(GlayoutManager);
            recyclerView.setAdapter(adapter);
            adapter.notifyDataSetChanged();

        }
if(适配器==null){
prod=newarraylist();
适配器=新产品适配器(prod,getActivity());
if(Constants.isNetworkAvailable(getActivity())){
recyclerView.setLayoutManager(GlayoutManager);
recyclerView.setAdapter(适配器);
}否则{
showtoos(getActivity(),“无互联网!!”);
recyclerView.setLayoutManager(GlayoutManager);
recyclerView.setAdapter(适配器);
adapter.notifyDataSetChanged();
}
}否则{
recyclerView.setLayoutManager(GlayoutManager);
recyclerView.setAdapter(适配器);
adapter.notifyDataSetChanged();
}

在将数据添加到ArrayList之前,只需清除列表即可。这意味着,当您单击back press时,它将首先清除,然后将数据插入列表。所以你总是能得到一份最新的名单

list.clear()

在fragement onCreateView中,使用条件,如if(adapter==null){then fetch data}else adapter.notifydatasetchangedit shows无法解析方法notifydatasetchangedadapter.notifyDataSetChanged();它不会显示任何数据
list.clear()