默认情况下或选中项目时,android微调框为空。我尝试使用微调器的默认布局,但仍然为空。我已经检查了该网站上的每个问题,但没有一个帮助。
代码如下:
activity_main.xml上的旋转视图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@color/background"
android:orientation="vertical">
<TextView
android:id="@+id/showTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:textSize="20sp"
android:textAlignment="center"
android:textColor="@color/textColor"
android:fontFamily="monospace"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"
>
</Spinner>
</LinearLayout>
活动:
public class ShowActivity extends AppCompatActivity {
private List<String> list;
Spinner dropdown;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
TextView titleView = findViewById(R.id.showTitle);
String title = getIntent().getExtras().getString("title");
titleView.setText(title);
list = new ArrayList<>();
dropdown = findViewById(R.id.spinner);
FirebaseFirestore.getInstance().collection(title).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
list.add(document.getId());
}
Log.d("Success ", list.toString());
} else {
Log.d("Failed ", "Error getting documents: ", task.getException());
}
}
});
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ShowActivity.this, R.layout.spinner_items, list);
adapter.setDropDownViewResource(R.layout.spinner_items);
dropdown.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
spinner_items.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/spinnerTV"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:textSize="20sp"
android:text="Text"
android:gravity="start"
android:padding="10dp"
android:textColor="@color/textColor"
android:layout_marginBottom="3dp"
android:layout_margin="8dp"
/>
先谢谢您。 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
答案 0 :(得分:0)
FirebaseFirestore.getInstance().collection(title).get().addOnCompleteListener(...)
是一个异步调用。您的代码将在此处继续执行:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ShowActivity.this, R.layout.spinner_items, list);
adapter.setDropDownViewResource(R.layout.spinner_items);
dropdown.setAdapter(adapter);
adapter.notifyDataSetChanged();
此时,列表仍然为空。您需要将此块移动到OnCompleteListener
内。
答案 1 :(得分:0)
在dropdown.setAdapter(adapter);之后您必须设置项目选择/单击的侦听器,请尝试以下操作:
dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//.. do stuff here
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// do default when nothing is selected
}
});
dropdown.setSelection(0) // this makes default selection for dropdown item....
希望我能为您提供最好的帮助, Csongi