1

I want to perform a click on a Spinner element automatically after my activity has been loaded completely.

I use this code to set up the spinner and adapter:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read_data);
        getActionBar().setDisplayHomeAsUpEnabled(true);

        mSpinnerDay = (Spinner) mTable.findViewById(R.id.spieltag_choice);
        mAdapterSpinnerDay = new ArrayAdapter<CharSequence>(this, R.layout.custom_spinner);
        mAdapterSpinnerDay.setDropDownViewResource(R.layout.custom_spinner);
        mSpinnerDay.setAdapter(mAdapterSpinnerDay);


}


    private void setUpSpinnerListener(final IGameData data) {
    mSpinnerDay.post(new Runnable() {
        public void run() {
        mSpinnerDay.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            for (GameDayData d : data.getGameDay()) {
                if (d.getName().equals(adapterView.getSelectedItem().toString())) {
                TableRow row = (TableRow) mTable.findViewById(R.id.row_punkte_tag);
                TextView t = (TextView) row.findViewById(R.id.punkte_tag);
                t.setText("Punkte: " + d.getScore());
                TableRow row2 = (TableRow) mTable.findViewById(R.id.row_position_tag);
                TextView t2 = (TextView) row2.findViewById(R.id.position_tag);
                t2.setText("Position: " + d.getPosition());
                return;
                }
            }
            }

            public void onNothingSelected(AdapterView<?> adapterView) {
            return;
            }
        });
        }
    });
}

public void onTeamCheckReadComplete(IGameData data) {
    for (GameDayData d : data.getGameDay()) {
        mAdapterSpinnerDay.add(d.getName());
    }
}

I try to perform the click with following code after I have set the adapter to the spinner:

mSpinnerDay.setSelection(0, true);
    View view = (View)  mSpinnerDay.getChildAt(0);
    long id = mSpinnerDay.getAdapter().getItemId(0);
    mSpinnerDay.performItemClick(view, 0, id);

But this does not work. Could somebody tell me how I can perfom a click on a spinner element automatically? When I select the spinner item over touch event in the application everything works fine.

Regards, Sandro

Goo
  • 1,318
  • 1
  • 13
  • 31
sk2212
  • 1,688
  • 4
  • 23
  • 43

2 Answers2

0

I dont think you are calling setUpSpinnerListener from the code that you have posted. Even if you are calling it, i dont think it is advantageous to post a runnable to setuplistener.

Move all the code in runnable to just after setAdapter

nandeesh
  • 24,740
  • 6
  • 69
  • 79
  • I call setUpSpinnerListener after setAdapter. The advantage of a post runnable is that the code in the onItemSelected will not be executed twice. I hope you know that this is a issue of spinner elements. Just search here in stackoverflow. – sk2212 Sep 15 '12 at 16:19
  • http://stackoverflow.com/questions/2562248/android-how-to-keep-onitemselected-from-firing-off-on-a-newly-instantiated-spin – sk2212 Sep 15 '12 at 16:27
  • oh ok, i did not know of that. Anyway are you executing your performitemclick after some Ui interaction or just after setting the adapter, Maybe the setonitemclicklistener has not executed when you call performitemclickk – nandeesh Sep 15 '12 at 16:38
  • You say that performitemclick generally has to work for my use case? – sk2212 Sep 15 '12 at 16:49
  • i meant, when are you calling performitemclick? if it is just after setAdapter, the runnable that you are posting will not have run, So post another runnable with performitemclick – nandeesh Sep 15 '12 at 16:54
0

Corrected Solution As I understand it, you have a spinner with items A, B, C, and D. You want item A pre-selected. You also want the user to be able to select A, B, C, and D and perform an action based on that.

In the onCreate() method:

mSpinner.setAdapter(myAdapter);
mSpinner.setOnItemSelectedListener(this); // have the activity implement
                                          // OnItemSelectedListener interface
doAction(0);

Then implement the onItemSelected action like so:

@Override
void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    doAction(position);
}

You must implement the doAction(int position) method to handle how to update your activity based on the position of the item in your adapter. Clear?

Read more about this item and how to use it here: http://developer.android.com/reference/android/widget/AdapterView.OnItemSelectedListener.html

Cookster
  • 1,463
  • 14
  • 21
  • This does the follow: 1. User selects empty drop down list 2. Shows a "Loading" progress dialog 3. In the background it fetches a list of items 4. When the list is completed, dismisses the loading dialog and populates the adapter 5. When adapter is populated, performing a "click" will pull up the list for the user to select. – Cookster Sep 15 '12 at 16:42
  • Hmm...the use case is that I set up data in a spinner adapter and also create some code in the onItemSelected method which is executed when a user select a spinner item. Now I want that this user action is performed automatically to a spinner element after the UI has been created. I just want to use code within onItemSelected method execute automatically on a special spinner element. Do you understand my problem now? – sk2212 Sep 15 '12 at 16:49
  • You just want to execute code based on which element the user selected? – Cookster Sep 15 '12 at 16:53
  • No, I want just to execute code from onItemSelected method without a user action. After the UI has been created a element in the spinner is shown. Now I want to execute onItemSelected on this preselected spinner item AUTOMATICALLY. – sk2212 Sep 15 '12 at 17:00
  • Could you provide a solution ;-)? – sk2212 Sep 15 '12 at 17:13
  • Hopefully that answers your question. – Cookster Sep 15 '12 at 18:30