Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Position 0 is not getting selected in Spinner in Android

I have created a spinner that has three items

Daily

Weekly

Monthly

I did the following in my java file:

navSpinner = new ArrayList<SpinnerNavItem>();
        navSpinner.add(new SpinnerNavItem(getResources().getString(R.string.dailyview)));
        navSpinner.add(new SpinnerNavItem(getResources().getString(R.string.weekview)));
        navSpinner.add(new SpinnerNavItem(getResources().getString(R.string.monthview)));

        adapter = new TitleNavigationAdapter(getActivity().getApplicationContext(), navSpinner);
        mSpinner = (Spinner) rootView.findViewById(R.id.spinner);
        mSpinner.setAdapter(adapter);
        mSpinner.setOnItemSelectedListener(this);

OnItemSelected method (Implements AdapterView.OnItemSelectedListener)

public void onItemSelected(AdapterView<?> parentView, View v, int position, long id) {

        Log.e("Position", "= " + position);

        if (mNaviFirstHit)
        {
            mNaviFirstHit = false;
        }
        else
        {

            Fragment fragment = null;
            switch (position) {
                case 0:

                    Log.e("Week", "= " + position);
                    break;

                case 1:

                    backspace = 1;

                    Log.e("Week", "= " + position);

                    break;

                case 2:

                    backspace = 1;

                    Log.e("Week", "= " + position);

                    break;

                default:
                    break;
            }
        }
    }

    public void onNothingSelected(AdapterView<?> parentView) {

    }

Adapter:

public class TitleNavigationAdapter extends BaseAdapter 
{
    private TextView txtTitle;
    private ArrayList<SpinnerNavItem> spinnerNavItem;
    private Context context;
    private TextView txtHeading;
    private SharedPreferences pref;

    public TitleNavigationAdapter(Context context, ArrayList<SpinnerNavItem> spinnerNavItem) 
    {
        this.spinnerNavItem = spinnerNavItem;
        this.context = context;
    }

    @Override
    public int getCount() 
    {
        return spinnerNavItem.size();
    }

    @Override
    public Object getItem(int index) 
    {
        return spinnerNavItem.get(index);
    }

    @Override
    public long getItemId(int position) 
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
        if (convertView == null) 
        {
            LayoutInflater mInflater = (LayoutInflater)
                    context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.list_item_title, null);
        }

        txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
        txtTitle.setText(spinnerNavItem.get(position).getTitle());

        txtTitle.setTextColor(context.getResources().getColor(R.color.orangeText));
        txtTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);

        pref = context.getSharedPreferences("MyPref", Context.MODE_PRIVATE);
        String text = pref.getString("selectedItem", context.getResources().getString(R.string.transaction_main_gridview));

        if (text.equalsIgnoreCase(convertView.getResources().getString(R.string.transaction_main_gridview)))
        {
            txtTitle.setText(convertView.getResources().getString(R.string.transaction_main_gridview_text));
        }
        else if (text.equalsIgnoreCase(convertView.getResources().getString(R.string.transaction_main_weekview)))
        {
            txtTitle.setText(convertView.getResources().getString(R.string.transaction_main_weekly_text));
        }
        else if (text.equalsIgnoreCase(convertView.getResources().getString(R.string.transaction_main_monthview)))
        {
            txtTitle.setText(convertView.getResources().getString(R.string.transaction_main_monthly_text));
        }

        return convertView;
    }


    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) 
    {
        if (convertView == null) 
        {
            LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.list_item_title, null);
        }

        txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
        txtTitle.setPadding(20, 20, 0, 20);

        txtHeading = (TextView) convertView.findViewById(R.id.txtheading);
        txtHeading.setVisibility(View.GONE);

        txtTitle.setText(spinnerNavItem.get(position).getTitle());
        return convertView;
    }

}

The above code works fine when I select position 1 or 2 (I get the logs properly) then from position 1 or 2 if I select position 0 the Log is not printed at all. Position doesn't get called. Even just the position log is not getting printed.

This is really strange I am not sure why this happening? can somebody help me with this?

Thanks!

like image 917
TheDevMan Avatar asked Jul 11 '15 09:07

TheDevMan


1 Answers

If you want to make a Spinner in a Fragment, you must to declare it in Fragment onCreatedView(). Not in onCreate() of your FragmentActivity.

OnItemSelectedListener Must be implemented inside your FragmentMain not inside your Activity if you want to use the listener on the spinner inside the Fragment.

These changes to your code have been tested.

your context comes back null (in my test code anyway):

Context mContext = getActivity().getApplicationContext();// returns null !

so change to:

TitleNavigationAdapter adapter = new TitleNavigationAdapter(this,navSpinner);// added this

does your Activity implent OnItemSelectedListener ?:

        public class InformationList  extends Activity implements OnItemSelectedListener {
    .....

I would also change this in TitleNavigationAdapter:

    @Override
public SpinnerNavItem getItem(int index) //SpinnerNavItem from Object
    {
        return spinnerNavItem.get(index);
    }

Results:

07-18 19:46:53.468: E/Position(27325): = 0
07-18 19:46:53.468: E/day(27325): = 0
07-18 19:47:04.487: E/Position(27325): = 1
07-18 19:47:04.488: E/Week(27325): = 1
07-18 19:47:06.426: E/Position(27325): = 2
07-18 19:47:06.426: E/month(27325): = 2

Functioning correctly.

spinner

like image 163
Jon Goodwin Avatar answered Nov 01 '22 20:11

Jon Goodwin