我有一個片段,它在應用程序啓動時不包含任何內容,但是在加載數據之後,它應該顯示加載的數據。我在加載MainActivity的函數onDataLoaded中實現了DataLoadedListener。數據加載後,函數通過函數調用(initializeAdapter())將數據(ArrayList)作爲參數發送。然後,函數initializeAdapter()獲取它並初始化定製的適配器(適配器是一個全局對象,在initializeAdapter()中初始化並在onCreateView()中使用)。在Android中刷新片段
我想我應該在數據加載和適配器初始化後刷新片段視圖,但我不知道該怎麼做。我嘗試了很多方法,但沒有成功。在MainActivity
onDataLoaded函數調用initializeAdapter()
@Override
public void onDataLoaded(List<Grad> gradovi, List<Ponuda> ponude) {
Spinner spinnerGradovi = (Spinner) findViewById(R.id.gradovi_spinner);
ArrayAdapter<String> adapterGradovi;
List<String> listaGradova = new ArrayList<>();
ArrayList<Ponuda> ponudaArrayList = new ArrayList<Ponuda>();
ponudaLista = ponude;
gradLista = gradovi;
for(Grad grad : gradovi){
listaGradova.add(grad.getNaziv());
}
for(Ponuda ponuda : ponude){
ponudaArrayList.add(ponuda);
}
adapterGradovi = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listaGradova);
spinnerGradovi.setAdapter(adapterGradovi);
Fragment fragmentGet = svePonudeFragment;
((SvePonudeFragment)fragmentGet).initializeAdapter(ponudaArrayList);
}
,這是一個片段
public class SvePonudeFragment extends Fragment {
private RecyclerView rv;
RVAdapter adapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.sve_ponude_fragment, container, false);
rv = (RecyclerView) rootView.findViewById(R.id.rv);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
rv.setAdapter(adapter);
return rootView;
}
public void initializeAdapter(List<Ponuda> preuzetePonude){
adapter = new RVAdapter(preuzetePonude);
//this isn't working
/*FragmentManager manager = getActivity().getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft = manager.beginTransaction();
ft.detach(this).attach(this).commit();*/
//this is not working, neither
Fragment frg = getFragmentManager().findFragmentByTag("sve_ponude_fragment_tag");
final android.support.v4.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
}
}
我也有興趣在如何在數據沒有被裝載做它加載一個空適配器然而?並且當數據被加載以填充另一個適配器並使用它時。