LordGash
LordGash

Reputation: 295

Room: Retrieving the user info by using ID

I want to have the info of a member passed to the second activity.

This is the code in the first activity.

public void onMemberClick(int position) {
        Member member = mMember.get(position);

    Intent intent = new Intent(getApplicationContext(),MemberInfo.class);
    intent.putExtra("MemberID", member.getId());
    MemberInfo.open(this, member.getId());

This is the code in the second activity.

public static void open(Activity activity, long memberid) {
    Intent intent = new Intent(activity, MemberInfo.class);
    intent.putExtra("MemberID", memberid);
    activity.startActivity(intent);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_memberinfo);

    Intent intent = getIntent();
    if (intent != null && intent.hasExtra("MemberID")) {
        long memberid = intent.getLongExtra("MemberID", -1);
        // TODO: get customer details based on customer id
        TextView firstname = findViewById(R.id.layout_memberfirstname);
        TextView surname = findViewById(R.id.layout_membersurname);
        TextView balance = findViewById(R.id.layout_memberbalance);
    }
    else {
        Toast.makeText(
                getApplicationContext(),
                R.string.empty_not_saved,
                Toast.LENGTH_LONG).show();
    }

So in the first activity, I got a list with members. I click on a member and I want to have the ID of the member passed through the open method. The ID should be passed to the second activity.

A member has a first name, surname and balance. I want to get those details shown in the Textviews. How can I get those information by using the ID of that member?

Upvotes: 0

Views: 3024

Answers (4)

LordGash
LordGash

Reputation: 295

I had to create a new method in my DAO. This query did the trick: SELECT * FROM member_table WHERE MemberID=:id

This query then should return a object of the class you try to get, in my case: Member getInfo(long id);

Upvotes: 0

Naveen T P
Naveen T P

Reputation: 7285

Try this.

Java:

@Query("select * from user where id= :id")
User getUserById(Long id);

Kotlin:

@Query("select * from user where id= :id")
fun getUserById(id: Long) : User

Hope this helps

Upvotes: 3

user4571931
user4571931

Reputation:

make query like below into dao interface in room db..

    @Query("SELECT * FROM TableName WHERE id=:id")
    User getUserData(long id);

create app level activity..

public class AppActivity extends Application {

static AppDatabase db;

@Override
public void onCreate() {
    super.onCreate();
    db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "database-name").build();
}

public static AppDatabase getDatabase() {
    return db;
}

}

this activity define into android manifest file in application tag..

     android:name=".db.AppActivity" // this line add into application tag.

after that define db version and other things..

@Database(entities = {MyTable.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
public abstract MyTableDao getTableDao();
}

after that in second activity getting member id then perform above query like below..

 Member member=AppActivity.getDatabase().getTableDao().getData(memberId);

make sure table has data and member id not null.

after you want show all details without factching data then pass all data into intent and get data using intent.

Upvotes: 0

sumit sonawane
sumit sonawane

Reputation: 1555

Query to get member list

@Query("SELECT firstname, surname FROM Member WHERE user IN (:users)")
public List<Member> Memberlist(List<String> members);

Upvotes: 1

Related Questions