gpt4 book ai didi

android - 微调器不显示所选项目或默认项目,但下拉列表有效

转载 作者:行者123 更新时间:2023-11-30 05:10:45 25 4
gpt4 key购买 nike

android 微调器在默认情况下或当项目被选中时为空。我尝试使用微调器的默认布局,但仍然是空的。我已经检查了该网站上的每个问题,但没有任何帮助。

代码如下:

activity_main.xml 上的微调器 View :

<?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>

Activity :

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"
/>

提前致谢。xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

最佳答案

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();

此时列表还是空的。您需要将此 block 移动到 OnCompleteListener 中。

关于android - 微调器不显示所选项目或默认项目,但下拉列表有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53796013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com