如何解决在Spinner Android中值无法正确显示
我有一个微调器,需要Firebase数据库中的值。我尝试了我所知道的东西,因为我是Android新手,但是Spinner中的价值并没有如我所愿地实现。
当我直接传递String Array时,它可以正常使用,但是当String Array中填充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) {
}
});
}}
我不知道我在做什么错。
解决方法
在获取数据时,您犯了一些错误。您不是根据您的插入技术获取的,请尝试此操作 在datasnapshot的for循环中,使用以下代码:
String a = batches.child("1710f").getValue().toString()
通过执行此操作,将仅使用the 1710f
键的数据来选择列出所有键的其他值。
希望这会锻炼
实际上问题是我以错误的方式将数据存储在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);。
现在工作正常。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。