【问题标题】:Value not displaying Correctly in Spinner Android值未在 Spinner Android 中正确显示
【发布时间】:2020-09-06 17:06:02
【问题描述】:

我有一个 Spinner,我想从 Firebase 数据库中获取价值。我尝试了我所知道的,因为我是 Android 新手,但 Spinner 的价值并没有像我想要的那样来。

当我直接传递字符串数组时,它会正确出现,但是当字符串数组填充 Firebase 数据时它会出现问题。这也是我的数据库。

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

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) {

        }
    });

}}

我不知道我做错了什么。

【问题讨论】:

    标签: android android-studio spinner android-spinner


    【解决方案1】:

    当您获取数据时,您犯了一些错误。你不是根据你的插入技术来获取的,试试这个 在数据快照的 for 循环中使用此代码:

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

    通过这样做,只有带有the 1710f 键的数据将被挑选出来,用于您列出所有键的其他值。 希望这会锻炼

    【讨论】:

      【解决方案2】:

      实际上问题在于我以错误的方式将数据存储在 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);
              }
          });
      

      我只需要删除 .child(User_Id);。

      现在一切正常。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-06-08
        • 1970-01-01
        • 2015-04-03
        • 2017-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多