vendredi 29 mai 2015

Getting same list for two tabs in tabs with listview on start of activity

So,I have this activity which has tabs with listviews in them, and the problems I'm facing are:

The first and second tabs are having the same content i.e. the content the second tab should have is in first and second tab.

The third tab has correct content, though this is because it only instantiates just first and second tabs initially. When I set
mViewPager.setOffscreenPageLimit(2);

all the three tabs have same content, which is that of the third tab.

But after i've scrolled to third tab and then scroll back to the first one, the first one refreshes and has correct content.

Also, the next problem is the second tab never refreshes,because its always on the right or left to the tab which is opened. I want the tabs to refresh every time they are opened since i'll be constantly changing the material in the lists.

package com.towntrot.anil.towntrot_eventmanager_02;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class SlidingTabsBasicFragment extends Fragment {

ListView list;
public ArrayList<ListModel> CustomListViewValuesArr = new ArrayList<ListModel>();
public guestlist gg;
private SlidingTabLayout mSlidingTabLayout;
private ViewPager mViewPager;
private String[] eventtype={"CHECKED IN","CHECKED OUT","WAITING"};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_sample, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
    mViewPager.setAdapter(new SamplePagerAdapter());
    mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
    mSlidingTabLayout.setViewPager(mViewPager);
}

class SamplePagerAdapter extends PagerAdapter {

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public boolean isViewFromObject(View view, Object o) {
        return o == view;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return eventtype[position];
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        View view = getActivity().getLayoutInflater().inflate(R.layout.pager_item,
                container, false);
        guestlist gg=(guestlist)getActivity();
        StringBuilder sb = new StringBuilder();
        sb.append("");
        sb.append(position);
        String strI = sb.toString();
        Toast.makeText(getActivity(),strI,Toast.LENGTH_LONG).show();
        setListData(gg.getNamelist(), gg.getStatuslist(), gg.getNo0fPeople(), position);
        list= ( ListView )view.findViewById(R.id.list);
        CustomAdapter adapter;
        adapter=new CustomAdapter( getActivity(), CustomListViewValuesArr);
        list.setAdapter(adapter);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

}
public void setListData(String[] string,String[] status,int no,int pos) {
    CustomListViewValuesArr.clear();
    for (int i = 0; i < no; i++) {

        final ListModel name = new ListModel();
        int x=Integer.parseInt(status[i]);
        if(pos==x){ name.setCompanyName(string[i]);
        CustomListViewValuesArr.add(name);}
    }
}


}

Please help me, I've seen a lot of answers but couldn't solve this. Any help is appreciated.

Aucun commentaire:

Enregistrer un commentaire