填充listview片段选项卡android

时间:2015-08-13 07:26:44

标签: android android-fragments android-listview android-tabs populate

我的问题是我的列表视图没有显示活动何时加载。

这是我的活动代码:

protected void onCreate(Bundle savedInstanceState) {
...
 TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.addTab(tabLayout.newTab().setText("A"));
        tabLayout.addTab(tabLayout.newTab().setText("B"));
        tabLayout.addTab(tabLayout.newTab().setText("C"));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        getServices();

        final ViewPager viewPager = (ViewPager)       findViewById(R.id.pager);
        final PagerAdapter adapter = new PagerAdapter
                (getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        viewPager.addOnPageChangeListener(new        TabLayout.TabLayoutOnPageChangeListener(tabLayout));

        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        adapter.notifyDataSetChanged();
  }





   public void getServices(){
    RequestQueue queue;
    queue = Volley.newRequestQueue(this);


    String url = "http://..."

    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET,url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    for(int i=0;i<response.length();i++){
                        try{

                            JSONObject jsonObjectService =        response.getJSONObject(i);
                            Service service = new Service(jsonObjectService);
                            serviceList.add(service);
                        }
                        catch(JSONException e){
                            e.printStackTrace();
                        }
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

    queue.add(jsonArrayRequest);
}

这是我的片段:

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

    v = inflater.inflate(R.layout.fragment_tab_fragment1, container, false);

    activity = (SalonInformationsActivity) getActivity();
    listView = (ListView) v.findViewById(R.id.list_service);
    serviceList = activity.getServiceList();
    adapter = new ServiceListAdapter(activity, serviceList);
    listView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            adapter.notifyDataSetChanged();
        }
    });

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
            ((SalonInformationsActivity)getActivity()).callSalon(v.findFocus());
            //Toast.makeText(getApplicationContext(), salonArrayList.get(position).getTel(), Toast.LENGTH_SHORT).show();
        }
    });
    return v;
}

我是Android的新手,我不知道为什么listview没有显示。但是,如果我在其他选项卡上clic然后返回tabA,列表视图出现,我的列表视图更新。什么解决方案,以便在列表视图启动时填充它?

1 个答案:

答案 0 :(得分:0)

您的网络通话需要时间来返回您添加到您的arraylist的数据,以片段而非活动的形式执行该网络调用,并且一旦将其添加到arraylist,就会在适配器上执行notifyDataSetChanged()

在您当前的情况下,在返回newtwork数据之前创建片段,以便arraylist size为0

并且不需要这个

 activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            adapter.notifyDataSetChanged();
        }
    });

您只需拨打adapter.notifyDataSetChanged();

即可