I have registration form and it has two edittexts and one spinner with adapter for gender selection and value from array, when I click register button and select a gender it's going successfully, but when I don't select a gender and click register button it get force close ?
here is my complete code ....
MainActivity
EditText Fullname, Email;
Spinner GenderSpinner;
Button btnReg;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Importing all assets like buttons, text fields
Fullname = (EditText) findViewById(R.id.full_name);
Email = (EditText) findViewById(R.id.Email);
// Gender = (EditText) findViewById(R.id.Gender);
GenderSpinner = (Spinner) findViewById(R.id.Gender);
ArrayAdapter<CharSequence> GenderAdapter = ArrayAdapter
.createFromResource(this, R.array.sex,
android.R.layout.simple_spinner_item);
GenderAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
GenderSpinner.setPrompt("SELECT YOUR GENDER");
GenderSpinner.setAdapter(new NothingSelectedSpinnerAdapter(
GenderAdapter, R.layout.gender_nothing_selected, MainActivity.this));
btnReg = (Button) findViewById(R.id.btnReg);
btnReg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// Read EditText dat
String fullname = Fullname.getText().toString();
String email = Email.getText().toString();
String gender = GenderSpinner.getSelectedItem().toString();
}
});
}
}
NothingSelectedSpinnerAdapter
protected static final int EXTRA = 1;
protected SpinnerAdapter adapter;
protected Context context;
protected int nothingSelectedLayout;
protected int nothingSelectedDropdownLayout;
protected LayoutInflater layoutInflater;
public NothingSelectedSpinnerAdapter(SpinnerAdapter spinnerAdapter,
int nothingSelectedLayout, Context context) {
this(spinnerAdapter, nothingSelectedLayout, -1, context);
}
public NothingSelectedSpinnerAdapter(SpinnerAdapter spinnerAdapter,
int nothingSelectedLayout, int nothingSelectedDropdownLayout,
Context context) {
this.adapter = spinnerAdapter;
this.context = context;
this.nothingSelectedLayout = nothingSelectedLayout;
this.nothingSelectedDropdownLayout = nothingSelectedDropdownLayout;
layoutInflater = LayoutInflater.from(context);
}
#Override
public final View getView(int position, View convertView, ViewGroup parent) {
if (position == 0) {
return getNothingSelectedView(parent);
}
return adapter.getView(position - EXTRA, null, parent);
}
protected View getNothingSelectedView(ViewGroup parent) {
return layoutInflater.inflate(nothingSelectedLayout, parent, false);
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
// BUG! Vote to fix!!
// http://code.google.com/p/android/issues/detail?id=17128 - Spinner
// does not support multiple view types
if (position == 0) {
return nothingSelectedDropdownLayout == -1 ? new View(context)
: getNothingSelectedDropdownView(parent);
}
return adapter.getDropDownView(position - EXTRA, null, parent); // could
}
protected View getNothingSelectedDropdownView(ViewGroup parent) {
return layoutInflater.inflate(nothingSelectedDropdownLayout, parent,
false);
}
#Override
public int getCount() {
int count = adapter.getCount();
return count == 0 ? 0 : count + EXTRA;
}
#Override
public Object getItem(int position) {
return position == 0 ? null : adapter.getItem(position - EXTRA);
}
#Override
public int getItemViewType(int position) {
return position == 0 ? getViewTypeCount() - EXTRA : adapter
.getItemViewType(position - EXTRA);
}
#Override
public int getViewTypeCount() {
return adapter.getViewTypeCount() + EXTRA;
}
#Override
public long getItemId(int position) {
return adapter.getItemId(position - EXTRA);
}
#Override
public boolean hasStableIds() {
return adapter.hasStableIds();
}
#Override
public boolean isEmpty() {
return adapter.isEmpty();
}
#Override
public void registerDataSetObserver(DataSetObserver observer) {
adapter.registerDataSetObserver(observer);
}
#Override
public void unregisterDataSetObserver(DataSetObserver observer) {
adapter.unregisterDataSetObserver(observer);
}
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
return position == 0 ? false : true; // don't allow the 'nothing
}
}
It looks like you are getting null here
String gender = GenderSpinner.getSelectedItem().toString();
because nothing has been selected so GenderSpinner.geSelecetedItem() is null when you try to call toString() on it. You could set a selected item as the default item or if you want to force the user to choose an item then you can just check for null here when running this code and if it is then display a message to the user to choose a gender.
If you go with the second option then you may consider using RadioButtons as it will be more natural for the user or use a text hint as the default value of the Spinner with a message of something like "Please choose a gender"
Related
Background:
I have created a ListView with three columns sNo, product and price. First column is defined as TextView (whose value is auto generated) and the next two columns are EditText (whose value is filled up by the user).
What I want:
I want to add a new row to the ListView whenever:
User hit enter key on any EditText
There is no empty EditText (meaning all the EditText defined so far have some value in them).
Basically I want display a new orders list where users can add orders.
My code so far:
ListView Model:
public class NewTableModel {
private String sNo, product, price;
public NewTableModel(String sNo, String product, String price){
this.sNo = sNo;
this.product = product;
this.price = price;
}
public String getProduct(){ return product; }
public String getPrice(){ return price; }
public String getsNo() { return sNo; }
}
ListView adapter:
public class NewTableAdapter extends BaseAdapter {
private ArrayList<NewTableModel> productList;
private Activity activity;
public NewTableAdapter(Activity activity, ArrayList<NewTableModel> productList) {
super();
this.activity = activity;
this.productList = productList;
}
#Override
public int getCount() { return productList.size(); }
#Override
public Object getItem(int position) { return productList.get(position); }
#Override
public long getItemId(int position) { return position; }
public class ViewHolder {
TextView mSno;
EditText mProduct;
EditText mPrice;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.new_table_row, null);
holder = new ViewHolder();
holder.mSno = (TextView) convertView.findViewById(R.id.sno);
holder.mProduct = (EditText) convertView.findViewById(R.id.product);
holder.mPrice = (EditText) convertView.findViewById(R.id.price);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
NewTableModel item = productList.get(position);
holder.mSno.setText(item.getsNo());
holder.mProduct.setText(item.getProduct());
holder.mPrice.setText(String.valueOf(item.getPrice()));
return convertView;
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
private ArrayList<NewTableModel> productList;
private ListView orderView;
private NewTableAdapter orderAdapter;
private void insertNewRow(){ insertNewRow("",""); }
private void insertNewRow(String productVal, String priceVal){
String serialNoVal = String.valueOf(orderView.getCount() + 1);
NewTableModel item = new NewTableModel(serialNoVal, productVal, priceVal);
productList.add(item);
}
private void setupAdapter(){
productList = new ArrayList<NewTableModel>();
orderView = (ListView) findViewById(R.id.newTableContent);
orderAdapter = new NewTableAdapter(this, productList);
orderView.setAdapter(orderAdapter);
orderAdapter.notifyDataSetChanged();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
setupAdapter();
insertNewRow();
}
}
My Listener:
setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER
&& noEmptyColumn())
insertNewRow();
return false;
}
});
Where should I place that listener ? and how would I check if any column is empty or not (define noEmptyColumn()) ?
You should place the listener where any of EditText values are changed. I would add a Button to any row, and set the listener at there. So in your ViewHolder:
saveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
boolean hasEmpty = false;
for (NewTableModel item: productList) {
if (item.getDesiredField().isEmpty()) {
hasEmpty = true;
break;
}
}
if (!hasEmpty) {
insertNewRow();
notifyDataSetChanged();
}
}
});
Another option could be setting a TextWatcher on EditText :
ed.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
boolean hasEmpty = false;
for (NewTableModel item: productList) {
if (item.getDesiredField().isEmpty()) {
hasEmpty = true;
break;
}
}
if (!hasEmpty) {
insertNewRow();
notifyDataSetChanged();
}
}
#Override
public void afterTextChanged(Editable editable) {
}
});
Just move both methods to your Adapter class. And note that the second solution is not efficient when there are too many rows.
The code below code can be select multiple choice. I only need to select one with a custom toggle button design.
I need to select one option and get the id of the selected toggle button. I searched on the internet for custom listview with single selection toggle button but I didn't find anything useful.
I simply need to add 5 toggle buttons dynamically - when I select one the others should be deselected.
public class MainActivity extends Activity {
int startminute;
int endminute;
Date date;
ToggleButton togg;
ListView lv;
String[] days = {"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY",
"FRIDAY", "SATURDAY"};
boolean[] onOff = new boolean[]{false, false, false, false, false, false,
false};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
onOff = savedInstanceState.getBooleanArray("status");
}
lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(new MyAdapter());
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
}
});
}
public class MyAdapter extends BaseAdapter {
public int getCount() {
return days.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getViewTypeCount() {
return days.length;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
View v = null;
TextView arryText;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item, null);
}
arryText = (TextView) v.findViewById(R.id.inflateText);
togg = (ToggleButton) v.findViewById(R.id.toggleButton1);
v.setTag(new ViewHolder(arryText, togg));
togg.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (onOff[position]) {
togg.setChecked(false);
onOff[position] = false;
Toast.makeText(MainActivity.this, "is off",
Toast.LENGTH_SHORT).show();
} else {
onOff[position] = true;
togg.setChecked(true);
Toast.makeText(MainActivity.this, "is on",
Toast.LENGTH_SHORT).show();
}
}
});
arryText.setText(days[position]);
togg.setChecked(onOff[position]);
return v;
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBooleanArray("status", onOff);
}
}
Instead of using a boolean array you can use an integer that indicates the position of the selected item.
when loading the value in the toggleButton it would be like this:
togg.setChecked(selectedDay == position);
And the item click listener:
public void onClick(View v) {
//Store selected position
selectedDay = position;
//Reload all items
notifyDataSetChanged();
}
I have spinner and my custom spinner adapter.
In some reasons spinner (when user change item selection) dont fire OnItemSelected in my adapter and does not call getView so i cannt render currently selected item ( spinner firing internal item click and set current item correctly)
If User open "software keyboard" spinner works perfectly! It always refreshing spinner content.
I can provide more info if necessary
Any suggestion ?
public class DictionarySpinnerAdapter implements SpinnerAdapter, OnItemSelectedListener{
public DictionarySpinnerAdapter(BaseActivity ctx, Spinner owner)
{
this.ctx = ctx;
li = (LayoutInflater) ctx.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
Owner = owner;
Owner.setClickable(true);
Owner.setOnItemSelectedListener(this);
}
#Override
public int getCount() {
return (dictionaries == null ) ? 0 : dictionaries.size() + offset ;
}
#Override
public Object getItem(int position) {
if ( position > (offset - 1) )
return dictionaries.get(position - offset);
else
return null;
}
#Override
public long getItemId(int position) {
if ( position > (offset - 1) )
return dictionaries.get(position-offset).GetKey();
else
return -1;
}
#Override
public int getItemViewType(int position) {
return 1;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//rendering my items...
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isEmpty() {
return ( dictionaries != null ) ? false : true;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
//rendering dropdowns
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
SetSelectedItem((int)id);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
SetSelectedItem(null);
}
public void SetSelectedItem(Integer byKey)
{
if ( byKey == null|| (byKey == -1 && allowNull))
{
selectedPosition = 0;
if ( allowNull)
selectedKey = -1;
else
if (dictionaries!=null&& dictionaries.size()>0)
selectedKey = dictionaries.get(0).GetKey();
if (selectedKey > -1)
firstSelected = true;
handleItemClick(selectedKey);
Owner.setSelection(selectedPosition);
return;
}
for(Dictionary entry : dictionaries )
if ( entry.GetKey() == byKey.intValue() )
{
selectedKey = byKey;
selectedPosition = dictionaries.indexOf(entry) + offset;
handleItemClick(selectedKey);
break;
}
// Log.i("MAH", "setSelectedItem("+selectedPosition+")");
firstSelected = true;
Owner.setSelection(selectedPosition);
}
}
EDIT:
After some debugging i think in this code (Absspinner.java) is problem.
#Override
public void setSelection(int position) {
setNextSelectedPositionInt(position); //works fine
requestLayout(); // ??
invalidate(); // ??
}
EDIT2:
Spinner is inside TableLayout if I move it outside tablelayout its works fine. So my new question is: how i can refresh view inside TableLayout ?
I have a listview that has a custom adapter, and I was trying to make it searchable using an Action Item. When I click the search icon in the action bar, the edit text comes up, but when I enter text and click "done" on the keyboard, nothing happens.
Here is the main class:
public class ItemId extends SherlockListActivity {
EditText editsearch;
ArrayAdapter<String> adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Context ctx = getApplication();
Resources res = ctx.getResources();
String[] options = res.getStringArray(R.array.item_ids);
String[] ids = res.getStringArray(R.array.item_names);
TypedArray icons = res.obtainTypedArray(R.array.item_images);
adapter = new ItemIDAdapter(ctx, R.layout.idslistitem, ids, options, icons);
setListAdapter(adapter );
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Get the options menu view from menu.xml in menu folder
getSupportMenuInflater().inflate(R.menu.items_menu, menu);
// Locate the EditText in menu.xml
editsearch = (EditText) menu.findItem(R.id.menu_search).getActionView();
// Capture Text in EditText
editsearch.addTextChangedListener(textWatcher);
// Show the search menu item in menu.xml
MenuItem menuSearch = menu.findItem(R.id.menu_search);
menuSearch.setOnActionExpandListener(new OnActionExpandListener() {
// Menu Action Collapse
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Empty EditText to remove text filtering
editsearch.setText("");
editsearch.clearFocus();
return true;
}
// Menu Action Expand
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Focus on EditText
editsearch.requestFocus();
// Force the keyboard to show on EditText focus
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
return true;
}
});
// Show the settings menu item in menu.xml
MenuItem menuSettings = menu.findItem(R.id.home);
// Capture menu item clicks
menuSettings.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Intent intent2 = new Intent(ItemId.this, Home.class);
startActivity(intent2);
return true;
}
});
return true;
}
// EditText TextWatcher
private TextWatcher textWatcher = new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
String text = editsearch.getText().toString()
.toLowerCase(Locale.getDefault());
adapter.getFilter().filter(text);
};
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
};
}
And here is the Adapter class:
public class ItemIDAdapter extends ArrayAdapter<String> implements Filterable {
public LayoutInflater mInflater;
public String[] mStrings;
public String[] mIds;
public TypedArray mIcons;
public int mViewResourceId;
public ItemIDAdapter(Context ctx, int viewResourceId,
String[] strings, String[] ids, TypedArray icons) {
super(ctx, viewResourceId, strings);
mInflater = (LayoutInflater)ctx.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mStrings = strings;
mIds = ids;
mIcons = icons;
mViewResourceId = viewResourceId;
}
#Override
public int getCount() {
return mStrings.length;
}
#Override
public String getItem(int position) {
return mStrings[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon);
iv.setImageDrawable(mIcons.getDrawable(position));
TextView tv = (TextView)convertView.findViewById(R.id.option_text);
tv.setText(mStrings[position]);
TextView tv1 = (TextView)convertView.findViewById(R.id.itemids);
tv1.setText(mIds[position]);
return convertView;
}
}
If anyone has any idea as to why nothing happens when I search, or knows how to fix it, it'd be greatly appreciated. Thanks!!
You need to implement a custom filter. Have a look at this post here someone else has had a similar problem which he solved: https://stackoverflow.com/a/8258457/2045570
Below is mine code, which is working perfectly:-
public class ExpenditureAdapter extends ArrayAdapter<Expenditure> implements Filterable {
ArrayList<Expenditure> listArray;
ArrayList<Expenditure> filteredlistArray;
private Filter mFilter;
public ExpenditureAdapter(#NonNull Context context, ArrayList<Expenditure> list) {
super(context,0, list);
listArray=list;
filteredlistArray=list;
}
#Override
public int getCount() {
return filteredlistArray.size(); // total number of elements in the list
}
public void add(Expenditure object) {
listArray.add(object);
this.notifyDataSetChanged();
}
public Filter getFilter() {
if (mFilter == null) {
mFilter = new CustomFilter();
}
return mFilter;
}
#Override
public Expenditure getItem(int i) {
return filteredlistArray.get(i); // single item in the list
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
#Override
public long getItemId(int i) {
return i; // index number
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Expenditure expenditure = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.listexpenditure, parent, false);
}
// Lookup view for data population
TextView ExpId = (TextView) convertView.findViewById(R.id.ExpId);
TextView ExDate = (TextView) convertView.findViewById(R.id.ExDate);
TextView ExpAmt = (TextView) convertView.findViewById(R.id.ExpAmt);
TextView ExpDetail = (TextView) convertView.findViewById(R.id.ExpDetail);
// Populate the data into the template view using the data object
ExpId.setText(expenditure.ExpId);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String dateTime = dateFormat.format(expenditure.ExpDate);
ExDate.setText(dateTime);
ExpAmt.setText(String.valueOf(expenditure.ExpAmt));
ExpDetail.setText(expenditure.ExpDetail);
return convertView;
}
private class CustomFilter extends Filter {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if(constraint == null || constraint.length() == 0) {
ArrayList<Expenditure> list = new ArrayList<Expenditure>(listArray);
results.values = list;
results.count = list.size();
} else {
ArrayList<Expenditure> newValues = new ArrayList<Expenditure>();
for(int i = 0; i < listArray.size(); i++) {
Expenditure e_item = listArray.get(i);
if(e_item.ExpDetail.contains(constraint)) {
newValues.add(e_item);
}
}
results.values = newValues;
results.count = newValues.size();
}
return results;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
filteredlistArray = (ArrayList<Expenditure>) results.values;
notifyDataSetChanged();
}
}
and use it in your code as below:-
private TextWatcher textWatcher = new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
String text = txtSearch.getText().toString()
.toLowerCase(Locale.getDefault());
adapter.getFilter().filter(text);
};
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
};
I have a Spinner on my Activity. I use an ArrayList and a custom SpinnerAdapter to populate the list that pops up when the Spinner is pressed.
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.
Also, when I select an item from the Spinner and then print the selected item position, it prints -1. Many have commented that there is no list of data attached to my spinner, but there obviously is. How else could I press on the Spinner and then choose from a 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'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
This is because you are referencing BLANK list
mData = new ArrayList<MyData>();
mAdapter = new MyAdapter(mData);
mSpinner.setAdapter(mAdapter);
Set spinner adapter in onPostExecute() 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.
}
});
}
Use Activity context instead of the Application context for your spinner. See documentation of getApplicationContext() api to understand its proper usage.
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.
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;
}
You can set static sizes using the xml attribute android:layout_height.
Using dp unit instead of px is recommended for multiple screen compatibility.
As for the text, try to use android:prompt attribute in your Spinner xml. For the color I'm guessing it's like other widgets, just use 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);
}
}
}
use this as 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>