0

I have two activities that are called DrinksActivity and FoodsActivity. Both have their own RecyclerViews to display items.

They work well, but I have to pass their values to another activity called.. sample_activity.

This is how sample_activity looks like.

public class sample_layout extends AppCompatActivity {

    private DrawerLayout dl;
    private ActionBarDrawerToggle t;
    private NavigationView nv;

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

        SetTexts();
        Navigation();

        TextView textView = findViewById(R.id.txtFoodName);

        Intent intent = getIntent();
        DrinksModel drinksM = intent.getParcelableExtra("drinks");
        FoodModel foodM = intent.getParcelableExtra("foods");
        
        //how can i set the textView from their own getParcelableExtra values
        String FoodName = foodM.getFoodName();
        textView.setText(FoodName);

        //how can i set the textView from their own getParcelableExtra values

        String DrinkName = drinksM.getDrinkName();
        textView.setText(DrinkName);
    }
        

    public String getName(String email) {
        String stringList = null;
        DatabaseHelperAccounts db = new DatabaseHelperAccounts(this);
        SQLiteDatabase dbb = db.getWritableDatabase();

        String selectQuery = "SELECT FullName FROM " + db.DATABASE_TABLE + " WHERE email = '" + email + "'";
        Cursor c = dbb.rawQuery(selectQuery, null);
        if (c != null) {
            c.moveToFirst();
            while (c.isAfterLast() == false) {
                String name2 = (c.getString(c.getColumnIndex("FullName")));
                stringList = name2;
                c.moveToNext();
            }
        }
        return stringList;
    }

    public void SetTexts() {
        String StringEmail = LoginFragment.EmailString;

        NavigationView nav_view = (NavigationView) findViewById(R.id.nv);//this is navigation view from my main xml where i call another xml file
        View header = nav_view.getHeaderView(0);//set View header to nav_view first element (i guess)
        TextView UserFullNameNav = (TextView) header.findViewById(R.id.UserFullNameNav);//now assign textview imeNaloga to header.id since we made View header.
        TextView UserEmailNav = (TextView) header.findViewById(R.id.UserEmailNav);

        getName(StringEmail);
        UserEmailNav.setText(StringEmail);// And now just set text to that textview
        UserFullNameNav.setText(getName(StringEmail));
    }

    public void Navigation() {
        dl = findViewById(R.id.sampleLayout);
        t = new ActionBarDrawerToggle(this, dl, R.string.open, R.string.close);
        dl.addDrawerListener(t);
        t.syncState();

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        nv = findViewById(R.id.nv);

        nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int id = item.getItemId();
                switch (id) {
                    case R.id.itemDrinks:
                        Intent DrinksIntent = new Intent(getApplicationContext(), DrinksActivity.class);
                        startActivity(DrinksIntent);
                        break;
                    case R.id.itemFoods:
                        Intent FoodsIntent = new Intent(getApplicationContext(), FoodsActivity.class);
                        startActivity(FoodsIntent);
                        break;
                    case R.id.itmLogout:
                        Intent LogOut = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity(LogOut);
                        finish();

                    default:
                        return true;
                }
                return true;
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull 'MenuItem' item) 
    {
        if (    t.onOptionsItemSelected(item))
            return true;
        return super.onOptionsItemSelected(item);
    }


}

How can I get the value of the item when it is chosen from the recyclerview of that specific activity?

J.F.
  • 13,927
  • 9
  • 27
  • 65
Danny
  • 13
  • 5
  • Share the code from where you are opening this activity. – Priyanka Nov 24 '20 at 11:52
  • this is the caller activity.. the method from the activity that is being called looks like this.. @Override public void onNoteClick(int position) { Intent intent = new Intent(this,sample_layout.class); intent.putExtra("drinks", DrinksList.get(position)); startActivity(intent); } – Danny Nov 24 '20 at 11:53
  • and from the other activity, it looks like this.. @Override public void onNoteClick(int position) { // FoodList.get(position) Intent intent = new Intent(this, sample_layout.class); intent.putExtra("food", FoodList.get(position)); startActivity(intent); }; – Danny Nov 24 '20 at 11:54
  • Can you share the whole code? I need to check the values of lists position. – Priyanka Nov 24 '20 at 11:56

2 Answers2

0

As I can see you're using database to store data, right? If so, the best (and the most correct) way is to pass some identifier between activity (like "id") and then get value by that id from database where you need. But, if you really want to pass the whole object then you need to use serializable or parcelable (parcelable is better choice since it's from Android SDK). Then in the activity where you're getting the value just parse it.

EDIT: To get parcelable value call:

Food food = getIntent().getExtras().getParcelable("food");

Then just call:

textView.setText(food.getYourField);
Alex.Marynovskyi
  • 1,264
  • 3
  • 16
  • 22
0

Try to use this

 if (getIntent()!=null ){
    if (getIntent.hasExtra("drinks")){ 
       DrinksModel drinksM = intent.getParcelableExtra("drinks");
       String DrinkName = drinksM.getDrinkName();
       textView.setText(DrinkName);
    }else if(getIntent.hasExtra("foods")){
       FoodModel foodM = intent.getParcelableExtra("foods");
       String FoodName = foodM.getFoodName();
       textView.setText(FoodName);
  }
}
Priyanka
  • 1,791
  • 1
  • 7
  • 12