在AsyncTask上尝试使用SQLite检索数据的怪异行为

问题描述 投票:1回答:1

我的应用程序非常笨拙,因此我决定使用AsyncTask在其中执行最繁重的操作,因此,该应用程序在更改选项卡时不会很慢。但是现在,它的行为很奇怪。让我解释一下:我有一个ViewPager2,在该ViewPager中,我有一个recyclerview。我将AsyncTask放在ViewPager中,因为这是片段中最繁重的操作,并且在该ViewPager的适配器中,我通过一个名为DatabaseHelper的类从Database检索了一些值,该类扩展了SQLiteOpenHelper并具有此方法。 >

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;
    }

因为DatabaseHelper仅返回一个Cursor,所以我使用了另一个类来保持代码的井井有条,该类将Cursor作为参数并返回“ ListItem”的列表。此类称为“ FolderUtils”,包含以下方法(我用来在ViewPager内部填充RecyclerView的方法):

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;
    }

但是这里是问题所在,有时此List为空,但有时,它只是检索我要查找的Table的第一个值,奇怪的是,有时返回错误的值,并且仅当我移动ViewPager时才有效移到另一个位置,或者如果我只是放置一些断点。这是我的适配器代码。

@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);

    }

我该如何解决?谢谢。

我的应用程序非常笨拙,因此我决定使用AsyncTask在其中执行最繁重的操作,因此,该应用程序在更改选项卡时不会很慢。但是现在,它的行为很奇怪。让...

android multithreading sqlite android-asynctask
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.