English 中文(简体)
S
原标题:Spinner doesn t look appropriate

我的活动有一位主人。 我使用ArrayList和习俗SpinnerAdapter,在Spinner受到压力时填满名单。

My problem is the way the Spinner looks on the Activity when it is not pressed. It is all gray. No text is visible. Even after I press the spinner and then choose an item from the resulting list, the Spinner does not populate with text.

此外,当我从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" />
问题回答
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);

Set spinner Adaptationer in PostExecute() of AsynTask.

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

利用活动环境,而不是为你的主人申请环境。 见getApplicationContext(。 a 了解其适当使用情况。

Pass the activity context to MyAdapter and use it in creating the TextView s in getView callback.

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

You can set static sizes using the xml attribute android:layout_height. Using dp unit instead of px is recommended for multiple screen compatibility.

关于案文,请在您的Spinner xml中使用android:prompt属性。 仅使用<代码>android:textColor

THIS code is WORKING, the spinner correctly display the field, however i must say maybe it is not 100% perfect, cause for some reason im unable to leave blank the initial value of the field, it has by default the value of item 0. package 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);
    }
}




}

将该术语作为主要要素使用:项目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>




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签