如何在片段中创建微调器?

时间:2017-07-17 22:27:18

标签: android fragment spinner

我正在尝试在片段中创建一个微调器但是在创建微调器对话框时,它无法使用“.this”识别片段java类。我不确定我应该做什么,任何帮助将不胜感激。 (对话框在OnCreate方法中定义)

ArrayList<String> ingredients = new ArrayList<>();
SpinnerDialog spinnerDialog;
Button add;

spinnerDialog = new SpinnerDialog(Add.this, ingredients, "Select An Ingredient");
    spinnerDialog.bindOnSpinerListener(new OnSpinerItemClick() {
        @Override
        public void onClick(String Ingredient, int i) {

            Toast.makeText(Add.super.getContext(), "Selected ", Toast.LENGTH_SHORT).show();

        }
    });

    add = (Button) getView().findViewById(R.id.add);
    add.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){

            spinnerDialog.showSpinerDialog();

        }

    });

2 个答案:

答案 0 :(得分:0)

Add.this替换为getActivity()

很难确切地说出问题究竟是什么(因为我不知道SpinnerDialog是如何实现的),但建构者的第一次机会非常好参数是ContextFragment未实现Context,但Activity不会。因此,不要传入Fragment个实例,而是传递其包含的Activity

答案 1 :(得分:0)

#-- Sample Master Config file, which can be generated using GIT rest api - against a user's org to find all user org repositories (in my case) looks like: ## github coolrepo-org org/repogroup contains: ##----------- github.mycompany.com:coolrepo-org:git@github.mycompany.com:coolrepo-org/somerepo1.git github.mycompany.com:coolrepo-org:git@github.mycompany.com:coolrepo-org/somerepo2.git ## somegit-instance pipeline org/repogroup contains: ##----------- somegit-instance.mycompany.com:pipeline:git@somegit-instance.mycompany.com:pipeline/shinynew-cool-pipeline.git ## !!!!! NO ORG ACCESS REPO ENTRIES BELOW !!!!! ## ## ----------------------------------------------- ## somegit-instance Misc no access org but access at just repo level enteries contains: ##----------- (appended to the master file at the end of master file generation script) --------- somegit-instance.mycompany.com:someorg-org:git@somegit-instance.mycompany.com:someorg-org/somerepofooter.git somegit-instance.mycompany.com:someorg-org:git@somegit-instance.mycompany.com:someorg-org/somereponav.git 是上下文参数。

如果Add.this是片段,请在传递上下文参数时使用Add.class

但如果getActivity()是一项活动,则为Add.class