0

I followed the Android Room with a View tutorial and I was successfully able to recreate the tutorial app. All of my code can be found in that tutorial. The only difference with my app now is my MainActivity is a webView and then when the user clicks on a button in the toolbar, they're taken to the Room with a View app functionality exactly how it is in the tutorial. So essentially I just added another activity beforehand.

I've tried using the methods getAllWords() and getAlphabetizedWords() associated with LiveData and dumped the results into the Logcat, but they always come out in a hash-like string (Ex: Word@6f2f356). I'm trying to get the exact value of the word I'm pulling from the Room database.

I have 2 issues currently:

  1. I'd like to select all of the words in the Room database table from the MainActivity.

  2. After selecting all terms from the table, I'd like to write them all to a text file or CSV.

In my MainActivity I'm using the function below to send the user to the start of the Room with a View tutorial functionality (WordList). My WordList class is the equivalent to the tutorial's MainActivity. Within this WordList class, I've tried using getAllWords() and it returns the terms in a hash-like state.

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case R.id.nix_menu_back:
            onBackPressed();
            break;
        case R.id.nix_menu_forward:
            onForwardPressed();
            break;
        case R.id.nix_menu_refresh:
            nixWebView.reload();
            break;
        case R.id.nix_string_list:
            nixWebView.reload();
            Intent intent = new Intent(this, WordList.class);
            startActivity(intent);
            break;
    }
    return super.onOptionsItemSelected(item);
}

Can someone please explain to me how to select all terms in the Room database and output them in a readable format to Logcat and an internal text file?

Any help is appreciated, and my sincerest apologies if this is a repeat! I've checked around SO and the only question I found related to my situation didn't cover this situation.

I'm happy to provide more code if asked. Thank you everyone!

Breezy
  • 74
  • 2
  • 14

1 Answers1

2

Hashy looking strings

If you want your Word object to be printed in a readable object you should override the method public String toString() for that class since the default implementation returns that hashy looking String.

Some light on this could be:

public class Word {
    // Other stuff you may have
    @Override
    public String toString() {
         return this.wordID+" - "this.wordContent;
    }
}

And this could print: "12 - Cat"

To list all words.

The methods provided in that codelab are enough to list all the words contained in the SQLite DB.

To save them in an external file as plain text.

Check this other stack overflow question

Don't forget to add <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

In your AndroidManifest.xml file!

Some random IT boy
  • 7,569
  • 2
  • 21
  • 47
  • Awesome! I'll be able to implement this in a few hours! Thank you so much, I'll let you know how it goes. – Breezy Mar 14 '19 at 17:43
  • 1
    Please do! And let me know if you need something else! – Some random IT boy Mar 14 '19 at 17:44
  • Looks like I got a chance to look at this sooner than expected! I'm a bit of an Android newbie so please excuse my ignorance lol. So is this as simple as initializing a new Word class instance in my MainActivity, calling getWord() and calling toString()? I don't understand why I can't declare a database object in my MainActivity and just query the data. – Breezy Mar 14 '19 at 18:18
  • I'm also open to sharing my simple project, if applicable. :/ – Breezy Mar 14 '19 at 18:19
  • What kind of error are you getting and what do you exactly mean? – Some random IT boy Mar 14 '19 at 18:22
  • 1
    Check this gist to see some usage of this new implementation: https://gist.github.com/NanoSpicer/4cdca5edab28b2a4e00bafb292195f09 – Some random IT boy Mar 14 '19 at 19:56
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/190051/discussion-between-some-random-it-boy-and-breezy). – Some random IT boy Mar 14 '19 at 20:05