androidstudio中依赖java的微调器

hyrbngr7  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(305)

我在android工作室使用depandent微调器。它们工作得很好,子微调器显示的数据与父微调器的选定值相对应,但当我将微调器文本传递到firebase实时数据库时,它仅传递childcat(子微调器)的第一个索引的文本,即使选择了其他项。主cat(父微调器)的文本在firebase数据库中传递良好。代码如下:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);

        emailtext = findViewById(R.id.emailtext);
        // category = findViewById(R.id.cat);
        fname = findViewById(R.id.fullnametext);
        organization = findViewById(R.id.org);
        maincat = findViewById(R.id.categories);
        childcat = findViewById(R.id.categoryitem);
        signup = findViewById(R.id.btn_signup);

        ArrayAdapter adp1=ArrayAdapter.createFromResource(this, R.array.Categories,R.layout.support_simple_spinner_dropdown_item);
        adp1.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
        maincat.setAdapter(adp1);
       maincat.setOnItemSelectedListener(this);

        signup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final String email = emailtext.getText().toString();
                //  final String fcategory = category.getText().toString();
                final String name = fname.getText().toString();
                //final String con_pass = organization.getText().toString();
                final String cat=category;
                final String subcat=specific;
                Map<String, Object> Users = new HashMap<>();
                Users.put("Name", name);
                Users.put("Email", email);
                Users.put("Category", cat);
                Users.put("SubCategory", subcat);

                FirebaseDatabase.getInstance().getReference().child("Users").push().setValue(Users).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        Log.i("vvv", "oncomplete");
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.i("nnn", "OnFailure:" + e.toString());
                    }
                });

            }
        });

        ;

    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        category=maincat.getSelectedItem().toString();
        if(i==0){
            ArrayAdapter adp2 = ArrayAdapter.createFromResource(this, R.array.Natural_Disasters, R.layout.support_simple_spinner_dropdown_item);
            childcat.setAdapter(adp2);
        }
        if(i==1){
            ArrayAdapter adp2 = ArrayAdapter.createFromResource(this, R.array.Medical, R.layout.support_simple_spinner_dropdown_item);
            childcat.setAdapter(adp2);
        }
        if(i==2){
            ArrayAdapter adp2 = ArrayAdapter.createFromResource(this, R.array.Events, R.layout.support_simple_spinner_dropdown_item);
            childcat.setAdapter(adp2);
        }
        if(i==3){
            ArrayAdapter adp2 = ArrayAdapter.createFromResource(this, R.array.Career_supporting, R.layout.support_simple_spinner_dropdown_item);
            childcat.setAdapter(adp2);
        }
        if(i==4){
            ArrayAdapter adp2 = ArrayAdapter.createFromResource(this, R.array.SocialIssues, R.layout.support_simple_spinner_dropdown_item);
            childcat.setAdapter(adp2);
        }
        specific=childcat.getSelectedItem().toString();
    }

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

    }
}
g6baxovj

g6baxovj1#

在创建时添加这3行

ProgressDialog mDialog = new ProgressDialog(getApplicationContext());
mDialog.setMessage("Please wait...");
mDialog.setCancelable(false);

在注册函数中添加两行

mDialog.show(); //to show activity indicator
mDialog. dismiss(); //to hide activity indicator

下面是您的代码示例

signup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final String email = emailtext.getText().toString();
                //  final String fcategory = category.getText().toString();
                final String name = fname.getText().toString();
                //final String con_pass = organization.getText().toString();
                final String cat=category;
                final String subcat=specific;
                Map<String, Object> Users = new HashMap<>();
                Users.put("Name", name);
                Users.put("Email", email);
                Users.put("Category", cat);
                Users.put("SubCategory", subcat);

                mDialog.show();//to show activity indicator

FirebaseDatabase.getInstance().getReference().child("Users").push().setValue(Users).addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            mDialog. dismiss();//to hide activity indicator
                            Log.i("vvv", "oncomplete");
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Log.i("nnn", "OnFailure:" + e.toString());
                        }
                    });

                }
            });

相关问题