ListView和GridView在制表符开关之间被清除

时间:2014-11-25 22:20:20

标签: android listview gridview android-listview fragment

我有一个包含3个标签的活动。每次我在选项卡之间切换时,我的ListView和GridView(在第一个选项卡上)都会被清除,有趣的是ArrayList包含之前下载的所有数据,如果我调用addToGrid()函数,则ListView会很好地填充。

为什么在切换选项卡时清除列表和网格视图。是否在每个制表符开关上重新创建了包含的片段?我是否需要查询我的数据库并在每个制表符开关之间重新填充我的视图?

谢谢!

按方式 - 我在onResume函数中添加了“if”-statement。如果我留下“if”语句,listview将行加倍,并在切换回时再次重新填充。但是如果使用“if”语句,我的listviews会在onResume上保持为空,因为正如我所提到的,ArrayList仍然拥有所有数据。

摘自包含ListView的片段:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            super.onCreateView(inflater, container, savedInstanceState);
            v = inflater.inflate(R.layout.offers_view, null, false);                                    

            SharedPreferences preferences = getActivity().getSharedPreferences("USERINFO", 
                    Context.MODE_PRIVATE);
            user = preferences.getString("name", null);


            Button showAllUserImages = (Button) v.findViewById(R.id.showAllUserImages);
            showAllUserImages.setOnClickListener(new OnClickListener()  {
                @Override
                  public void onClick(View v)  {                    
                    Intent i = new Intent(getActivity(), AllUserImages.class);
                    startActivity(i);                      
                }                
               }); 

            RelativeLayout topLoc = (RelativeLayout) v.findViewById(R.id.topLocationsBtn);
            topLoc.setOnClickListener(new OnClickListener()  {
                @Override
                  public void onClick(View v)  {                    
                       Intent loc = new Intent(getActivity(), com.fideli.top.guide.TopLocations.class);
                       startActivity(loc);                         
                    }                
               }); 

            RelativeLayout guide = (RelativeLayout) v.findViewById(R.id.guideBtn);
            guide.setOnClickListener(new OnClickListener()  {
                @Override
                  public void onClick(View v)  {                    
                       Intent i= new Intent(getActivity(), com.fideli.top.guide.GloveGuide.class);
                       startActivity(i);                       
                    }                
               }); 



          return v;          
    }



    @Override
    public void onPause() {     
        super.onPause();
        System.out.println("paused!");


    }

    @Override
    public void onResume() {        
        super.onResume();

        if(imageItems.isEmpty()){
        getInfoFromDB();

        }

        if(tasteMakersList.isEmpty()){
        getTastemakers();
        }

        if(feedList.isEmpty()){
        downloadFeed();
        }

    }



   private void downloadFeed() {

       AsyncHttpClient client = new AsyncHttpClient();      
        System.out.println("Downloading Feed!!");
        client.get("http://www.edmondvarga.com/android_dev/fideli/get-relations.php?mainUser=" + user, null, 
                                    new JsonHttpResponseHandler() {             
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                try {                                       
                    JSONArray contacts = response.getJSONArray("info");

                    for (int i=0; i< contacts.length();i++){
                        JSONObject json = contacts.getJSONObject(i);                            

                        String followedUser = json.getString("folwed_users");                       
                        getFeed(followedUser);

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

         });


    }



    public void getInfoFromDB(){

        datasource = new SQLiteDataSource(getActivity());
        datasource.open();


        List<ImageItem> values = datasource.getAllImageItems();
        for (int i = 0; i < values.size(); i++) {

            ImageItem ii = new ImageItem(null, null);

            ii.setImage(values.get(i).getImage());
            ii.setUser(values.get(i).getUser());
            ii.setUserId(values.get(i).getUserId());
            ii.setLikes(values.get(i).getLikes());
            ii.setDescription(values.get(i).getDescription());
            ii.setLocation(values.get(i).getLocation());    
            ii.setAddress(values.get(i).getAddress());
            ii.setCity(values.get(i).getCity());
            ii.setLat(values.get(i).getLat());
            ii.setLng(values.get(i).getLng());          


            imageItems.add(ii);
            addToGrid();
        }

    }

            public void addToGrid() {
                gridView = (GridView) v.findViewById(R.id.gridView);
                customGridAdapter = new GridViewAdapter(getActivity(), R.layout.row_grid, imageItems);          
                gridView.setAdapter(customGridAdapter);         
                gridView.setOnItemClickListener(this); 
            }   

1 个答案:

答案 0 :(得分:1)

您的视图很可能会被破坏,但片段可能不会被破坏。我知道当用户浏览了多个页面时,会在ViewPager中发生这种情况。因此,如果您的imageItems变量是实例变量,则不会重新创建它,因为片段仍然是创建的。请参阅Fragments指南中的生命周期图。