Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Dynamic Spinner

I have a Game View which triggers 1 Dialog, from here a second Dialog is triggered.

Within this second dialog, I have 2 buttons and 1 spinner. The issue I am having is getting a spinner to appear within my view, with dyamic data.

Essentially, I need to spinner to popup when the button is clicked on and list all the relevent data. I have 3 different ArrayLists (1 String and 2 integers), which hold sepearte information which will need to be dynamically added to each spinner choice e.g.

pt1 -   ArrayLists1
        ArrayLists2
        ArrayLists3
pt2 -   ArrayLists1
        ArrayLists2
        ArrayLists3

Spinner XML layout code:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal">
    <Button android:text="@string/attack" 
            android:id="@+id/Attack" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" />
    <Button android:text="@string/useitem" 
            android:id="@+id/UseItemBtn" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" />
    <Spinner android:text="@string/useitem"
            android:id="@+id/UseItemSpin"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:visibility="gone" />
    <Button android:text="@string/equipweapon" 
            android:id="@+id/EquipWeapon" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"/>
</LinearLayout>

Here is the code that I am currently using to trigger the click of the spinner (the log here is triggered):

import android.widget.AdapterView.OnItemSelectedListener;

@Override
public void onClick(View clicked)
{
    if(clicked.getId() == R.id.UseItemBtn)
    {
        Spinner spinner = (Spinner) findViewById(R.id.UseItemSpin);

        ArrayList<String> arrayList1 = new ArrayList<String>();
        arrayList1.add("test item the first one");
        arrayList1.add("really long list item - section 2 goes here - finally section 3");

        ArrayAdapter<String> adp = new ArrayAdapter<String> (context,android.R.layout.simple_spinner_dropdown_item,arrayList1);
        // APP CURRENTLY CRASHING HERE
        spinner.setAdapter(adp);
       //Set listener Called when the item is selected in spinner
       spinner.setOnItemSelectedListener(new OnItemSelectedListener() 
       {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long arg3) 
            {
                String city = "The city is " + parent.getItemAtPosition(position).toString();
                Toast.makeText(parent.getContext(), city, Toast.LENGTH_LONG).show();
            }

            public void onNothingSelected(AdapterView<?> arg0) 
            {
                // TODO Auto-generated method stub
            }
        });
    }
    BattleRun.show();
}

How can I add the dynamic spinner on this click. On top of this when the spinner is clicked, ideally I would like a submit button within the actual spinner so they can flick through the choices before selecting (which should be visible at all times).

Finally, when the item has been selected in the spinner and submitted the previous dialog will automatically refresh with the new data (this I can do easy enough).

If you require any more code snippets please let me know, any help or guidence would be much appreciated.

Thanks, L & L Partners

like image 639
Raj Avatar asked Jul 19 '12 16:07

Raj


1 Answers

You can use this code to populate your spinner with the string array list -

    public void onClick(View clicked){
    if(clicked.getId() == R.id.Attack){
        Spinner spinner = (Spinner) findViewById(R.id.UseItem);

      //Sample String ArrayList
        ArrayList<String> arrayList1 = new ArrayList<String>();

        arrayList1.add("Bangalore");
        arrayList1.add("Delhi");
        arrayList1.add("Mumbai");
        ArrayAdapter<String> adp = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_dropdown_item,arrayList1);
        spinner.setAdapter(adp);

        spinner.setVisibility(View.VISIBLE);
       //Set listener Called when the item is selected in spinner
       spinner.setOnItemSelectedListener(new OnItemSelectedListener() 
       {
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long arg3) 
            {
                String city = "The city is " + parent.getItemAtPosition(position).toString();
                Toast.makeText(parent.getContext(), city, Toast.LENGTH_LONG).show();

            }

            public void onNothingSelected(AdapterView<?> arg0) 
            {
                // TODO Auto-generated method stub
            }
        });

        //BattleRun.dismiss();
        Log.d("Item","Clicked");

    }
}

This code will go inside your layout xml file -

<!-- Add onClick attribute on this button -->
<Button android:text="@string/attack" 
        android:id="@+id/Attack" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:onClick="onClick" />
<Spinner android:text="@string/useitem" 
        android:id="@+id/UseItem" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:visibility="gone" />
<Button android:text="@string/equipweapon" 
        android:id="@+id/EquipWeapon" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"/>

Note that you are not adding onClick inside the xml file. That's why your onClick function is not getting called. You can also use the string-array in strings.xml file for using static list.

like image 103
Sonam Daultani Avatar answered Oct 30 '22 02:10

Sonam Daultani