Use a string in another class depending on the option chosen by user

问题: I have a menu at the beginning of my application which allows the user to choose a specific type of diet, I want to save this as a string to use in another class which find...

问题:

I have a menu at the beginning of my application which allows the user to choose a specific type of diet, I want to save this as a string to use in another class which finds nearby restaurants,cafes,etc.

For example, if the user chooses the "Vegan" card, then I want to be able to use "Vegan" in other classes, but if it's "Kosher" I want to use the string "kosher" in other classes.

I tried creating a DietChoice class to set/get the diet but that doesn't work because I can't create a HomeMenu object in my MapsActivity class.

How can I make it so that when the user clicks the Vegan card, I can make it so that I can use a string "Vegan" in my MapsActivity class?

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.view.View;

public class HomeMenu extends AppCompatActivity implements View.OnClickListener {

private CardView veganMenu,halalMenu,vegeterianMenu,kosherMenu;
private DietChoice diet;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_menu);
    getSupportActionBar().setDisplayShowHomeEnabled(true);



}

public void onClick(View v) {
    Intent intent = new Intent(this,Home.class)
    switch (v.getId()) {
        case R.id.vegan_menu:
            intent.putExtra("STRING_I_NEED", "vegan");
            startActivity(intent);
            break;
        case R.id.vegetarian_menu:
            intent.putExtra("STRING_I_NEED", "vegetarian");
            startActivity(intent);
            break;
        case R.id.halal_menu:
            intent.putExtra("STRING_I_NEED", "halal");
            startActivity(intent);      
            break;
        case R.id.kosher_menu:
            intent.putExtra("STRING_I_NEED", "kosher");
            startActivity(intent);      
            break;
}

回答1:

Hello just putExtra with intent and extract it later on in your next Activity.No need of any Pojo.

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.view.View;

public class HomeMenu extends AppCompatActivity implements View.OnClickListener {

private CardView veganMenu,halalMenu,vegeterianMenu,kosherMenu;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_menu);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    veganMenu = (CardView) findViewById(R.id.vegan_menu);
    halalMenu = (CardView) findViewById(R.id.vegetarian_menu);
    vegeterianMenu = (CardView) findViewById(R.id.halal_menu);
    kosherMenu = (CardView) findViewById(R.id.kosher_menu);

    veganMenu.setOnClickListener(this);
    halalMenu.setOnClickListener(this);
    vegeterianMenu.setOnClickListener(this);
    kosherMenu.setOnClickListener(this);
}

public void onClick(View v) {
    Intent intent = new Intent(this,Home.class)
    switch (v.getId()) {
        case R.id.vegan_menu:
            intent.putExtra("STRING_I_NEED", "vegan");
            startActivity(intent);
            break;
        case R.id.vegetarian_menu:
            intent.putExtra("STRING_I_NEED", "vegetarian");
            startActivity(intent);
            break;
        case R.id.halal_menu:
            intent.putExtra("STRING_I_NEED", "halal");
            startActivity(intent);      
            break;
        case R.id.kosher_menu:
            intent.putExtra("STRING_I_NEED", "kosher");
            startActivity(intent);      
            break;
    }
}
}

To Retrive the String in Home activity use below Code

String newString;
if (savedInstanceState == null) {
    Bundle extras = getIntent().getExtras();
    if(extras == null) {
        newString= null;
    } else {
        newString= extras.getString("STRING_I_NEED");
    }
} else {
    newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
}
  • 发表于 2019-01-09 07:39
  • 阅读 ( 228 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除