使用getparcelableextra()从不同的活动中获取价值

mcvgt66p  于 2021-07-08  发布在  Java
关注(0)|答案(2)|浏览(355)

我有两个活动叫做 DrinksActivity 以及 FoodsActivity . 两个都有自己的 RecyclerViews 显示项目。
它们运行良好,但我必须将它们的值传递给另一个名为。。 sample_activity .
这是怎么回事 sample_activity 看起来像。

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);
    }

}

当从特定活动的recyclerview中选择项目时,如何获取项目的值?

ntjbwcob

ntjbwcob1#

我可以看到你在用数据库存储数据,对吗?如果是这样,最好的(也是最正确的)方法是在活动之间传递一些标识符(如“id”),然后从需要的数据库中通过该id获取值。但是,如果您真的想传递整个对象,那么您需要使用serializable或parcelable(parcelable是更好的选择,因为它来自androidsdk)。然后在获取值的活动中,只需解析它。
编辑:要获取parcelable值,请调用:

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

那就打电话:

textView.setText(food.getYourField);
x8goxv8g

x8goxv8g2#

试着用这个

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);
  }
}

相关问题