سؤال

I want to build android app with spinner , I would like to appear in spinner some text , and when selected, it appears in TextView other text than the value of which is a selected. for example , i want to appear in spinner website name (like= stackoverflow) and when select appear in TextView the website URL (like=https://stackoverflow.com/)

can anyone help me?

thanks in advance

هل كانت مفيدة؟

المحلول 2

You can add text to spinner like this:

Spinner spinner = (Spinner) findViewById(R.id.spinner);

List<String> texts = new ArrayList<String>();
texts.add("Google");
texts.add("Facebook");

List<String> urls = new ArrayList<String>();
urls.add("www.google.com");
urls.add("www.facebook.com");

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, texts);
spinner.setAdapter(adapter);

then you need to listen for item selected on spinner

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        String url = urls.get(i);
        // do with url whatever you need
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {
        // do nothing
    }
});

نصائح أخرى

First, you create yourself an Object:

public class LanguageObject {
    private String title;
    private String url;

    public LanguageObject(String title, String url) {
        this.title = title;
        this.url = url;
    }

    public String getTitle() {
        return title;
    }

    public Integer getUrl() {
        return url;
    }

}

Then, in your Activity/Fragment, you create an ArrayList with the objects you need:

ArrayList<SpinnerObject> spinnerObjectList = new ArrayList<SpinnerObject>();
        spinnerObjectList.add(new SpinnerObject("Google", "www.google.com"));
        spinnerObjectList.add(new SpinnerObject("StackOverflow", "www.stackoverflow.com"));

In addition to that, create a custom adapter that will handle your data:

public class SpinnerAdapter extends ArrayAdapter<SpinnerObject> {
public SpinnerAdapter (Context context, int textViewResourceId, ArrayList<SpinnerObject> spinnerObjects) {
    super(context, textViewResourceId, spinnerObjects);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    SpinnerObject spinnerObject = getItem(position);

    if(view == null) {
        LayoutInflater layoutInflater;
        layoutInflater = LayoutInflater.from(getContext());

        view = layoutInflater.inflate(android.R.layout.select_dialog_item, null);

        if (view != null) {
            TextView textView = (TextView) view.findViewById(android.R.id.text1);
            textView.setText(spinnerObject.getTitle());
        }
    }

    return view;
}

Now you can use this adapter on your Spinner, and get the url when one of the items was clicked.

For adding text in spinner you can use following code in onCreate()

Spinner locale;
ArrayList<String> lList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


     locale =(Spinner)findViewById(R.id.Cmblocale);

    // bind data to Spinner(ComboBox)
    lList =new ArrayList<String>();
    lList.add("English");
    lList.add("Canada");
    lList.add("Chinese");
    lList.add("French");
    lList.add("Germany");
    lList.add("Japan");
    ArrayAdapter<String> cAdapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item,lList);

    locale.setAdapter(cAdapter);
}

For Retrieving value use following code inside your setOnItemSelectedListener on spinner

if(locale.getSelectedItem().equals("English"))
    {
        txtText.setText(English);
    }else if(locale.getSelectedItem().equals("Chinese"))
    {
        txtText.setText(Chinese);
    }else if(locale.getSelectedItem().equals("Canada"))
    {
        txtText.setText(Canada);
    }else if(locale.getSelectedItem().equals("French"))
    {
        txtText.setText(French);
    }else if(locale.getSelectedItem().equals("Germany"))
    {
        txtText.setText(Germany);
    }else if(locale.getSelectedItem().equals("Japan"))
    {
        txtText.setText(Japan);
    }
Spinner spinner = (Spinner) findViewById(R.id.spinner); //create a spinner view in XML

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
    R.array.your_array, android.R.layout.simple_spinner_item); // create adapter which has second parameter as the array which contains the values you want to show as entries of spinner
spinner.setAdapter(adapter); // set the adapter to your spinner and your spinner will show the values from array that you set in second parameter of adapter

To listen to the click on the options of the spinner

public class SpinnerActivity extends Activity implements OnItemSelectedListener {
...

public void onItemSelected(AdapterView<?> parent, View view, 
        int pos, long id) {
    // An item was selected. You can retrieve the selected item using
    // parent.getItemAtPosition(pos)
}

public void onNothingSelected(AdapterView<?> parent) {
    // Another interface callback
}
}

Now to show the appropriate link of the website for selected option from spinner you can have a map which has keys as same as the spinner values and values as websites as you want....

HashMap<String, String> nameWebsiteMap = new HashMap<String, String>();
nameWebsite.put("stack overflow","http://stackoverflow.com/"); // for example

So in onItemSelected you can simply do this

selectedSiteName = nameWebsite.get(parent.getItemAtPosition(pos).toString());
textView.setText(selectedSiteName);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top