我是新來的android,我有我的TabActivity
(動作欄選項卡)與一些片段。在第一個選項卡中,我有一個列表,當我切換第二個選項卡時,我想在第二個選項卡中顯示所選列表的內容。在我的代碼中,當我在第一個選項卡中選擇列表項目時,它切換到第二個選項卡,但內容不更新,但是當我移到tab4和tab5然後返回到tab2時,我在第二個選項卡中獲取內容更新?我不知道Tab是如何工作的?所以給我一個例子標籤列表Fragment
android tabbed活動列表片段
我的第一個片段(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;
}
我的第二個片段(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();
}
}
請添加您的更新代碼中的第二個片段 –
請上傳您爲此做了代碼。 –