【问题标题】:Spinner not showing selected item or default item but dropdown list works微调器未显示选定项目或默认项目,但下拉列表有效
【发布时间】:2018-12-16 04:03:09
【问题描述】:

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

提前谢谢你。 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

【问题讨论】:

  • android:layout_width="match_parent"android:layout_height="match_parent" 应该是 wrap_content
  • 这会改变下拉列表中文本视图的宽度,但选择时文本仍然不出现。
  • 你也改变了高度吗?我必须查看更多您的代码才能提供帮助
  • @Emmanuel 我已经更新了这个问题现在有帮助吗?

标签: android spinner


【解决方案1】:

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 内。

【讨论】:

  • 很高兴我能提供帮助。
【解决方案2】:

在 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....

希望我能帮助你,最好的问候, 松吉

【讨论】:

  • 谢谢,这是我接下来要添加的内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-10
相关资源
最近更新 更多