1

I want to Convert the Languages. So i am using two Spinners one is "From Language" and Another one is for "To Language". If One Language is Selected in "From Language" Spinner, it shouldn't display (or it should be disabled) in 2nd spinner. how can i achieve it?

Ex. if i Select English in 1st Spinner, 2nd Spinner Shouldn't display English in its dropdown.

Sindhoor
  • 514
  • 3
  • 14

2 Answers2

0

This is may not be the best way try this.

 ArrayList<String> languages = new ArrayList<>();
    languages.add("English");
    languages.add("Hindi");
    languages.add("Telugu");
    languages.add("Tamil");
    languages.add("Kannada");
    languages.add("Malayalam");
    // make a array list of languages
    String option1 = null;
    Spinner spinnerOption1 = (Spinner) findViewById(R.id.spinner1);
    final ArrayAdapter<String> adapterOpton1 = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, languages);
    spinnerOption1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerOption1.setAdapter(adapterOpton1);

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

            option1 = adapterOpton1.getItem(position);
        }

    });

    int selectedIndex;

    for (String item : languages) {
        if (item.equals(option1)) {
            selectedIndex == languages.indexOf(item);
        }
    }
    ArrayList<String> languages2 = languages;
    languages2.remove(selectedIndex);

    Spinner spinnerOption2 = (Spinner) findViewById(R.id.spinner2);

    final ArrayAdapter<String> adapterOption2 = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, languages2);
    spinnerOption2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerOption2.setAdapter(adapterOption2);

Explanation: lets create a arraylist with languages bind it to the adapter on the spinner, on selection to the spinner one keep a track of that selection, then find the index of the selection in the arraylist. create second arraylist with the same languages and find and remove the user selected item, create an adapter and bind the data.

Hope it helps.

Irfan
  • 956
  • 9
  • 16
0

Use Hashmaps it will be easier. Create an Adapter that uses Key Values for populating adapter.

This is a snippet I found from another link on how to do that, in case you are not familiar

   public class HashMapAdapter extends BaseAdapter {

    private HashMap<String, String> mData = new HashMap<String, String>();
    private String[] mKeys;
    public HashMapAdapter(HashMap<String, String> data){
        mData  = data;
        mKeys = mData.keySet().toArray(new String[data.size()]);
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(mKeys[position]);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(int pos, View convertView, ViewGroup parent) {
        String key = mKeys[pos];
        String Value = getItem(pos).toString();

        //do your view stuff here

        return convertView;
    }
}

Credit What adapter shall I use to use HashMap in a ListView

Now for your management of the adapters.

LanguageOneMap.put (all your keys 0-whatever) value (english-whatever)
LanguageTwoMap.put (same as above)
LanguageAllMap.put (same as above)

Adapter 1 selects Language Callback(languageSelectedFromOneKey){
      LanguageTwoMap.clearAll
      LanguageTwoMap.put (all again)
      LanguageTwoMap.remove(languageSelectedFromOneKey)
      LanguageTwoAdapter.notifyDataSetChanged()
}

The above is just pseudo code meant to give the idea, not exact copy and paste. Hope that is enough to get you going. There are many ways to skin this cat, you could even use the same list for both adapters. Then when one is selected from one or the other, set a property of "selectedOtherLanguage" in the opposite adapter, then in the GetView method if data.get(pos) == selectedFromOtherListItem return, don't draw.

Many ways to do this, just a matter of how you want to do it. Goodluck.

Sam
  • 5,342
  • 1
  • 23
  • 39