2016-11-21 176 views
0

我有一個片段,它在應用程序啓動時不包含任何內容,但是在加載數據之後,它應該顯示加載的數據。我在加載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(); 
} 

} 

我也有興趣在如何在數據沒有被裝載做它加載一個空適配器然而?並且當數據被加載以填充另一個適配器並使用它時。

回答

0

在設置之前初始化適配器。如果你有興趣改變你的方法,我可以給你建議:爲你創建一個可以傳遞ArrayList的構造函數。 OnCreate或OnCreateView可以初始化適配器並將其傳入。在設置適配器notifyDataChanges並檢查是否有數據之後,請在傳遞數據和傳遞數據之前使用一些日誌。不要直接責怪適配器或片段控制你的數據。此外,我建議你學習更多使用片段+ RecyclerView。我發佈了一個例子,你可以看看,但數據加載在片段中。

public class CategoryFragment extends Fragment { 

//Class Tag 
private static final String TAG = CategoryFragment.class.getSimpleName(); 
//Fragments base context 
private Context mContext; 
//RecyclerView widget 
private RecyclerView mRecyclerView; 
//RecyclerView's Layout Manager 
private RecyclerView.LayoutManager mLayourManager; 
//RecyclerView's Adapter 
private CategoryFragmentAdapter mCategoryFragmentAdapter; 
//Category Data List 
private ArrayList<Category> mCategoryList; 
//Interface object 
onCategoryItemClick mCallBackCategory; 

@Override 
public void onAttach(Context context) { 
    super.onAttach(context); 
    //Instantiate base context 
    mContext = context; 
    //Instantiate interface 
    mCallBackCategory = (onCategoryItemClick)getActivity(); 
} 

@Override 
public void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //Instantiate Category List 
    mCategoryList = new ArrayList<Category>(); 
} 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    super.onCreateView(inflater, container, savedInstanceState); 
    //Custom fragment view instantiating 
    View view_fragment_category = inflater.inflate(R.layout.fragment_items, container, false); 
    //Instantiating Fragment's recycler view 
    mRecyclerView = (RecyclerView) view_fragment_category.findViewById(R.id.recycle_categories); 
    //Instantiate RecycleView's Layout Manager: LinearLayoutManager is one of the default 
    mLayourManager = new LinearLayoutManager(mContext); 
    //Setting layout manager for recycler view 
    mRecyclerView.setLayoutManager(mLayourManager); 
    //Retrieve data from server, populate the category data list and set the RecyclerView's adapter 
    getCategories(); 
    //Instantiate the adapter and push the data 
    mCategoryFragmentAdapter = new CategoryFragmentAdapter(mContext, mCategoryList); 

    //RecyclerView item click listener 
    mCategoryFragmentAdapter.SetOnItemClickListener(new CategoryFragmentAdapter.OnItemClickListener() { 

     @Override 
     public void onItemClick(View v , int position) { 
      //Test display 
      //Toast.makeText(mContext, mCategoryList.get(position).getId(),Toast.LENGTH_LONG).show(); 

      //Push to Activity method with clicked item ID 
      mCallBackCategory.proceedToProducts(mCategoryList.get(position).getId()); 

     } 
    }); 

    return view_fragment_category; 
}