gpt4 book ai didi

android - 微调器看起来不合适

转载 作者:太空宇宙 更新时间:2023-11-03 10:27:40 26 4
gpt4 key购买 nike

我的 Activity 上有一个 Spinner。我使用 ArrayList 和自定义 SpinnerAdapter 来填充按下 Spinner 时弹出的列表。

我的问题是 Spinner 在未按下时在 Activity 上的显示方式。全是灰色的。没有文字可见。即使在我按下微调器然后从结果列表中选择一个项目后,微调器也不会填充文本。

此外,当我从微调器中选择一个项目然后打印所选项目的位置时,它会打印 -1。许多人评论说我的微调器没有附加数据列表,但显然有。我还能如何按下微调器,然后从结果列表中进行选择?

        // This sets up the adapter and the arraylist that contains the data
private void setUpAdapter() {
mData = new ArrayList<MyData>();
mAdapter = new MyAdapter(mData);
mSpinner.setAdapter(mAdapter);

mSpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
MyData g = (MyData) parent.getItemAtPosition(pos);
// TODO
}

public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
});
}


// this populates the arraylist that is attached to the spinner's adapter
// it is called once an AsyncTask finishes pulling data from a local database
private void populateSpinner(ArrayList<MyData> result) {
if (result != null) {
if (mData == null) {
mData = new ArrayList<MyData>();
}
else {
mData.clear();
}

for (int index = 0; index < result.size(); index++) {
mData.add(result.get(index));
}

mSpinner.setSelected(0);
}
}




// this is the adapter for the spinner
private class MyAdapter implements SpinnerAdapter {

ArrayList<MyData> data;

public MyAdapter(ArrayList<MyData> data){
this.data = data;
}

@Override
public int getCount() {
return data.size();
}

@Override
public Object getItem(int position) {
return data.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public int getItemViewType(int position) {
return android.R.layout.simple_spinner_dropdown_item;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView v = new TextView(getApplicationContext());
v.setTextColor(Color.BLACK);
v.setText(data.get(position).getName());
v.setPadding(0, 20, 0, 20);
return v;
}

@Override
public int getViewTypeCount() {
return 1;
}

@Override
public boolean hasStableIds() {
return false;
}

@Override
public boolean isEmpty() {
return false;
}

@Override
public void registerDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}

@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return this.getView(position, convertView, parent);
}
}


<Spinner
android:id="@+id/my_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

最佳答案

When I select an item from the Spinner and then print the selected item position, it prints -1

这是因为您正在引用BLANK 列表

mData = new ArrayList<MyData>();
mAdapter = new MyAdapter(mData);
mSpinner.setAdapter(mAdapter);

在 AsynTask 的 onPostExecute() 中设置微调适配器。

@Override
protected void onPreExecute() {
mData = new ArrayList<MyData>();
super.onPreExecute();
}

@Override
protected Void doInBackground(String... params) {
//gets "result" to fill mData
return null;
}

@Override
protected void onPostExecute(Void result) {
setUpAdapter();
}


private void setUpAdapter() {
if (result != null) {
if (mData == null) {
mData = new ArrayList<MyData>();
}
else {
mData.clear();
}

for (int index = 0; index < result.size(); index++) {
mData.add(result.get(index));
}

mAdapter = new MyAdapter(mData);
mSpinner.setAdapter(mAdapter);
mSpinner.setSelected(0);

mSpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
MyData g = (MyData) parent.getItemAtPosition(pos);
// TODO
}

public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
});
}

关于android - 微调器看起来不合适,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10658726/

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