How to change the position of opened spinner? - android

I would like the spinner dropdown to open right below the spinner itself.
E.g.:
How can i set the position of spinner dropdown?

Declare the spinner mode as a dropdown:
android:spinnerMode="dropdown"
then use the vertical offset to close the gap:
android:dropDownVerticalOffset="-15dp"
Spinner Android Documentation

You have to extend Spinner and change the location of AlertDialog (spinner when clicked acts as alertDialog).
Code (does a bit more than just position, it also sets background for opened spinner):
public class CustomSpinner extends Spinner {
private AlertDialog mPopup;
public CustomSpinner(Context context) {
super(context);
}
public CustomSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomSpinner(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
}
#Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mPopup != null && mPopup.isShowing()) {
mPopup.dismiss();
mPopup = null;
}
}
//when clicked alertDialog is made
#Override
public boolean performClick() {
Context context = getContext();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
CharSequence prompt = getPrompt();
if (prompt != null) {
builder.setTitle(prompt);
}
mPopup = builder.setSingleChoiceItems(
new DropDownAdapter(getAdapter()),
getSelectedItemPosition(), this).show();
WindowManager.LayoutParams WMLP = mPopup.getWindow().getAttributes();
//width and height must be set to anything other than WRAP_CONTENT!
WMLP.x = 0; // x position
WMLP.y = 50; // y position
WMLP.height =390 ; //LayoutParams.WRAP_CONTEN
WMLP.width = 315;
WMLP.horizontalMargin = 0;
WMLP.verticalMargin = 0;
mPopup.getWindow().setAttributes(WMLP);
//ListView.getDefaultSize(size, measureSpec)
ListView listView = mPopup.getListView();
//listView.set
// Remove divider between rows
listView.setDivider(null);
// Set custom background
listView.setBackgroundResource(R.drawable.drop);
// Remove background from all (grand)parent's
ViewParent parent = listView.getParent();
while (parent != null && parent instanceof View) {
((View) parent).setBackgroundDrawable(null);
parent = parent.getParent();
}
return true;
}
#Override
public void onClick(DialogInterface dialog, int which) {
setSelection(which);
dialog.dismiss();
mPopup = null;
}
* <p>Wrapper class for an Adapter. Transforms the embedded Adapter instance
* into a ListAdapter.</p>
*/
private static class DropDownAdapter implements ListAdapter, SpinnerAdapter {
private SpinnerAdapter mAdapter;
/**
* <p>Creates a new ListAddapter wrapper for the specified adapter.</p>
*
* #param adapter the Adapter to transform into a ListAdapter
*/
public DropDownAdapter(SpinnerAdapter adapter) {
this.mAdapter = adapter;
}
public int getCount() {
return mAdapter == null ? 0 : mAdapter.getCount();
}
public Object getItem(int position) {
return mAdapter == null ? null : mAdapter.getItem(position);
}
public long getItemId(int position) {
return mAdapter == null ? -1 : mAdapter.getItemId(position);
}
public View getView(int position, View convertView, ViewGroup parent) {
return getDropDownView(position, convertView, parent);
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return mAdapter == null ? null :
mAdapter.getDropDownView(position, convertView, parent);
}
public boolean hasStableIds() {
return mAdapter != null && mAdapter.hasStableIds();
}
public void registerDataSetObserver(DataSetObserver observer) {
if (mAdapter != null) {
mAdapter.registerDataSetObserver(observer);
}
}
public void unregisterDataSetObserver(DataSetObserver observer) {
if (mAdapter != null) {
mAdapter.unregisterDataSetObserver(observer);
}
}
/**
* <p>Always returns false.</p>
*
* #return false
*/
public boolean areAllItemsEnabled() {
return true;
}
/**
* <p>Always returns false.</p>
*
* #return false
*/
public boolean isEnabled(int position) {
return true;
}
public int getItemViewType(int position) {
return 0;
}
public int getViewTypeCount() {
return 1;
}
public boolean isEmpty() {
return getCount() == 0;
}
}
}
Then you just gotta insert it to your layout with "yourPackage.CustomSpinner" element like:
<yourPackage.CustomSpinner
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:layout_width="fill_parent">
</yourPackage.CustomSpinner>

For dropdown Spinner mode you can use this:
mSortingSpinner.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
mSortingSpinner.setDropDownVerticalOffset(
mSortingSpinner.getDropDownVerticalOffset() + mSortingSpinner.getHeight());
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
mSortingSpinner.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
mSortingSpinner.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
});
This set vertical offset of dropdown by spinner height.

Related

Android Spinner does not refresh after item selection

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 ?

Viewpager, Cursor and Fragment

I am new to the viewpager and was wondering if anyone could point me to a tutorial or source code of a project that uses a viewpager with fragments and a database. I've seen examples of PagerAdapters but I'm just not getting how they all work together (Cursor, Fragment and PagerAdapter)
Thanks in advance.
I have posted some answers in the other posts related to your question.
Here's some of the links that you might find helpful.
First Link: To get custom views in every page slide. (Accepted answer)
Android:How to create different view in ViewPager?
Second: How to properly use fragments with ViewPager. (Accepted answer)
How to properly use fragments with ViewPager?
If those two links are not that helpful, try this:
public class AllActivities extends FragmentActivity implements ActionBar.TabListener {
public ViewPager viewPager;
private AllPagesAdapter mAdapter;
private ActionBar actionBar;
private String [] tabs = {"Android","CoreJava","J2EE","Database","Web Services"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing all stuff
viewPager = (ViewPager)findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new AllPagesAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//Add the tabs here
for(String tab_name:tabs){
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
#Override
public void onPageSelected(int position){
//on Page change, that particular page should be selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0,float arg1,int arg2){
}
#Override
public void onPageScrollStateChanged(int position){
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {
viewPager.setCurrentItem(tab.getPosition());
}
}
//This is one of the fragment and assume that Expandablelistview is your list view.. In both the cases, i am setting the adapter in the onActivityCreated() method.
public class Android extends android.support.v4.app.Fragment {
ExpandableListAdapter listAdapter;
// private ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
ListView lv1;
private static final String QUESTION = "question";
private static final String ANSWERS = "answer";
// ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mikeview = inflater.inflate(R.layout.androidlayout, container, false);
return mikeview;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ExpandableListView expListView = null;
try{
expListView = (ExpandableListView) getActivity().findViewById(R.id.androidExpandableList);
}
catch (Exception e){
e.printStackTrace();
}
new Thread(){
#Override
public void run(){
}
}.start();
try {
setParent();
prepareChild();
} catch (Exception e) {
e.printStackTrace();
}
ExpandableListAdapter listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);
expListView.setAdapter(listAdapter);
// Listview on child click listener
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
/*Toast.makeText(
getActivity(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();*/
return true;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
/* Toast.makeText(getActivity(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();*/
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
/*Toast.makeText(getActivity(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();*/
}
});
}
public void setParent(){
listDataHeader = new ArrayList<String>();
try{
JSONObject json = new JSONObject(loadJSONFromAsset());
JSONArray array = json.getJSONArray("androidquestion");
for(int my =0;my<array.length();my++){
JSONObject c = array.getJSONObject(my);
String topics = c.getString(QUESTION);
listDataHeader.add(topics);
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
public void prepareChild() throws JSONException {
listDataChild = new HashMap<String, List<String>>();
try{
JSONObject json = new JSONObject(loadJSONFromAsset());
JSONArray array = json.getJSONArray("androidquestion");
for(int mz = 0;mz<array.length();mz++){
ArrayList<String> child = new ArrayList<String>();
JSONObject d = array.getJSONObject(mz);
String ans = d.getString(ANSWERS);
child = new ArrayList<String>();
child.add(ans);
int position = mz ;
listDataChild.put(listDataHeader.get( position),child);
}
}
catch(JSONException e) {
e.printStackTrace();
}
}
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getActivity().getAssets().open("android.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
}
//This is the adapter.
public class AllPagesAdapter extends FragmentStatePagerAdapter {
public AllPagesAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new Android();
case 1:
return new CoreJava();
case 2:
return new J2EE();
case 3:
return new Database();
case 4:
return new WebServices();
}
return null;
}
#Override
public int getCount() {
return 5;
}
}
//This is the adapter for my expandable list view.. in your case, your can just use the listadapter or any other adapter you want to use. This is just an optional part.
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.newlistitems,null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.newlistviewitems);
txtListChild.setText(childText);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
#Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
#Override
public int getGroupCount() {
return this._listDataHeader.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater myInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = myInflater.inflate(R.layout.newlistgroup, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.newlistviewgroup);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Lemme know if you need some more examples on that..:)
You need to create abstract class adapter to use both cursor and pager adapter and extend to your own adapter and bind the view.
public abstract class CursorPagerAdapter extends PagerAdapter {
public static final String TAG = CursorPagerAdapter.class.getSimpleName();
protected WeakReference<Context> ctxRef;
protected boolean mDataValid;
protected boolean mAutoRequery;
protected Cursor mCursor;
protected int mRowIDColumn;
protected ChangeObserver mChangeObserver;
protected DataSetObserver mDataSetObserver;
/**
* If set the adapter will call requery() on the cursor whenever a content change notification is
* delivered. Implies {#link #FLAG_REGISTER_CONTENT_OBSERVER}.
*
* #deprecated This option is discouraged, as it results in Cursor queries being performed on the
* application's UI thread and thus can cause poor responsiveness or even Application
* Not Responding errors. As an alternative, use {#link android.app.LoaderManager} with
* a {#link android.content.CursorLoader}.
*/
#Deprecated
public static final int FLAG_AUTO_REQUERY = 0x01;
/**
* If set the adapter will register a content observer on the cursor and will call {#link
* # onContentChanged()} when a notification comes in. Be careful when using this flag: you will
* need to unset the current Cursor from the adapter to avoid leaks due to its registered
* observers. This flag is not needed when using a CursorAdapter with a {#link
* android.content.CursorLoader}.
*/
public static final int FLAG_REGISTER_CONTENT_OBSERVER = 0x02;
public CursorPagerAdapter(Context context, Cursor c, int flags) {
init(context, c, flags);
}
void init(Context context, Cursor c, int flags) {
if ((flags & FLAG_AUTO_REQUERY) == FLAG_AUTO_REQUERY) {
flags |= FLAG_REGISTER_CONTENT_OBSERVER;
mAutoRequery = true;
} else {
mAutoRequery = false;
}
boolean cursorPresent = c != null;
mCursor = c;
mDataValid = cursorPresent;
ctxRef = new WeakReference<>(context);
mRowIDColumn = cursorPresent ? c.getColumnIndexOrThrow("_id") : -1;
if ((flags & FLAG_REGISTER_CONTENT_OBSERVER) == FLAG_REGISTER_CONTENT_OBSERVER) {
mChangeObserver = new ChangeObserver();
mDataSetObserver = new MyDataSetObserver();
} else {
mChangeObserver = null;
mDataSetObserver = null;
}
if (cursorPresent) {
if (mChangeObserver != null) {
c.registerContentObserver(mChangeObserver);
}
if (mDataSetObserver != null) {
c.registerDataSetObserver(mDataSetObserver);
}
}
}
public Cursor getCursor() {
return mCursor;
}
#Override
public int getCount() {
if (mDataValid && mCursor != null)
return mCursor.getCount();
else
return 0;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
if (!mDataValid) {
throw new IllegalStateException("this should only be called when the cursor is valid");
}
if (!mCursor.moveToPosition(position)) {
throw new IllegalStateException("couldn't move cursor to position " + position);
}
Context context = ctxRef.get();
if (context != null) {
View v = newView(context, mCursor, (ViewGroup) container);
bindView(v, context, mCursor);
((ViewPager) container).addView(v);
return v;
} else {
throw new IllegalStateException("context is null");
}
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public boolean isViewFromObject(View view, Object o) {
return view == o;
}
public abstract View newView(Context context, Cursor cursor, ViewGroup parent);
public abstract void bindView(View view, Context context, Cursor cursor);
public void changeCursor(Cursor cursor) {
Cursor old = swapCursor(cursor);
if (old != null) {
old.close();
}
}
public Cursor swapCursor(Cursor newCursor) {
if (newCursor == mCursor) return null;
Cursor oldCursor = mCursor;
if (oldCursor != null) {
if (mChangeObserver != null) {
oldCursor.unregisterContentObserver(mChangeObserver);
}
if (mDataSetObserver != null) {
oldCursor.unregisterDataSetObserver(mDataSetObserver);
}
}
mCursor = newCursor;
if (newCursor != null) {
if (mChangeObserver != null) {
newCursor.registerContentObserver(mChangeObserver);
}
if (mDataSetObserver != null) {
newCursor.registerDataSetObserver(mDataSetObserver);
}
mRowIDColumn = newCursor.getColumnIndexOrThrow("_id");
mDataValid = true;
// notify the observers about the new cursor
notifyDataSetChanged();
} else {
mRowIDColumn = -1;
mDataValid = false;
notifyDataSetChanged();
}
return oldCursor;
}
private void onContentChange() {
if (mAutoRequery && mCursor != null && !mCursor.isClosed()) {
mDataValid = mCursor.requery();
}
}
private class ChangeObserver extends ContentObserver {
public ChangeObserver() {
super(new Handler());
}
/**
*
* #return True if self-change notifications should be delivered to the observer.
*/
#Override
public boolean deliverSelfNotifications() {
return true;
}
/**
* This method is called when a content change occurs.
* #param selfChange
*/
#Override
public void onChange(boolean selfChange) {
onContentChange();
}
}
private class MyDataSetObserver extends DataSetObserver {
#Override
public void onChanged() {
mDataValid = true;
notifyDataSetChanged();
}
#Override
public void onInvalidated() {
mDataValid = false;
notifyDataSetChanged();
}
}
}
Sample Adapter (in kotlin):
class SampleAdapter(context: Context, cursor: Cursor?, autoQuery: Int): CursorPagerAdapter(context, cursor, autoQuery) {
internal var mContext: Context
internal var mLayoutInflater: LayoutInflater
internal var aQuery: AQuery
val imageWidth = Helper.getDisplayWidth(context)
init {
mContext = context
mLayoutInflater = mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
aQuery = AQuery(context)
}
override fun newView(context: Context?, cursor: Cursor?, parent: ViewGroup?): View {
return mLayoutInflater.inflate(R.layout.your_item_row, parent, false)
}
override fun bindView(view: View?, context: Context?, cursor: Cursor?) {
val sampleVo = SampleVo.getValueFromCursor(cursor)
val tvTitle = view?.findViewById<AppCompatTextView>(R.id.tvTitle)
tvTitle?.text = sampleVo.title
}
Original Source: link

Android Spinner with adapter get force close?

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"

ListView does not render all items until interacted with

I have a very strange problem while using my ListView.
Only a part of my adapter items are renderd in the listview on screen but when I interact with the listview (ie tries to scroll it) all items are renderd properly.
This fenonemon only occurs if i have less items than the screen can show. Take a look at these screenshots below.
Before interaction:
After interaction:
Source code of activity where adding items:
String[] jRests = getResources().getStringArray(R.array.j_restaurants);
String[] lRests = getResources().getStringArray(R.array.l_restaurants);
items = new ArrayList<Object>();
items.add(getString(R.string.campus_j));
for(String item : jRests){
String[] val = item.split(",,,");
items.add(new FoodSectionListItem(new Restaurant(val[0], val[1], val[2], "")));
}
items.add(getString(R.string.campus_l));
for(String item : lRests){
String[] val = item.split(",,,");
items.add(new FoodSectionListItem(new Restaurant(val[0], val[1], val[2], "")));
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
adapter = new BaseSectionAdapter(this, R.layout.list_item_fragment_header);
if(!isTabletView()){
adapter.setSelectedItem(-1);
}
adapter.setItems(items);
Code of adapter:
public class BaseSectionAdapter extends AmazingAdapter {
private LayoutInflater inflater;
private int selectedItem = 0;
private List<Object> items;
private List<SectionItem> sections = new ArrayList<SectionItem>(10);
private List<Class> itemTypes = new ArrayList<Class>();
private List<Integer> sectionPositions = new ArrayList<Integer>();
private int listHeaderLayoutId;
private View headerView;
public static interface ISectionListItem {
public void setProps(View convertView, int position, int selectedItem);
public View getLayout(LayoutInflater inflater);
}
private class SectionItem implements Serializable {
private static final long serialVersionUID = -8930010937740160935L;
String text;
int position;
public SectionItem(String text, int position) {
this.text = text;
this.position = position;
}
}
public BaseSectionAdapter(Context context, int listHeaderLayoutId) {
this.listHeaderLayoutId = listHeaderLayoutId;
init(context);
}
public BaseSectionAdapter(Context context, int listHeaderLayoutId, List<Object> listItems) {
this.listHeaderLayoutId = listHeaderLayoutId;
init(context);
initListItems(listItems);
}
private void init(Context context) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void setSelectedItem(int position) {
selectedItem = position;
}
// public List<ListItem> getItems() {
// return items;
// }
private void initListItems(List<Object> itemList) {
int curSection = -1;
//int curPosition = 0;
//curSection = 0;
this.items = itemList;
itemTypes.clear();
sections.clear();
sectionPositions.clear();
int listSize = itemList.size();
for(int i = 0; i < listSize; i++){
Object currentItem = items.get(i);
if(currentItem instanceof String){
sections.add(new SectionItem((String) currentItem,i));
curSection++;
}
if(!itemTypes.contains(currentItem.getClass())){
itemTypes.add(currentItem.getClass());
}
sectionPositions.add(curSection);
}
Log.d("test", "No of items = "+items.size());
Log.d("test", "No of itemtypes = "+itemTypes.size());
Log.d("test", "View type count = "+getViewTypeCount());
}
public void setItems(List<Object> itemList) {
initListItems(itemList);
}
public int getCount() {
return items==null?0:items.size();
}
#Override
public int getViewTypeCount(){
return (itemTypes.size() == 0)?1:itemTypes.size();
}
#Override
public int getItemViewType(int position){
return itemTypes.indexOf(items.get(position).getClass());
}
#Override
public boolean isEnabled(int position){
return !(items.get(position) instanceof String || items.get(position) instanceof EmptySectionListItem);
}
#Override
public Object getItem(int position) {
return items.get(position);
}
public long getItemId(int position) {
return position;
}
#Override
protected void onNextPageRequested(int page) {
// TODO Auto-generated method stub
}
#Override
protected void bindSectionHeader(View view, int position,
boolean displaySectionHeader) {
// TextView lSectionTitle = (TextView) view
// .findViewById(R.id.txt_list_header);
// if (displaySectionHeader) {
// lSectionTitle.setVisibility(View.VISIBLE);
// lSectionTitle
// .setText(getSections()[getSectionForPosition(position)]);
// } else {
// lSectionTitle.setVisibility(View.GONE);
// }
}
#Override
public View getAmazingView(int position, View convertView, ViewGroup parent) {
Object curItemObject = items.get(position);
boolean isHeader = (curItemObject instanceof String);
if(convertView == null){
if(isHeader && headerView != null){
convertView = headerView;
}else if(isHeader){
convertView = inflater.inflate(listHeaderLayoutId, null);
headerView = convertView;
}else{
convertView = ((ISectionListItem) curItemObject).getLayout(inflater);
}
}
if(isHeader){
TextView header = ((TextView)convertView.findViewById(R.id.txt_list_header));
header.setText((String)curItemObject);
}else{
((ISectionListItem)curItemObject).setProps(convertView, position, selectedItem);
}
return convertView;
}
#Override
public void configurePinnedHeader(View header, int position, int alpha) {
TextView textView = ((TextView)header.findViewById(R.id.txt_list_header));
textView.setText(getSections()[getSectionForPosition(position)]);
}
#Override
public int getPositionForSection(int section) {
if(section >= sections.size()){
return 0;
}
return sections.get(section).position;
}
#Override
public int getSectionForPosition(int position) {
return sectionPositions.get(position);
}
#Override
public String[] getSections() {
String[] res = new String[sections.size()];
for (int i = 0; i < res.length; i++) {
res[i] = sections.get(i).text;
}
return res;
}
}
Code of layout:
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
FrameLayout listLayout = new FrameLayout(this);
LinearLayout.LayoutParams listParams = new LinearLayout.LayoutParams(0, FrameLayout.LayoutParams.MATCH_PARENT);
listParams.weight = 1;
listLayout.setId(LIST_FRAGMENT_VIEW_ID);
FrameLayout detailLayout = new FrameLayout(this);
LinearLayout.LayoutParams detailParams = new LinearLayout.LayoutParams(0, FrameLayout.LayoutParams.MATCH_PARENT);
detailParams.weight = 2;
detailLayout.setId(DETAIL_FRAGMENT_VIEW_ID);
layout.addView(listLayout, listParams);
layout.addView(detailLayout, detailParams);
if(savedInstanceState == null){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(listLayout.getId(), (Fragment) listFragment, TWO_PANEL_LIST_FRAGMENT_TAG);
ft.add(detailLayout.getId(), detailFragment);
ft.commit();
}
setContentView(layout);
try calling notifyDataSetChanged() in runOnUIThread() method like I have shown below and it will work like a charm. :)
runOnUiThread(new Runnable() {
#Override
public void run() {
messageAdapter.notifyDataSetChanged();
}
});
i dont know what causes the problem, but if you don't find a logical solution to it you could try something like this:
trigger an onTouchEvent() programmatically whenever you launch the ListView.
scroll down and back up programmatically as soon as the ListView is launched.
etc..
Add ListView widget to layout.xml and add content of list to that. Do not use FrameLayout as it probably is the cause of the problem. It is updating content after touch so the Layout it is on is no implementing the correct onCreate() setup as the ListView widget has.
Are you calling the method notifyDataSetChanged() on your adapter after adding new items? This causes the listview to refresh its view when the underlying dataset is changed.
If it still doesn't work, try notifyDataSetInvalidated() that causes the listview to redraw completely.
Solved it!!!
Problem was with the adapter trying to reuse the same section item. Not good!!!
Changed it to inflate the section item each time we hit a section!

Spinner doesn't look appropriate

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>

Categories

Resources