2

I have got a actionbar spinner example project. It is working fine. Now I am trying change few things like:

When I open my app the spinner shows the following:

enter image description here

Local subitem is choosen by default which is fine but I would like to change the Local text to something else when I choose item Local is this possible?

When I open the app? By default it shows the first item text like screen above. I want to change something like TITLE will be ITEMS and Sub title will be SubItems (Default selection of first Item is fine - just that I don't want to show Local text instead I want to show SubItems.

Now it shows ITEMS and Local - This Local text should only be changed when I click Local Item from the spinner.

enter image description here

The text when the spinner expands with items should be the same. like below:

enter image description here

I think it can possible in Adapter but not sure how:

Here is my customAdapter java file:

public class SubItemNavigationAdapter extends BaseAdapter 
{

    private TextView txtTitle;
    private ArrayList<SpinnerNavItem> spinnerNavItem;
    private Context context;
    private TextView txtHeading;

    public SubItemNavigationAdapter(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_navigation, null);
        }

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

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

}

Let me know!

Thanks!

TheDevMan
  • 5,914
  • 12
  • 74
  • 144
  • Checkout this: http://stackoverflow.com/questions/867518/how-to-make-an-android-spinner-with-initial-text-select-one – Hamid Shatu Apr 07 '15 at 05:50
  • I am not looking to change initial value. Initial default selection is perfect for me. I just need the text to be changed... – TheDevMan Apr 07 '15 at 05:53
  • do you want something like hint ? like an initial value shown as click here but that value shouldn't be in the list ? – Sharp Edge Apr 07 '15 at 05:55
  • Nope. Default Value should be Local but instead of local I want some other text subItem (THIS IS JUST THE TEXT) THE POSITION IT's VALUE EVERYTHING SHOULD BE LOCAL. This is one and when ever i click Local I need the following instead showing Local I need SubItem text to shown. – TheDevMan Apr 07 '15 at 05:58
  • can you post your `list_item_title_navigation.xml` ? – Sharp Edge Apr 07 '15 at 06:02

1 Answers1

1

Just use your getView() method for displaying selected dropdown items. So you will have something like txtTitle.setText(spinnerNavItem.get(position).getDisplayTitle()); // <--- extend SpinnerNavItem with displaytitle

inside your getView() method. That's it. :-)

einschnaehkeee
  • 1,858
  • 2
  • 17
  • 20