【问题标题】:Spinner with Key-Value Pair具有键值对的微调器
【发布时间】:2013-09-10 10:55:28
【问题描述】:

我正在开发多语言用户界面。我的要求是微调器,我想用印地语显示数据,但是当它被选中时,它应该返回英文显示,它可以与进一步的决策进行比较。就像带标签的标签一样。

我的java代码是这样的

    HashMap<String,String> options=new HashMap<String,String>();
    String optionsEnglish [] = getResources().getStringArray(R.array.option_array);
    String optinsHindi[]= getResources().getStringArray(R.array.option_array_hindi);

    for(int i=0;i<optionsEnglish.length;i++)
    {
        options.put(optionsEnglish[i], optinsHindi[i]);
    }
    Spinner optionSpinner = (Spinner) findViewById(R.id.optionPicker);


    ArrayAdapter<HashMap<String, String>> dataAdapter = new ArrayAdapter<HashMap<String,String>>(this, android.R.layout.simple_spinner_item);
    dataAdapter.add(options);


    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    optionSpinner.setAdapter(dataAdapter);

在xml中

    <resource>       
            <string-array name="option_array">
                  <item>Market</item>
                  <item>Commodity</item>
            </string-array>

            <string-array name="option_array_hindi">
                  <item>बाजार</item>
                  <item>वस्तु</item>


            </string-array>

     </resources>

【问题讨论】:

  • 那么你的问题是什么??
  • 我没有得到两个选项 बाजार、वस्तु 的下拉菜单,而不是 {Market-बाजार、Commodity-वस्तु} 打印为一个选项
  • 我想要两个带有标签 बाजार、वस्तु 的选项以及何时选择 बाजार 市场应该返回,当 वस्तु 所选商品应该返回时,我可以对 itemSelected 事件的某些操作做出正确的决定
  • 我知道键/值的这个功能在 Android 中不可用。所以我已经实现了我发布的代码,使用该代码可能对你有帮助。因为我有相同的要求我这样做了
  • 为什么不能在 spinner 的 onItemSelected() 方法中设置文本?

标签: android spinner


【解决方案1】:

第 1 步:创建 POJO 类来处理键和值

  public class Country {
      private String id;
      private String name;
      public Country(String id, String name) {
      this.id = id;
      this.name = name;
     }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    //to display object as a string in spinner
    @Override
    public String toString() {
        return name;
    }

    @Override
    public boolean equals(Object obj) {
        if(obj instanceof Country){
            Country c = (Country )obj;
            if(c.getName().equals(name) && c.getId()==id ) return true;
        }

        return false;
    }

  }

第 2 步:准备要加载到微调器中的数据

private void setData() {

        ArrayList<Country> countryList = new ArrayList<>();
        //Add countries

        countryList.add(new Country("1", "India"));
        countryList.add(new Country("2", "USA"));
        countryList.add(new Country("3", "China"));
        countryList.add(new Country("4", "UK"));

        //fill data in spinner 
        ArrayAdapter<Country> adapter = new ArrayAdapter<Country>(context, android.R.layout.simple_spinner_dropdown_item, countryList);
        sp_country.setAdapter(adapter);
        sp_country.setSelection(adapter.getPosition(myItem));//Optional to set the selected item.    
    }

第三步:最后在spinner的onitemselected监听方法中获取选中项的key和value

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

                 Country country = (Country) parent.getSelectedItem();
                 Toast.makeText(context, "Country ID: "+country.getId()+",  Country Name : "+country.getName(), Toast.LENGTH_SHORT).show();    
            }

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

【讨论】:

  • 但它在下拉列表中显示整个数组,如下所示:DepartmentSel(id=5, name=DepartmenName)
  • @haidarvm, 添加这个 // 将对象显示为微调器中的字符串 @@Override // 跳过一个 @ public String toString() { return name; }
【解决方案2】:

希望对你有帮助

Spinner 中添加数据

private void setDataInSpinner(Spinner id, int dataArray) {
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, dataArray, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
        id.setAdapter(adapter);
    }

要获得 Spinner 的 selected 值,请使用此

Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
String Text = mySpinner.getSelectedItem().toString();

或者你可以使用setOnItemSelectedListener来获取选中的值

String value = GetClassCode.getCode(Text);//here u have to pass the value that is selected on the spinner

创建一个

public class GetClassCode {
    static HashMap<String, String> codeHash = new HashMap<String, String>();

    static {
        init();
    }

    public static void init() {
        codeHash.put("key", "value");
        codeHash.put("key", "value");
        codeHash.put("key", "value");
        codeHash.put("key", "value");

    }

    public static String getCode(String param) {
        return codeHash.get(param);
    }
}

【讨论】:

  • 我不知道如何将它添加到微调器中,我们可以给我完整的代码
  • 查看微调器的选择 将数据传递给函数 GetClassCode 在该 codeHash 散列中将键设置为 बाजार 并将值设置为 Market 所以当他选择 बाजार 这个类将返回 Market it very简单
  • 你有问题还是有问题
  • 感谢您的回答。从您的代码中,getCode(param) 将仅返回 null,因为对于 hashMap get(param) 方法,如果参数是键,则返回 hashmap 的值。因此,获取密钥的方法应该是 public static String getCode(String param) { for (String keyVlue : codeHash.keySet()) { if (codeHash.get(keyVlue).equals(param)) { return keyVlue; } } 返回空值; }
【解决方案3】:

我创建了一个可能有帮助的 HashMap adapter。另见示例项目here

    mapData = new LinkedHashMap<String, String>();

    mapData.put("shamu", "Nexus 6");
    mapData.put("fugu", "Nexus Player");
    mapData.put("volantisg", "Nexus 9 (LTE)");
    mapData.put("volantis", "Nexus 9 (Wi-Fi)");
    mapData.put("hammerhead", "Nexus 5 (GSM/LTE)");
    mapData.put("razor", "Nexus 7 [2013] (Wi-Fi)");
    mapData.put("razorg", "Nexus 7 [2013] (Mobile)");
    mapData.put("mantaray", "Nexus 10");
    mapData.put("occam", "Nexus 4");
    mapData.put("nakasi", "Nexus 7 (Wi-Fi)");
    mapData.put("nakasig", "Nexus 7 (Mobile)");
    mapData.put("tungsten", "Nexus Q");

    adapter = new LinkedHashMapAdapter<String, String>(this, android.R.layout.simple_spinner_item, mapData);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinner = (Spinner) findViewById(R.id.spinner);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);

【讨论】:

    【解决方案4】:

    您为给定 xml 中的语言单词创建了两个单独的字符串数组,对吗? 然后只需将 ArrayAdapter 用于第一个选项数组并在微调器中设置适配器。并根据它的位置从第二个字符串数组中选择任何微调器项目..

    这将非常简单,并且适用于超过 2 种语言。

    【讨论】:

      【解决方案5】:

      一个有点老的问题,但这是从谷歌搜索时的最佳答案。

      我用了这个方法:

      从微调器中获取本地化值:

      String localizedValue = ((Spinner) findViewById(R.id.mySpinner)).getSelectedItem().toString();    
      

      然后从字符串资源文件中动态获取key:

      String key = (String) getResources().getText(getResources().getIdentifier(localizedValue, "string", "my.package.name"));    
      

      所有语言都应在 strings.xml 中包含本地化值的键:

      बाजार=Market
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-21
        • 2021-10-18
        • 2014-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多