数据在realm数据库中插入两次

时间:2018-05-30 10:19:28

标签: android android-recyclerview realm

我正在尝试向领域插入值,如果没有互联网连接则显示到RecyclerView。如果在安装时有互联网,它将调用webservice.Afterwards每次都会从数据库显示数据。代码正在运作,但问题是数据在RecyclerView中显示重复(即两次)。我无法理解是什么问题。我的代码在下面

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    mOnFragmnetSwitch = (Config.OnFragmnetSwitch) getActivity();
    View mFragmentView = inflater.inflate(R.layout.fragment_home, container, false);
    mCategoryListRecyclerView = (ParallaxRecyclerView) mFragmentView.findViewById(R.id.recyclerView);
    mTopicsArrayList = new ArrayList<>();
    realm = Realm.getDefaultInstance();
    RealmResults<Topics> topiclist = realm.where(Topics.class).findAll();
    for (Topics s : topiclist) {
        mTopicsArrayList.add(s);
    }

    if (mTopicsArrayList.size() == 0) {
        if (isNetworkAvailable()) {
            new WebserviceCall(HomeFragment.this, AppConstants.Methods.getTopics).execute(new String[]{getSharedPreference(AppConstants.SharedKey.DEVICE_ID), getSharedPreference(AppConstants.SharedKey.USER_ID)});
        } else {
            Snackbar.make(mFragmentView, "Connet to the internet and try again!", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    }else {
        setupFunctioning();
    }
    return mFragmentView;
}

private void setupFunctioning() {

    Log.d("count",String.valueOf(count));
    mCategoryListRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    mCategoryListRecyclerView.setHasFixedSize(true);
    mCategoryListRecyclerView.setAdapter(new ParallaxRecyclerAdapter(context, HomeFragment.this, mTopicsArrayList));
}

@Override
public void onWebServiceCall(JSONObject mJsonObject, int method) {
    mTopicsArrayList = new ArrayList<>();
    try {
        JSONArray mJsonTopicArray = mJsonObject.getJSONArray(AppConstants.APIKeys.TOPICS_LIST);
        for (int i = 0; i < mJsonTopicArray.length(); i++) {
            final Topics mTopics = new Topics();
            mTopics.setId(mJsonTopicArray.getJSONObject(i).getString(AppConstants.APIKeys.ID));
            mTopics.setTitle(mJsonTopicArray.getJSONObject(i).getString(AppConstants.APIKeys.TOPICS));
            mTopics.setImage(mJsonTopicArray.getJSONObject(i).getString(AppConstants.APIKeys.TOPICS_IMAGE));
            mTopics.setDescription(mJsonTopicArray.getJSONObject(i).getString(AppConstants.APIKeys.DESCRIPTION));

            try {
                realm = Realm.getDefaultInstance();
                realm.executeTransaction(new Realm.Transaction() {
                    @Override
                    public void execute(Realm realm) {
                        realm.copyToRealm(mTopics);
                    }
                });

            } finally {
                if (realm != null) {
                    realm.close();
                }
            }
            mTopicsArrayList.add(mTopics);
        }
        count=1;
        setupFunctioning();

    } catch (JSONException e) {
        Log.d("gggggg", "enter");
        e.printStackTrace();
    } catch (Exception e) {
        Log.d("gggggg", "ff");
        e.printStackTrace();
    }
}

@Override
public void onResume() {
    super.onResume();
    mOnFragmnetSwitch.loadTitle("Home");
    HomeActivity.fragment = HomeFragment.this;
}
@Override
public void onDestroy() {
    super.onDestroy();
    realm.close();
}

任何人都可以帮我找到问题。

0 个答案:

没有答案