Spinner看起来不合适

时间:2012-05-18 19:35:10

标签: android spinner

我的活动中有一个Spinner。我使用ArrayList和自定义SpinnerAdapter来填充按下微调器时弹出的列表。

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

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

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

4 个答案:

答案 0 :(得分:1)

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.
                    }
            });
        }

答案 1 :(得分:1)

使用Activity上下文代替微调器的Application上下文。请参阅getApplicationContext() api的文档以了解其正确用法。

将活动上下文传递给MyAdapter并在TextView回调中创建getView时使用它。

mAdapter = new MyAdapter(mData, this); // this is activity context.

在MyAdapter中:

public MyAdapter(ArrayList<MyData> data, Context context){
     this.data = data;
     mContext = context;
}

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

答案 2 :(得分:0)

您可以使用xml属性android:layout_height设置静态大小。 建议使用dp单位代替px以实现多屏兼容性。

至于文本,请尝试在Spinner xml中使用android:prompt属性。对于颜色,我猜测它和其他小部件一样,只需使用android:textColor

答案 3 :(得分:0)

这个代码是WORKING,微调器正确显示字段,但是我必须说它可能不是100%完美,因为某些原因我无法将字段的初始值留空,它默认具有的值第0项。 包com.cccheck;

public class OneCheckActivity extends Activity {

LayoutInflater factory;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.spinner_view);
    ArrayList tdata = new ArrayList<MyData>();
    MyData mdata =new MyData();
    mdata.setName("");
    mdata.setData("-1");
    MyData ndata =new MyData();
    ndata.setName("ciao belluzzo");
    ndata.setData("1");
    tdata.add(mdata);
    tdata.add(ndata);
    mdata= new MyData();
    mdata.setName("vai alla fnac");
    mdata.setData("2");
    tdata.add(mdata);
    mSpinner = (Spinner) findViewById(R.id.my_spinner);
    factory = LayoutInflater.from(this);

    populateSpinner(tdata);

    setUpAdapter();
    mSpinner.setSelected(false);
    try  {
        mAdapter.notify();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    //mAdapter.registerDataSetObserver(new MyObserver());
}
ArrayList<MyData> mData;
MyAdapter mAdapter = new MyAdapter(null);
Spinner mSpinner;
// This sets up the adapter and the arraylist that contains the data
private void setUpAdapter() {
    mSpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent,
                View view, int pos, long id) {
                MyData g = (MyData) parent.getItemAtPosition(pos);
                // TODO
                Toast.makeText(OneCheckActivity.this , "selected item : " + pos + ", value: " + g.getData(),Toast.LENGTH_LONG).show();

            }
        @Override
            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));
        }
        mAdapter = new MyAdapter(mData);

        mSpinner.setAdapter(mAdapter);
    }
}




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

    ArrayList<MyData> data;

    public MyAdapter(ArrayList<MyData> data){
        this.data = data;
    }
    public void updateData(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 LinearLayout getView(int position, View convertView, ViewGroup parent) {
        LinearLayout pv = (LinearLayout)(factory.inflate(R.layout.spinner_item, null));
        TextView tv = (TextView) pv.findViewById(R.id.textviewid);
        tv.setTextColor(Color.BLACK);
        MyData item = data.get(position);
        tv.setText( item.getName() + " - " + item.getData() + " ");
        tv.setPadding(0, 20, 0, 20);
        return pv;
    }

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

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

    @Override
    public boolean isEmpty() {
        return data.isEmpty();
    }

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

    }

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

    @Override
    public LinearLayout getDropDownView(int position, View convertView, ViewGroup parent) {
        if (convertView instanceof LinearLayout) System.out.println("%%%%%%%%%%%%%%55555 hai ragione");
        return this.getView(position, convertView, parent);
    }
}




}

将其用作spinner_item.xml的布局

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>

<TextView 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="28px"
android:id="@+id/textviewid"
/>

</LinearLayout>