samedi 18 juillet 2015

How to populate a spinner in an android fragment using sqlite db and from the main activity

What i am doing in code is as follows -

Spinner spinnertest;
Spinner spinnertwo;
LayoutInflater inflater;
ViewGroup container;
    inflater = this.getLayoutInflater();
    container = (ViewGroup) ((ViewGroup) this
            .findViewById(android.R.id.content)).getChildAt(0);
    View rootView = inflater.inflate(R.layout.fragment1, container, false);
    spinnertwo = (Spinner)rootView.findViewById(R.id.spinner2);
    spinnertest = (Spinner)findViewById(R.id.spinnert);
    DBHandler db = new DBHandler(getApplicationContext());
    db.insertLabel("facebook");
    db.insertLabel("linkedin");
    db.insertLabel("twitter");
    // Spinner Drop down elements
    List<String> lables = db.getAllLabels();

    // Creating adapter for spinner
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, lables);

    // Drop down layout style - list view with radio button
    dataAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // attaching data adapter to spinner
    spinnertest.setAdapter(dataAdapter);
    spinnertwo.setAdapter(dataAdapter);

Here spinnert is a spinner in the main activity but spinner2 is in one of four Fragmnents - the 1st one.

spinnert is getting populated by this code fine but not spinner2. Can anybody say why plz and how it is to be done?

Aucun commentaire:

Enregistrer un commentaire