简体   繁体   English

值未在 Spinner Android 中正确显示

[英]Value not displaying Correctly in Spinner Android

I have a Spinner in which I want value from Firebase Database.我有一个 Spinner,我想从中获得 Firebase 数据库的价值。 I try what I Know since I am new to Android but the value in Spinner is not coming as i want it is coming like this.我尝试了我所知道的,因为我是 Android 新手,但 Spinner 的价值并没有像我希望的那样到来。

微调图像 在此处输入图片说明

When i am passing String Array directly it is coming Properly but it is doing problem when the String Array fill with the Firebase data.Here is also my Database.当我直接传递字符串数组时,它会正确地出现,但是当字符串数组填充 Firebase 数据时出现问题。这里也是我的数据库。

final String[] batcht = {"1903f","343","33323"};

在此处输入图片说明 在此处输入图片说明

XML XML

    <Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/stubatch"
    />

JAVA爪哇

public class Student extends AppCompatActivity {
 DatabaseReference ref;
 Spinner spinner;
 List<String> store;
 String[] batchlist;
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_student);
   spinner = findViewById(R.id.stubatch);

    store =new ArrayList<>();

    Student_ID =getIntent().getStringExtra("Student_Id");
    ref = FirebaseDatabase.getInstance().getReference("Batch");

    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot batches : dataSnapshot.getChildren()){
                    String a= batches.getValue().toString();
                    store.add(a);
                }

            batchlist = new String[store.size()];

            for(int i=0;i<batchlist.length;i++){
                batchlist[i]=store.get(i);

            }

            ArrayAdapter<String> adapter = new ArrayAdapter<String> 
            (getApplicationContext(),android.R.layout.simple_spinner_item,batchlist);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
            spinner.setAdapter(adapter);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

}}

I don't know what I am doing wrong.我不知道我做错了什么。

When you are fetching data you are doing some mistake.当您获取数据时,您会犯一些错误。 You are not fetching according to ur insertion technique try this In for loop of datasnapshot use this code:您不是根据您的插入技术获取的,请尝试在数据快照的 for 循环中使用此代码:

String a = batches.child("1710f").getValue().toString()

By doing this only data with the 1710f key will be picked for other values you have listed out all the keys.通过这样做,只有具有the 1710f键的数据才会被选择用于您列出所有键的其他值。 Hopefully, this will workout希望这会锻炼

Actually the problem was that I was Storing the Data in the Firebase the wrong way.实际上问题在于我以错误的方式将数据存储在 Firebase 中。

  save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String batchdata = edit.getText().toString();
            ref = FirebaseDatabase.getInstance().getReference("Batch").child(User_Id);

            ref.child(batchdata).setValue(batchdata);

            Toast.makeText(getApplicationContext(), "Successful", Toast.LENGTH_SHORT).show();

            Intent i  = new Intent(getApplicationContext(),Faculty.class);
            i.putExtra("User_Id",User_Id);
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(i);
        }
    });

I Just have to remove the .child(User_Id);.我只需要删除 .child(User_Id);。

Now it is working Fine.现在它工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM