【问题标题】:HashMAp in androidAndroid中的HashMAP
【发布时间】:2012-05-21 05:49:07
【问题描述】:

我在我的应用程序中使用微调器,因为我使用的是哈希表。如果用户从微调器数组中选择一个条目,则该条目的相应键值应传递到另一个屏幕。请任何人帮助我。

我的代码:

ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
                    Spinner spinner1=(Spinner)findViewById(R.id.spinner1);        
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                    this,R.array.Source, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);        
            HashMap hm = new HashMap(); 
            hm.put("Chennai","123");
            menuItems.add(hm);         
            spinner1.setAdapter(adapter);

【问题讨论】:

  • 请说明您仅将哈希表用作微调器的数据源或哈希表列表。
  • 嗨 jeet & yogesh kamaliya,我已经发布了我的代码。

标签: android hashtable


【解决方案1】:

尝试替换此代码

 HashMap hm = new HashMap(); 
            hm.put("Chennai","123");

HashMap<String, String> hm = new HashMap<String, String>(); 
            hm.put("Chennai","123");

【讨论】:

    【解决方案2】:
    sp.setOnItemSelectedListener(new OnItemSelectedListener() {
    
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    int item = sp.getSelectedItemPosition();
                    Toast.makeText(getBaseContext(), 
                            "You have selected the book: " + androidBooks[item], 
                            Toast.LENGTH_SHORT).show();
                }
    
                public void onNothingSelected(AdapterView<?> arg0) {
                }
                
            });
    

    在吐司中,您将获得选定的项目 将此项目传递到另一个屏幕。

    【讨论】:

      猜你喜欢
      • 2012-07-12
      • 2013-03-22
      • 1970-01-01
      • 2015-07-08
      • 2011-05-06
      • 1970-01-01
      • 1970-01-01
      • 2012-03-12
      • 2012-03-12
      相关资源
      最近更新 更多