首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Android中获取HashMap元素的值时遇到问题

在Android中获取HashMap元素的值时遇到问题
EN

Stack Overflow用户
提问于 2012-01-23 16:02:34
回答 3查看 250关注 0票数 0

我有以下代码,我正在尝试使用this类来创建一个独立的列表。

我有这个:

代码语言:javascript
复制
public Map<String, ?> createItem(String title, String caption, String uri) {
        Map<String, String> item = new HashMap<String, String>();
        item.put(ITEM_TITLE, title);
        item.put(ITEM_CAPTION, caption);
        item.put(ITEM_URI, uri );
        return item;
    }

public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        /** @todo Draw the splash screen */
        setContentView(R.layout.list_complex);
        List<Map<String,?>> security = new LinkedList<Map<String,?>>();
        security.add(createItem("Remember passwords", "Save usernames and passwords for Web sites","uri"));
        security.add(createItem("Clear passwords", "Save usernames and passwords for Web sites","uri2"));
        security.add(createItem("Show security warnings", "Show warning if there is a problem with a site's security","uri3"));

SeparatedListAdapter adapter = new SeparatedListAdapter(this);
        adapter.addSection("Security", new SimpleAdapter(this, security, R.layout.list_complex,
            new String[] { ITEM_TITLE, ITEM_CAPTION}, new int[] { R.id.list_complex_title, R.id.list_complex_caption }));

        ListView list = new ListView(this);

        list.setOnItemClickListener(new OnItemClickListener() {
              public void onItemClick(AdapterView<?> parent, View view,
                      int position, long id) {
                 try{
                      Log.v(TAG, "Pressed id: "+(parent.getItemAtPosition(position).getClass()));
                 }catch(Exception e){
                     Log.v(TAG, "Field not found: "+e.getCause()+" : "+e.getMessage());
                 }
//Log.v(TAG, "Pressed id: "+parent.);
                  //  Intent newActivity = new Intent(view.getContext(),agones.class);     
                        //     startActivity(newActivity);

                  }
                });
        list.setAdapter(adapter);
        this.setContentView(list);

我在这里尝试做的是从HashMap获取字段uri。如果我注销它,它会告诉我这个类是实例HashMap的,但是当我尝试在它上面使用方法.get()时,Eclipse显示如下:

代码语言:javascript
复制
The method get() is undefined for the type Class<capture#5-of ? extends Object>

我该如何着手解决这个问题呢?如果这很简单,我很抱歉,但由于我是新人,我无法理解这一点。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-01-23 16:12:02

这与Capture Conversion有关。

在您的createItem方法中,您将返回一个HashMap<String, String>,并期望Java将其绑定到Map<String, ?>

这会导致JVM取消将您的String绑定到未知类型的?。因此,当您执行Map<String, ?>.get()时,它不知道如何将未知类型?绑定到类型T

我的建议是在createItem()上返回一个Map<String, String>,并且

代码语言:javascript
复制
List<Map<String,?>> security = new LinkedList<Map<String,?>>();

转到

代码语言:javascript
复制
List<Map<String, String>> security = new LinkedList<Map<String, String>>();
票数 0
EN

Stack Overflow用户

发布于 2012-01-23 16:07:35

为什么您的createItem方法返回Map<String, ?>而不是Map<String, String>?将其更改为返回Map<String, String>

您可能还想要更改以下内容:

代码语言:javascript
复制
List<Map<String,?>> security = new LinkedList<Map<String,?>>();

如下所示:

代码语言:javascript
复制
List<Map<String, String>> security = new LinkedList<Map<String, String>>();
票数 0
EN

Stack Overflow用户

发布于 2012-01-23 16:12:29

不是将数据存储到映射中,而是创建一个包含这些数据的类,然后将其存储到IList或HashMap中。

如下所示:

代码语言:javascript
复制
public class Storage
{
  public Storage(String t, String c, String u)
  {
    title = t;
    caption = c;
    uri = u;
  }

  public String title;
  public String caption;
  public String uri;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8968617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档