Loading...

Thursday, 9 August 2012

Add Spinner (Android)

package app.Test;

import java.util.ArrayList;
import java.util.List;

import com.test.android.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class appTest extends Activity {
    private Spinner spinner1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        List<String> list = new ArrayList<String>();       
        list.add(" Test 1 ");
        list.add(" Test 2 ");
        list.add(" Test 3 ");    
        list.add(" Test 4 ");
        list.add(" Test 5 ");
        list.add(" Test 6 ");
        list.add(" Test 7 ");
       
        addItemsOnSpinner(list);
    }
    private void addItemsOnSpinner1(List<String> list) {

        spinner1 = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(dataAdapter);
        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){           
            public void onItemSelected(AdapterView<?> parent, View view, int pos,
                    long id) {
                Toast.makeText(this, "Possition:" + pos, Toast.LENGTH_SHORT).show();
            }
            public void onNothingSelected(AdapterView<?> arg0) {
                // if not select
            }
        });
    }   
       
}

No comments:

Post a Comment