如何在主要活动中单击按钮时基于所选微调框选项的文本在另一个活动中设置标题

时间:2016-12-25 20:14:03

标签: java android button android-intent spinner

我打算创建一个包含3个活动的Android应用程序项目。

第一个活动的布局包含3个元素,2个微调器和一个按钮。

我的目的是让他们以下列方式工作:

MainActivity.java:  1.我们只是说我希望第一个微调器在app启动时加载一组类别。例如,收入,支出。  2.从第一个微调器中选择一个类别后,第二个微调器将在第二个微调器中加载其各自的子类别。例如,如果我在第一个微调器中选择收入,第二个微调器加载佣金,工资,奖金等,如果选择支出,则每月支出,运输,票据等装入第二个微调器。  3.现在,根据我选择的子类别,当我单击按钮时,它将打开一个新活动并为该特定类别和子类别设置操作栏的标题。例如,如果我选择收入和工资,下一个活动将在点击主要活动中的按钮后显示收入,薪水作为标题。

我创建了一个名为Utils.java的类,用于将类别和子类别名称的集合存储为字符串数组。

public class Utils{

    public static String[] mainCategoryList =
    {
        "Choose One",
        "Income",
        "Expenditure"
    };

    //
    public static String[] chooseOne_list=
    {
        "Choose One"
    };

    public static String[] income_subCategoryList =
    {
        "Bonus",
        "Commision",
        "Salary"                 
    };

    public static String[] expenditure_subCategoryList=
    {
         "Bills",
         "Conveyance",
         "Monthly Expenditure"
    };

    public static String[] income=
    {
        "Income, Bonus",
        "Income, Commission",
        "Income, Salary"
    };

    public static String[] expenditure=
    {
        "Expenditure, Bills",
        "Expenditure, Conveyance",
        "Expenditure, Monthly Expenditure"
    };
}

我为MainActivity.java做的编码如下,

public class MainActivity extends AppCompatActivity {

Spinner spinner1_mainCategory, spinner2_subCategory;
Button buttonChoose;
String spinner1Item, spinner2Item;
Intent intent;


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

    //Spinner element
    spinner1_mainCategory = (Spinner)findViewById(R.id.spinner1_mainCategory);
    spinner2_subCategory = (Spinner)findViewById(R.id.spinner2_subCategory);

    //Button element
    buttonChoose = (Button)findViewById(R.id.buttonChoose);

    //To keep onCreate() clean of multiple adapter setting
    adapterMethod(spinner1_mainCategory,mainCategoryList);

    // Spinner click listener
    spinner1_mainCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
            //String.valueOf(array_name.getSelectedItem()) captures the value of the selected option of a specific string array onClick event
            spinner1Item = String.valueOf(spinner1_mainCategory.getSelectedItem());


            switch (position){
                case 0:
                    adapterMethod(spinner2_subCategory,chooseOne_list);
                    spinner2_subCategory.setVisibility(View.GONE);
                    //Button click listener
                    buttonChoose.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {

                            if (spinner1Item.contentEquals(Utils.mainCategoryList[0])){
                                Toast toast = Toast.makeText(getApplicationContext(), "Choose a Category First", Toast.LENGTH_SHORT);
                                toast.setGravity(Gravity.CENTER|Gravity.CENTER_HORIZONTAL,0,0);
                                toast.show();
                            }
                        }
                    });
                    break;
                case 1:
                    adapterMethod(spinner2_subCategory,income_subCategoryList);
                    spinner2_subCategory.setVisibility(View.VISIBLE);
                    break;
                case 2:
                    adapterMethod(spinner2_subCategory,expenditure_subCategoryList);
                    spinner2_subCategory.setVisibility(View.VISIBLE);
                    break;
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });


    spinner2_subCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, final int position, long id) {
            spinner2Item = String.valueOf(spinner2_subCategory.getSelectedItem());

            switch (position){
                case 0:
                    //Income Category, Bonus Sub-category
                    if (spinner2Item.contentEquals(Utils.mainCategoryList[1]) && spinner2Item.contentEquals(income_subCategoryList[0])){
                        buttonChoose.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                changeTitleInNextActivity(Utils.income[0]);
                            }
                        });
                    }
                    //Expenditure Category, Bills Sub-category
                    if (spinner2Item.contentEquals(Utils.mainCategoryList[2]) && spinner2Item.contentEquals(expenditure_subCategoryList[0])){
                        buttonChoose.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                changeTitleInNextActivity(Utils.expenditure[0]);
                            }
                        });
                    }

                    break;
                case 1:
                    //Income Category, Commission Sub-category
                    if (spinner2Item.contentEquals(Utils.mainCategoryList[1]) && spinner2Item.contentEquals(income_subCategoryList[1])){
                        buttonChoose.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                changeTitleInNextActivity(Utils.income[1]);
                            }
                        });
                    }
                    //Expenditure Category, Conveyance Sub-category
                    if (spinner2Item.contentEquals(Utils.mainCategoryList[2]) && spinner2Item.contentEquals(expenditure_subCategoryList[1])){
                    buttonChoose.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            changeTitleInNextActivity(Utils.expenditure[1]);
                        }
                    });
                }

                break;
                case 2:
                    //Income Category, Salary Sub-category
                    if (spinner2Item.contentEquals(Utils.mainCategoryList[1]) && spinner2Item.contentEquals(income_subCategoryList[2])){
                        buttonChoose.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                changeTitleInNextActivity(Utils.income[2]);
                            }
                        });
                    }
                    //Expenditure Category, Monthly Expenditure Sub-category
                    if (spinner2Item.contentEquals(Utils.mainCategoryList[2]) && spinner2Item.contentEquals(expenditure_subCategoryList[2])){
                        buttonChoose.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                changeTitleInNextActivity(Utils.expenditure[2]);
                            }
                        });
                    }
                    break;
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });
}

public void adapterMethod(View v, String[] list){
    // Creating adapter for spinner
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(this,
            android.R.layout.simple_spinner_item, list);
    // Drop down layout style - list view with radio button
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    dataAdapter.notifyDataSetChanged();
    // attaching adapter to spinner for district
    if (v.getId()==R.id.spinner1_mainCategory){
        spinner1_mainCategory.setAdapter(dataAdapter);
        spinner1_mainCategory.setSelection(0, false);
    }
    if (v.getId()==R.id.spinner2_subCategory){
        spinner2_subCategory.setAdapter(dataAdapter);
        spinner2_subCategory.setSelection(0, false);
    }
}

public void changeTitleInNextActivity(String title){

    intent = new Intent(getApplicationContext(),SecondActivity.class);
    intent.putExtra("Title", title);
    startActivity(intent);

}

}

SecondActivity.java

public class SecondActivity extends AppCompatActivity {

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

    Intent intent = getIntent();
    String title = intent.getStringExtra("Title");
    getSupportActionBar().setTitle(title);
}

}

我不明白为什么,但我不能按照我想要的方式让编码为我工作。从第一个和第二个微调器中选择的效果非常好,但无论我尝试多少种方法,我都无法在下一个活动中设置标题。

告诉我,伙计们,我做错了什么?如何更好地编码以避免这种陷阱。

无法制作按钮&#34;选择&#34;:

  1. 转到SecondActivity

1 个答案:

答案 0 :(得分:0)

在你的第二次活动中, 得到你传递的字符串

String title=getIntent().getStringExtra("Title");

然后设置你的标题

getSupportActionBar().setTitle(title);