What I am trying to do:
In a fragment, first I load some data from server to SQLite
and change a SharedPreference
in onPostExecute
, after copying it to SQLite
, of AsyncTask
. Then I have defined a OnSharedPreferenceChangeListener
in the onCreateView
of the fragment. In that I update a RecyclerViewAdapter
by loading data from SQLite
and calling mAdapter.notifyDatasetChanged
.
What works:
According to my tests, the data is completely getting loaded from server to SQLite. The adapter is updating properly the second time.
The problem:
On calling notifyDatasetChanged
my RecyclerView
should update completely but it updates partially. According to a test I concluded that the data is not getting loaded properly from SQLite
for the first time. But surprisingly it works properly the next time I refresh. What do you think the problem is?
CODES:
onCreate:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new EventsRecyclerViewAdapter(
getFragmentManager(), getArguments().getInt(Tags.ARG_SECTION_NUMBER));
if(PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean(Tags.GET_ALL_EVENTS, true)) {
new MyAsyncTask(getActivity()).execute(Tags.AT_EVENT_ALL);
}
}
onCreateView:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
MainActivity mainActivity = (MainActivity)getActivity();
int section_number = getArguments().getInt(Tags.ARG_SECTION_NUMBER);
mainActivity.onSectionAttached(section_number);
mainActivity.updateNavigationDrawer(section_number);
View v = inflater.inflate(R.layout.fragment_recycler_view, container, false);
v.setAnimation(AnimationUtils.loadAnimation(
v.getContext(), R.anim.abc_grow_fade_in_from_bottom));
final Context context = v.getContext();
RecyclerView mRecyclerView = (RecyclerView)v.findViewById(R.id.my_recycler_view);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(v.getContext());
mRecyclerView.setLayoutManager(mLayoutManager);
load(context);
mRecyclerView.setAdapter(mAdapter);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(v.getContext());
final SharedPreferences.OnSharedPreferenceChangeListener listener =
new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if(!prefs.getBoolean(Tags.GET_ALL_EVENTS, true)) {
load(context);
}
}
};
prefs.registerOnSharedPreferenceChangeListener(listener);
mSwipeRefreshLayout = (SwipeRefreshLayout)v.findViewById(R.id.swipe_refresh_layout);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
if(PreferenceManager.getDefaultSharedPreferences(context).getBoolean(Tags.GET_ALL_EVENTS, true)) {
new MyAsyncTask(context, mSwipeRefreshLayout, listener).execute(Tags.AT_EVENT_ALL);
}
else {
load(context);
mAdapter.notifyDataSetChanged();
mSwipeRefreshLayout.setRefreshing(false);
}
}
});
return v;
}
load():
public void load(final Context context) {
MySQLiteHelper mySQLiteHelper = new MySQLiteHelper(context);
SQLiteDatabase sqLiteDatabase = mySQLiteHelper.getReadableDatabase();
String[] projection = {
MySQLiteContract.EventTable.NEW,
MySQLiteContract.EventTable.COLUMN_NAME,
MySQLiteContract.EventTable.COLUMN_BRIEF,
MySQLiteContract.EventTable.COLUMN_DATE
};
String sortOrder = MySQLiteContract.EventTable.EVENTS_ID + " DESC";
Cursor cursor = sqLiteDatabase.query(
MySQLiteContract.EventTable.TABLE_NAME, // The table to query
projection, // The columns to return
null, // The columns for the WHERE clause
null, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // don't order the data
);
int count = cursor.getCount();
int[] eventNew = new int[count];
String[] eventName = new String[count];
String[] eventBrief = new String[count];
String[] eventDate = new String[count];
if (cursor.moveToFirst()) {
for (int i = 0 ; !cursor.isAfterLast() ; i++) {
eventNew[i] = cursor.getInt(cursor.getColumnIndex(
MySQLiteContract.EventTable.NEW));
eventName[i] = cursor.getString(cursor.getColumnIndex(
MySQLiteContract.EventTable.COLUMN_NAME));
eventBrief[i] = cursor.getString(cursor.getColumnIndex(
MySQLiteContract.EventTable.COLUMN_BRIEF));
eventDate[i] = cursor.getString(cursor.getColumnIndex(
MySQLiteContract.EventTable.COLUMN_DATE));
cursor.moveToNext();
}
}
cursor.close();
sqLiteDatabase.close();
mAdapter.setEventNew(eventNew);
mAdapter.setEventName(eventName);
mAdapter.setEventBrief(eventBrief);
mAdapter.setEventDate(eventDate);
mAdapter.notifyDataSetChanged();
}
Aucun commentaire:
Enregistrer un commentaire