Skip to content
Advertisement

Android get ArrayList from Room Database in adapter class

I have a Room Database table with multiple columns (PartsTable). I need to fetch only one column from the table that contains one word String and I’m using a subset of a table as per google docs (PartsTuple).

Now I need to create a function that will or something else that will return the ArrayList of fetched data, which I can access in my adapter class. I am able to return the data and see it in a console log (from the main fragment where I get data from ViewModel), but I just can’t seem to make it work on a function that will return the said list of data which I could then access from a different class.

Code from DAO:

@Query("SELECT keyword FROM partsTable")
LiveData<List<PartsTuple>> getTuple();

Code from repo:

public LiveData<List<PartsTuple>> getPartsTuple() {
return partsKeyword;
}

Code from view model:

public LiveData<List<PartsTuple>> getPartsTuple() {
    return partsKeyword;
}

Fragment class where I display data in a log:

mViewModel.getPartsTuple().observe(getViewLifecycleOwner(), new Observer<List<PartsTuple>>() {
        @Override
        public void onChanged(List<PartsTuple> partTuple) {
            Log.d(TAG, "vraceno: " + partTuple.toString());
        }
    });

, and data from the log

D/PartsFragment: vraceno: [part1, parts3, part_2]

Code from adapter class where I compare strings and highlight them.

ArrayTEST arrayTEST = new ArrayTEST();
        ArrayList<String> values = arrayTEST.getWordFromHardcodedList();

        String text = note.getPartsSubtitle();
        Spannable textSpannable = new SpannableString(text);

        for (int j = 0; j < values.size(); j++) {
            //word of list
            String word = String.valueOf(values.get(j));
            //find index of words
            for (int i = -1; (i = text.indexOf(word, i + 1)) != -1; i++) {
                //find the length of word for set color
                int last = i + word.length();
                
                textSpannable.setSpan(new BackgroundColorSpan(Color.parseColor("#1a0cab8f")),
                        i, last, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                textSpannable.setSpan(new ForegroundColorSpan(Color.RED),
                        i, last, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        if (note.getPartsSubtitle().trim().isEmpty()) {
            tvTEXT.setVisibility(View.GONE);
        } else {
            tvTEXT.setText(textSpannable);
        }

The part that I’m having trouble with is this, where I need to get a list of data from database and not hardCoded like this

arrayTEST.getWordFromHardcodedList();

Now I need to access this list of data from my adapter class since if there is a match I wanna highlight the parts from the list of parts in my main recycler view where all the data is shown. I can do this when I type the list manually but it needs to be dynamic based on user input.

Thanks in advance

Answer

Ended up using shared preferences with Gson.

In app gradle add

implementation 'com.google.code.gson:gson:2.8.6'

Save the data to SP in a fragment:

SharedPreferences sharedPreferences = requireActivity().getSharedPreferences("shared_preferences", MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();

            Gson gson = new Gson();
            String json = gson.toJson(myListOfData);
            editor.putString("partsKEY", json);
            editor.apply();

Load the array in the adapter class:

SharedPreferences sharedPreferences = context.getSharedPreferences("shared_preferences", MODE_PRIVATE);
        Gson gson = new Gson();
        String json = sharedPreferences.getString("partsKEY", null);
        Type type = new TypeToken<ArrayList<NoteTupleTest>>() {
        }.getType();
        partsArrayList= gson.fromJson(json, type);
        if (partsArrayList== null) {
            partsArrayList= new ArrayList<>();
        }
Advertisement