Steve
Steve

Reputation: 1153

Spinner items not displayed in spinner and spinner onClick listener not working

I am getting values from json string and add it in arraylist getSpinArrList.I have posted the relevant code.

My issue is, Spinner not showing the first item and OnItemClick is not working in spinner.

But If I click spinner, I can view only the items I am getting from arraylist.

MainActivity.java:

 Spinner spinCrePage;

spinCrePage = (Spinner) findViewById(R.id.sp_create_page);

 ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, getSpinArrList);

adapter_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinCrePage.setAdapter(adapter_state);

spinCrePage.setOnItemSelectedListener(this);


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

        Toast.makeText(getApplicationContext(),"Working" , Toast.LENGTH_SHORT).show();

        spinCrePage.setSelection(position);
        String selState = (String) spinCrePage.getSelectedItem();
    //  textView.setText("Selected Android OS:" + selState);

    }

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

    }

Upvotes: 1

Views: 1913

Answers (3)

Akhil Soman
Akhil Soman

Reputation: 2217

In your application, i think the only time when the toast doesn't appear is when you are selecting the same item twice consecutively. I tried out your code and it works fine for me. You might want to remove the

spinCrePage.setSelection(position);

from your code. It doesn't have any use at all. The OnItemSelected() will be triggered only when you select a different item from the previous item in the Spinner.

And as far as the problem of "Spinner not showing the first item", you should probably check the contents of your arraylist. See if anything is getting removed during execution or something. Check loops with which you are adding data into arraylist.

"OnClick not working" : I am pretty sure that the Spinner doesn't have an OnClickListener, Spinners uses OnItemSelectedListener().

Upvotes: 0

Sukhbir
Sukhbir

Reputation: 1608

please Use this code and Follow these steps

  1. List of Items in Spinner Open “res/values/strings.xml” file, define the list of items that will display in Spinner (dropdown list).

File : res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
 <resources>
<string name="app_name">MyAndroidApp</string>
<string name="country_prompt">Choose a country</string>
<string-array name="country_arrays">
    <item>Malaysia</item>
    <item>United States</item>
    <item>Indonesia</item>
    <item>France</item>
    <item>Italy</item>
    <item>Singapore</item>
    <item>New Zealand</item>
    <item>India</item>
</string-array>

  1. Create a xml file res/layout/main.xml use country_arrays in Spinner entries
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:entries="@array/country_arrays"
    android:prompt="@string/country_prompt" />

<Spinner
    android:id="@+id/spinner2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/btnSubmit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Submit" />
</LinearLayout>
  1. Code Create Java file MyAndroidAppActivity.java
package com.webastral.drinkin.home;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class MyAndroidAppActivity extends Activity {

private Spinner spinner1, spinner2;
private Button btnSubmit;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    addItemsOnSpinner2();
    addListenerOnButton();
    addListenerOnSpinnerItemSelection();
}

// add items into spinner dynamically
public void addItemsOnSpinner2() {

    spinner2 = (Spinner) findViewById(R.id.spinner2);
    List<String> list = new ArrayList<String>();
    list.add("list 1");
    list.add("list 2");
    list.add("list 3");
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);
    dataAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(dataAdapter);
}

public void addListenerOnSpinnerItemSelection() {
    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}

// get the selected dropdown list value
public void addListenerOnButton() {

    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner2 = (Spinner) findViewById(R.id.spinner2);
    btnSubmit = (Button) findViewById(R.id.btnSubmit);

    btnSubmit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Toast.makeText(
                    MyAndroidAppActivity.this,
                    "OnClickListener : " + "\nSpinner 1 : "
                            + String.valueOf(spinner1.getSelectedItem())
                            + "\nSpinner 2 : "
                            + String.valueOf(spinner2.getSelectedItem()),
                    Toast.LENGTH_SHORT).show();
        }

    });
}

}

Create Java File CustomOnItemSelectedListener.java

package com.webastral.drinkin.home;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;

public class CustomOnItemSelectedListener implements  OnItemSelectedListener {

public void onItemSelected(AdapterView<?> parent, View view, int pos,
        long id) {
    Toast.makeText(
            parent.getContext(),
            "OnItemSelectedListener : "
                    + parent.getItemAtPosition(pos).toString(),
            Toast.LENGTH_SHORT).show();
}

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

}

Hope This wil Help you

Upvotes: 0

Nisith Kumar
Nisith Kumar

Reputation: 115

onItemSelected, you have to create a switch case using the position. For every position create a case and implement it with ur desiered code...

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
                           long id) {
    switch (position) {
        case 0:
            Toast.makeText(getApplicationContext(),"Working" , Toast.LENGTH_SHORT).show();
            break;
        case 1:
            //code
            break;

    }
}

Upvotes: 1

Related Questions