0

I have form with a spinner control and a text box; and tables are stored in the SQLite database.

I am populating spinner inside onCreate method of activity. Is it right place to call database query & update ui?

Depending upon value user selects from spinner, i have to give auto suggestion in text box. (which control i can use instead of text box, i want to force user to select from list coming from database.)

Also i have heard about fragment, can anyone guide me is it helpful for improving performance of the application.

Rutu Raj
  • 839
  • 1
  • 6
  • 5

2 Answers2

1

it would be better u call a function inside onCreate method and fill the spinner value in that function like

 protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.spinner);

    addItemsOnSpinner1();
}

 public void addItemsOnSpinner1() {

    Spinner spinner = (Spinner) findViewById(R.id.spinnerSource);
    List<String> list = new ArrayList<String>();
    list.add("sony");
    list.add("apple");
    list.add("samsung");
    list.add("htc");
    list.add("blackberry");
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    Collections.sort(list);
    spinner.setAdapter(dataAdapter);

And for using fragment use this linkWhen should I use fragments in Android applications ? Why to use fragments?

Community
  • 1
  • 1
Avinash Kumar Pankaj
  • 1,700
  • 2
  • 18
  • 27
0

You must populate your spinner with data from that moment on you want to provide it to the user. So just in the onCreate of the containing Activity.

For more info I guess Spinner | Android Developers .

Fragments give you the ability to create independent parts for your UI. For example a Fragment which contains a List and another showing detail information to the selected element. On Tablet you could display both beside each other. On a small display handheld device you have to display the list and on item selection the details. With Fragments you have reusability. Take a look here Fragments | Android Developers - I recommend to read this if you will use Fragments. It will save you the trouble.

sascha10000
  • 1,245
  • 1
  • 10
  • 22