Search code examples
androidandroid-room

How to use data from returned in a List from a ROOM query?


I'm trying to pull a few values from my ROOM database to show to the user - they are all coming from one row, and the user picks the row by inputing a number that corresponds to the value set as the primary key. I know that everything is working up until this point because I am able to query the data without throwing an out of range error (which it does if I put an invalid number in).

Here is my DAO:

'''

@Query("SELECT * FROM pitData")
List<PitData> getAllScores();




@Insert(onConflict = OnConflictStrategy.IGNORE)
void insertAll(PitData pitData);


@Query("DELETE FROM PitData")
void nukeTable();

@Query("Select * FROM pitData WHERE teamNum = :idInt")
List<PitData> findTeam(int idInt);

'''

And here is the class where I am trying to get the return (findTeam) from:

'''

EditText teamnum;
Button search;
TextView shower;

String id;
int idInt;

private List<PitData> pit;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pit_view);

    teamnum = findViewById(R.id.textView15);
    search = findViewById(R.id.button6);
    shower = findViewById(R.id.shower);

    search.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            id = teamnum.getText().toString();
            idInt = Integer.parseInt(id);
            Log.d("dataPull", "Ran searchData");
            searchData();

        }


    });

}

public void searchData(){

    ScoreDataBase SDB = ScoreDataBase.getDatabase(this);

    PitData pitData = new PitData("1", "two", "three");

    SDB.pitDao().insertAll(pitData);




    Log.d("PitView","Returned List");
    pit = SDB.pitDao().findTeam(idInt);

        //Log.d("pitdata", pit.getClass().getName());

        Log.d("pitviewdata", String.valueOf(pit.get(0))); // this is where I am trying to pull a specific column from the row that I queried into the list 'pit'

'''

However, when I try to get data from the list, it says that there is only one item in the list (item 0). When I take that value and print it in the log and/or the textview, it returns a string of the class path to my Entity with @some random string tacked onto the end. How do I get it to put the values from the entity into the list as opposed to the classpath of the entity?


Solution

  •  pit = null;
     for(PitData p: SDB.pitDao().findTeam(idInt) {
         pit = p;
         break; // Only expect/want the 1 PtData
     }
     if (pit != null) {
         Log.d("pitviewdata",pit.the_appropriate_member_variable_or_getter_method);
     } else {
         Log.d("pitviewdata","NOT FOUND");
     }
    

    Or, if you instead use (as you know you can only retrieve 1 PitData) :-

    @Query("Select * FROM pitData WHERE teamNum = :idInt")
    PitData findTeam(int idInt);
    
    pit = SDB.pitDao().findTeam(idInt);
    // Test here for no data extracted (depends upon the PitData class definition)
    Log.d("pitviewdata",pit.the_appropriate_member_variable_or_getter_method);
    

    That is instead of using the default toString method you need to get the appropriate member variable value(s).

    my Entity with @some random string

    That is the address/pointer of the Object i.e. what the default toString method outputs (you could alternately override the toString method BUT this may prove to complicate subsequent use of the Object)