Issue
My app was very laggy, so I decided to use an AsyncTask to do the heaviest operations inside it and so, the app wouldn't be so slow at changing tabs.
But now, it is behaving in a very weird way. Let me explain: I have a ViewPager2, and inside that ViewPager, I have a recyclerview.
I put an AsyncTask inside the ViewPager, because it is the heaviest operation done in the fragment, and in the adapter of that ViewPager, I retrieve some values from a Database via a class called DatabaseHelper which one that extends SQLiteOpenHelper and has this method.
public Cursor getAllTasksByList(int ListID)
{
SQLiteDatabase db = this.getWritableDatabase();
Cursor c = db.rawQuery("SELECT * FROM " + Db.Tables.Tasktable.TASKS_TABLE + " WHERE " + Db.Tables.Tasktable.COL_LIST_ID + " = " + ListID, null);
return c;
}
Because the DatabaseHelper only returns one Cursor, I use another class to keep the code organized, this class takes the Cursor as argument and returns a list of "ListItem". This class is called "FolderUtils" and contains the following method (which one that I use to populate my RecyclerView inside that is inside my ViewPager):
public ArrayList<TaskItem> getTasksByList(int ListID, Context context) {
ArrayList<TaskItem> tasks = new ArrayList<>();
DatabaseHelper d = new DatabaseHelper(context);
Cursor c = d.getAllTasksByList(ListID);
while (c.moveToNext()) {
int id = c.getInt(0);
int listid = c.getInt(1);
boolean checked = c.getInt(2) > 0;
String title = c.getString(3);
tasks.add(new TaskItem(id, listid, checked, title));
}
return tasks;
}
But here it is the problem, sometimes this List is empty, but another times, it just retrieves the first value of the that Table I look for, strangely, sometimes it returns wrong values and it only works sometimes if I move my ViewPager to another position or if I just put some breakpoints. Here is my Adapter code.
@Override
public void onBindViewHolder(@NonNull ListHolder holder, int position) {
new LoadData(mList.get(position), holder).execute();
}
@Override
public int getItemCount() {
return mList.size();
}
private class LoadData extends AsyncTask<Void, Void, Void> {
private ListItem item;
private ListHolder holder;
public LoadData(ListItem item, ListHolder holder) {
this.item = item;
this.holder = holder;
}
@Override
protected void onPreExecute(){
super.onPreExecute();
//I set the visibility to GONE so that the user can just see the final layout and not the layout "Building" itself.
holder.itemView.setVisibility(View.GONE);
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
setItems(item, holder); //setItems is for setting the UI Content.
AttachRecycler(holder, item); //AttachRecycler creates an adapter for the recyclerview with the TaskList values, and attaches it to the recyclerview inside the ViewPager item.
holder.itemView.setVisibility(View.VISIBLE); //Shows the finished item
}
@Override
protected Void doInBackground(Void... voids) {
SetList(item); //SetList is where it takes the values from database and adds it to the list.
return null;
}
}
private void SetList(ListItem item) {
TaskList = new ArrayList<>();
else if (Mode == 1)
{
//Mode by default is 1. The line below does gets executed, however, it returns the wrong values.
TaskList.addAll(FolderUtils.getInstance().getTasksByList(item.getID(), context));
}
private void AttachRecycler(ListHolder holder, ListItem item)
{
LinearLayoutManager manager = new LinearLayoutManager(context);
holder.recycler.setLayoutManager(manager);
adapter = new TaskAdapter(TaskList, item.getColor(), context, item.getID());
holder.recycler.setAdapter(adapter);
}
How could I fix this? Thank You.
Solution
Solved this by myself. Solution was to make TaskList a private variable inside the LoadData class, not a private variable of the entire Adapter, this acts like a local variable for every item instance, removing the duplicates in some items.
Answered By - RushedPotato
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.