Use of Spinner widget in android studio

By this tool I made to get table of any number.

MyAdapter.java-

public class MyAdapter implements SpinnerAdapter {
    Context context;
int n;
    public MyAdapter(Context context,int n) {
        this.context = context;
        this.n=n;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        Button button=new Button(context);
        button.setText("" + ( position + 1)*n);

        return button;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public int getCount() {
        return 10;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Button button=new Button(context);
        button.setText("" + ( position + 1)*n);

        return button;
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }
}

MainActivity.java-

public class MainActivity extends AppCompatActivity {
    EditText e;
    Spinner spinner;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        e = (EditText) findViewById(R.id.editText);
        spinner = (Spinner) findViewById(R.id.spinner);
    }
public void perform(View view) {
    try {
        int n = Integer.parseInt("" + e.getText());
        spinner.setAdapter(new MyAdapter(this, n));
    }catch (Exception ex){
        System.out.println(ex);
    }
}
}



Result-   

Leave a Reply