title: Loading data from sqlite database with AsyncTask and display inside recyclerview items
body: <p>I have a Navigation menu with nav menu on it. When clicked on each nav menu, the specific fragment is opened.For example, when I click on Words nav menu, words item display with <code>recyclerView</code> items on it. I'm fetching data from offline and external SQLite database and display on <code>recyclerView</code> items. Now I want to fetch data in another thread, NOT in the main thread, because I want increase loading speed data and app performance. But I don't know how to do this. please help me with a code. I read the same subject on the internet, but I still now have my issue. </p>
  
  <p>this is my <strong>AllWordsFragment</strong></p>
  
  <pre><code>public class AllWordsFragment extends Fragment {
  
  private List&lt;WordsList&gt; wordsLists = new ArrayList&lt;&gt;();
  private Cursor cursor;
  ProgressBar progressBar;
  RecyclerView recyclerView;
  AllWordsAdapter allWordsAdapter;
  
  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater,
                           @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  
      View view = inflater.inflate(R.layout.all_words_fragment, container, false);
      progressBar = view.findViewById(R.id.progressBar);
      progressBar.setMax(600);
      allWordsAdapter = new AllWordsAdapter(getActivity(), wordsLists);
      allWordsAdapter.notifyDataSetChanged();
      recyclerView = view.findViewById(R.id.recyclerViewAllWords);
      RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
      recyclerView.setLayoutManager(layoutManager);
      recyclerView.setItemAnimator(new DefaultItemAnimator());
      recyclerView.setHasFixedSize(true);
      recyclerView.setAdapter(allWordsAdapter);
      loadingWords();
  
      return view;
  }
  
  private void loadingWords() {
  
      WordDatabase wordDatabase = new WordDatabase(getActivity());
  
      try {
  
          wordDatabase.createDatabase();
          wordDatabase.openDatabase();
  
      } catch (SQLiteException e) {
          e.printStackTrace();
      }
  
  
      try {
  
          cursor = wordDatabase.QueryData("SELECT Word, Definition, Example, WordList, ImageWord FROM Words");
  
          if (cursor != null &amp;&amp; cursor.moveToFirst()) {
  
              do {
  
                  WordsList wordList = new WordsList();
                  wordList.setWordTitle(cursor.getString(0));
                  wordList.setDefinition(cursor.getString(1));
                  wordList.setExample(cursor.getString(2));
                  wordList.setVocubList(cursor.getString(3));
                  wordList.setImageWord(cursor.getString(4));
                  wordsLists.add(wordList);
  
  
              } while (cursor.moveToNext());
  
  
              wordDatabase.close();
          }
  
  
      } catch (SQLiteException w) {
          w.printStackTrace();
      } finally {
  
          if (cursor != null) {
              cursor.close();
          }
      }
  
  
  }
  </code></pre>
  
  <p>and this is my <strong>AllWordsAdapter</strong></p>
  
  <p>public class AllWordsAdapter extends RecyclerView.Adapter {</p>
  
  <pre><code>private int lastPosition = -1;
  protected Context context;
  private List&lt;WordsList&gt; wordsListList = new ArrayList&lt;&gt;();
  
  public AllWordsAdapter(Context context, List&lt;WordsList&gt; wordsListList) {
      this.context = context;
      this.wordsListList = wordsListList;
  }
  
  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  
      View view = LayoutInflater.from(context).inflate(R.layout.all_words_item, parent, false);
  
  
      return new ViewHolder(view);
  }
  
  @Override
  public void onBindViewHolder(final ViewHolder holder, int position) {
  
      WordsList wordsList = wordsListList.get(position);
      holder.wordTitle.setText(wordsList.getWordTitle());
      holder.definitionWord.setText(Html.fromHtml(wordsList.getDefinition()));
      holder.exampleWord.setText(Html.fromHtml(wordsList.getExample()));
      holder.labelWordList.setLabelText(wordsList.getVocubList());
  
  
      //get image from assets with Glide.
      String pathImage = wordsList.getImageWord();
      String assetsPath = "file:///android_asset/";
      Glide.with(context)
              .asBitmap()
              .load(Uri.parse(assetsPath + "" + pathImage))
              .into(holder.wordImage);
      Log.d("path", assetsPath + "" + pathImage);
  
      Typeface headerFont = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Bold.ttf");
      holder.wordTitle.setTypeface(headerFont);
  
      Typeface customFont = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Italic.ttf");
      holder.exampleWord.setTypeface(customFont);
      holder.definitionWord.setTypeface(customFont);
  
      //cal animation function
      setAnimation(holder.itemView, position);
  
      holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
  
              Intent intent = new Intent(context, AllWordsDetails.class);
              intent.putExtra("word", holder.wordTitle.getText().toString());
              context.startActivity(intent);
          }
      });
  
  
  }
  
  @Override
  public int getItemCount() {
      return wordsListList.size();
  }
  
  public class ViewHolder extends RecyclerView.ViewHolder {
  
      private CircleImageView wordImage;
      private LabelTextView labelWordList;
      private TextView wordTitle, definitionWord, exampleWord;
  
      private RelativeLayout relativeLayout;
  
      public ViewHolder(View itemView) {
          super(itemView);
  
  
          wordTitle = itemView.findViewById(R.id.allWordTitle);
          wordImage = itemView.findViewById(R.id.circleHeaderImage);
          exampleWord = itemView.findViewById(R.id.exampleAllWord);
          definitionWord = itemView.findViewById(R.id.definitionAllWord);
          labelWordList = itemView.findViewById(R.id.labelWordList);
          relativeLayout = itemView.findViewById(R.id.relativeAllWords);
      }
  }
  
  private void setAnimation(View viewToAnimation, int position) {
      // If the bound view wasn't previously displayed on screen, it's animated
      if (position &gt; lastPosition) {
  
          ScaleAnimation scaleAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
                  Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
          scaleAnimation.setDuration(new Random().nextInt(501));//to make duration random number between [0,501)
          viewToAnimation.startAnimation(scaleAnimation);
          lastPosition = position;
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I know must be use <strong>AsyncTask</strong> and do this in background, but I don't know how do this ? Please help me with a code. <strong>Thanks</strong> .</p>