I'm learning Android MVP architecture given here. Based on the example I created my own simple app that just displays list with recyclerview
with pull to refresh functionality.
When list is pulled I want first to clear the recyclerview and then reload the data again from the repository (with fake latency delay). But, when I clear the data inside RecyclerViewAdpater, all data inside repository are also being cleared and there is nothing to show. I just can't understand the reason.
Here's my activity that creates both Presenter and View (that is Fragment):
protected void onCreate(Bundle savedInstanceState) {
mainFragment = MainFragment.newInstance();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.contentFrame, mainFragment);
transaction.commit();
new MainPresenter(new MainDataRepository(), mainFragment);
}
Here's my Fragment (MainFragment) - ATTENTION: updateList(null) - This is where it clears all data, including inside Repository:
public MainFragment() {
}
public static MainFragment newInstance() {
return new MainFragment();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//....
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
recyclerAdapter.updateList(null); // **This is where it clears all data, including inside Repository**
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mPresenter.loadList(); // to reload the list
}
}, 1500);
}
});
//...
}
@Override
public void onResume() {
super.onResume();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mPresenter.loadList();
}
}, 3000);
}
@Override
public void setPresenter(MainContractor.Presenter presenter) {
mPresenter = presenter;
}
@Override
public void showList(List<String> mainDataList) {
if(recyclerAdapter == null) {
recyclerAdapter = new RecyclerAdapter(mainDataList);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(recyclerAdapter);
}else{
recyclerAdapter.updateList(mainDataList);
}
}
Here's my presenter (MainPresenter):
public MainPresenter(MainDataRepository mainDataRepository, MainContractor.View view){
mMainDataRepository = mainDataRepository;
mMainContractorView = view;
view.setPresenter(this);
}
@Override
public void loadList() {
ArrayList<String> strings = mMainDataRepository.getList();
mMainContractorView.showList(strings);
}
Here's my repository (MainDataRepository):
private ArrayList<String> repositoryList;
public MainDataRepository() {
createList();
}
private void createList() {
if(repositoryList == null){
repositoryList = new ArrayList<>();
}
for (int i = 1; i <= 10; i++) {
repositoryList.add("Item " + i);
}
}
public ArrayList<String> getList(){
return repositoryList;
}
And the last one, This is how I am updating recyclerview inside RecyclerAdapter:
public void updateList(List<String> newStrings){
if(newStrings != null){
stringsList = newStrings;
}else{
stringsList.clear();
}
notifyDataSetChanged();
}
Why updateList mehod inside RecyclerAdapter also clears the data inside repository that is ArrayList<String> repositoryList
?
Aucun commentaire:
Enregistrer un commentaire