Spinner does not fit

I have a spinner at my work. I use ArrayList and a custom SpinnerAdapter to populate the list that appears when I click Spinner.

My problem is how Spinner looks at Activity when it is not clicked. Everything is gray. The text is not displayed. Even after I click the spinner and then select an item from the resulting list, Spinner does not fill the text.

Also, when I select an element from Spinner and then print the position of the selected element, it prints -1. Many commented that there is no list of data attached to my counter, but obviously there is. How else can I click on Spinner and then select from the resulting list?

// 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 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
source share
4 answers
 When I select an item from the Spinner and then print the selected item position, it prints -1 

This is because you are linking to the BLANK list.

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

Install the spinner adapter in onPostExecute() 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. } }); } 
+2
source

Use the action context instead of the application context for your counter. See the documentation for getApplicationContext() api to understand its proper use.

Pass the MyAdapter activity MyAdapter and use it when creating the TextView in getView .

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

In 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; } 
+1
source

You can set static sizes using the xml attribute android:layout_height . Using a dp block instead of a px is recommended for compatibility with multiple screens.

For text, try using the android:prompt attribute in your Spinner xml. For color, I assume this is similar to other widgets, just use android:textColor

0
source

THIS code WORKS, the spinner displays the field correctly, however I should say maybe it is not 100% perfect, for some reason they cannot leave the initial field value empty, it defaults to 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 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); } } } 

use this as a layout for 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> 
0
source

Source: https://habr.com/ru/post/1413242/


All Articles