Mounika
Mounika

Reputation: 109

Spinner not showing the selected item

PROBLEM

Whenever I write the code for spinner it is not displaying the selected item. It is just showing the toast of that selected item.

    public class Retailormainhomepage extends Activity implements AdapterView.OnItemSelectedListener {

EditText mname;
TextView mcategory;
EditText mproduct;
Button mupload,mproducts;
String enteredRetailorname,enteredCategory,enteredProduct;
String enteredid,enteredRetailor_name,enteredCategory_id,enteredProduct_name;
String jsonresponce=null;
String UPLOAD_URL,PRODUCTS_URL;
ProgressDialog pd;
ImageView mimageback;

InputStream is = null;

private Spinner spinner;
String result = null;

List<String> list;
boolean connection = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_retailorhomepage);
    mimageback = (ImageView)findViewById(R.id.imageback);
    mname = (EditText)findViewById(R.id.rname);
    mcategory = (TextView)findViewById(R.id.category);
    mproduct  = (EditText)findViewById(R.id.product);
    mupload = (Button)findViewById(R.id.upload);
    spinner  = (Spinner)findViewById(R.id.spinner);
    spinner.setOnItemSelectedListener(Retailormainhomepage.this);
    // Spinner Drop down elements
    List<String> categories = new ArrayList<String>();
    categories.add("Automobile");
    categories.add("Business Services");
    categories.add("Computers");
    categories.add("Education");
    categories.add("Personal");
    categories.add("Travel");

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

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

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

.

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    String item = parent.getItemAtPosition(position).toString();

    // Showing selected spinner item
    Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}

XML File:

This is my xml file for the same requirement.I didn't get any logcat error for this just it is not showing in edit text the selected item.

  <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="54dp"
        android:layout_marginTop="22dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:weightSum="1">

        <TextView
            android:layout_width="125dp"
            android:layout_height="40dp"
            android:text="category"
            android:id="@+id/category"
            android:textColor="#000000"
            android:layout_below="@+id/textView"
            android:layout_alignParentLeft="true"
            android:textSize="20dp"/>

        <EditText
            android:layout_width="173dp"
            android:layout_height="40dp"
            android:id="@+id/etcategory"
            android:hint="1,2,..."
            android:layout_marginLeft="20dp"
            android:textColor="#000000"
            android:layout_alignTop="@+id/textView2"
            android:layout_alignParentRight="true"
            android:textSize="15dp"
            />

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/category"
            android:layout_centerHorizontal="true"
           android:layout_marginBottom="10dp" />

    </LinearLayout>

Upvotes: 1

Views: 7577

Answers (3)

Jay Rathod
Jay Rathod

Reputation: 11245

Try this.

@Override public void onItemSelected(AdapterView parent, View view, int position, long id) {

    String item = parent.getItemAtPosition(position).toString();

    // Showing selected spinner item
    Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();

    spinner.setSelection(position);

}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}

Looking like you need to set value using setSelection method of spinner.

EDIT:

For multiple select items check below reference link.

https://trinitytuts.com/tips/multiselect-spinner-item-in-android/

https://asnehal.wordpress.com/2012/04/03/multi-select-drop-down-list-in-android/

Upvotes: 1

Mounika
Mounika

Reputation: 109

Yes, I solved my issue but still a small doubt i.e., am not able to select multiple items at a time. Is there any other code for that .

Upvotes: 0

Keval Patel
Keval Patel

Reputation: 592

Try following:

@Override public void onItemSelected(AdapterView parent, View view, int position, long id) { String item = parent.getItemAtPosition(position).toString();

// Showing selected spinner item
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();

//set as selected item.
spinner.setSelection(position)
}

For more info visit documentation.

Upvotes: 1

Related Questions