在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(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会说:

The method get() is undefined for the type Class<capture#5-of ? extends Object>

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

这与捕获转换有关。

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

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

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

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

进入

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

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

你可能还想改变这个:

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

进入这个:

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

不要将数据存储到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;
}

最新更新