android tabbed Activity list fragment

223 views Asked by At

I am new to android, I have my TabActivity(Action bar tab) with some fragments. In first Tab, I have a list and I want to display the content of the selected list in the second tab when I switch over the second Tab. In my code when I selected the list item in the first tab, it switch over to the second Tab but the content not update but when I move on to the tab4 and tab5 then return to tab2,I get the content update in the second Tab? I don't know how the Tab works? So give me an example tabbed list Fragment

My first Fragment (tab1)

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view;
    Toast.makeText(getActivity(),"frag1",Toast.LENGTH_SHORT).show();
    view = inflater.inflate(R.layout.fragment_company, container, false);
    TextView textView = (TextView)view.findViewById(R.id.section_label);
    textView.setText("Company");

    ListView listView = (ListView)view.findViewById(R.id.companyList) ;
    final List<Company> companies = Company.fetchCompanies();
    final List<String> compamnyNames = new ArrayList<String>();

    for (Company company : companies) {
        compamnyNames.add(company.getName().getName());
    }

    ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1,compamnyNames);
    listView.setAdapter(listAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
           Company.setSelectedCompany(companies.get(i));

            TabLayout tabLayout = (TabLayout) getActivity().findViewById(R.id.tabs);
            tabLayout.getTabAt(1).select();

        }
    });

    return view;

}

My second fragment (tab2)

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

    return loadTableData();
}


public View loadTableData()
{
    LayoutInflater inflater = getActivity().getLayoutInflater();

    ViewGroup container = (ViewGroup) getView();

    View view;

    view = inflater.inflate(R.layout.fragment_outstanding, container, false);
    OutstandingAdapterGenerator adapterGenerator = new OutstandingAdapterGenerator();
    adapterGenerator.setContext(getActivity());

    ListView listView = (ListView)view.findViewById(R.id.outstandingPurchaseList) ;


    adapterGenerator.setListViewResource(R.layout.list_item);
    adapterGenerator.setPurchase(true);
    adapterGenerator.setCompany(Company.getSelectedCompany());
    adapterGenerator.setViewIds(new int[]{R.id.textViewPartyName,R.id.textViewTotal});
    SimpleAdapter listAdapter = adapterGenerator.generateAdapter();
    listView.setAdapter(listAdapter);
    return view;
}



@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);



    if(isVisibleToUser && isResumed())
    {

        loadTableData();
    }
}
2

There are 2 answers

2
masoud vali On BEST ANSWER

override setUserVisibleHint method in your second fragment and put your updating code in it, I just wrote what i get from your code that it is your updating part

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
   super.setUserVisibleHint(isVisibleToUser);
   if (isVisibleToUser && isResumed()) {
        adapterGenerator.setCompany(Company.getSelectedCompany());
        adapterGenerator.setViewIds(new int[] {R.id.textViewPartyName,R.id.textViewTotal});
        SimpleAdapter listAdapter = adapterGenerator.generateAdapter();
        listView.setAdapter(listAdapter);
   }
}
0
Narandhran Thangavel On
//my view pager


mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        mViewPager.setOffscreenPageLimit(6);

//onCreateView

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view;
        view = inflater.inflate(R.layout.fragment_outstanding, container, false);
        return view;
    }

//UserVisibleHint

    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(isVisibleToUser)
        {
            OutstandingAdapterGenerator adapterGenerator_purchase = new OutstandingAdapterGenerator();
        adapterGenerator_purchase.setContext(getActivity());

        ListView listView = (ListView)getView().findViewById(R.id.outstandingPurchaseList) ;


        adapterGenerator_purchase.setListViewResource(R.layout.list_item);
        adapterGenerator_purchase.setPurchase(true);
        adapterGenerator_purchase.setCompany(Company.getSelectedCompany());
        adapterGenerator_purchase.setViewIds(new int[]{R.id.textViewPartyName,R.id.textViewTotal});
        SimpleAdapter listAdapter = adapterGenerator_purchase.generateAdapter();
        listView.setAdapter(listAdapter);

        }
    }