Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android中获取HashMap元素值时遇到问题_Java_Android_Hashmap - Fatal编程技术网

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

Java 在Android中获取HashMap元素值时遇到问题,java,android,hashmap,Java,Android,Hashmap,我有下面的代码,我正试图通过使用类创建一个单独的列表 我有这个: 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(IT

我有下面的代码,我正试图通过使用类创建一个单独的列表

我有这个:

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);

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

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

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

List<Map<String,?>> security = new LinkedList<Map<String,?>>();
List security=newlinkedlist();
为此:

List<Map<String, String>> security = new LinkedList<Map<String, String>>();
List security=newlinkedlist();
这与

createItem
方法上,返回一个
HashMap
,希望Java将其绑定到
Map

这会导致JVM将您的
字符串
解除绑定为未知类型
。因此,当您进行映射时,它不知道如何将未知类型
绑定到类型
t

我的建议是在
createItem()
上返回
Map
,然后

List<Map<String,?>> security = new LinkedList<Map<String,?>>();
List security=newlinkedlist();
进入

List security=newlinkedlist();

不要将数据存储到HashMap中,而是创建一个包含此数据的类,然后将其存储到IList或Map中

大概是这样的:

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;
}

仍然返回
类型类的get()方法未定义
,但是现在有了一个捕获#2你知道,它们完全相同吗?在你的帖子中将x变成x。此外,将
createItem()
转换为
Map
也没有帮助。对不起,我的错误,请将
转换为
字符串,即将
e
绑定到
字符串,而不是未知的
。再次尝试引用
父项.getItemAtPosition(position)
as
HashMap
返回
类型不匹配:无法从类转换为HashMap
谢谢,这将非常好,但我使用的是现成的类,因此这不是一个选项。但这对我来说是最清楚的。
List<Map<String, String>> security = new LinkedList<Map<String, String>>();
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;
}