我有以下代码,我正在尝试使用this类来创建一个独立的列表。
我有这个:
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>我该如何着手解决这个问题呢?如果这很简单,我很抱歉,但由于我是新人,我无法理解这一点。
发布于 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>,并且
List<Map<String,?>> security = new LinkedList<Map<String,?>>();转到
List<Map<String, String>> security = new LinkedList<Map<String, String>>();发布于 2012-01-23 16:07:35
为什么您的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>>();发布于 2012-01-23 16:12:29
不是将数据存储到映射中,而是创建一个包含这些数据的类,然后将其存储到IList或HashMap中。
如下所示:
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;
}https://stackoverflow.com/questions/8968617
复制相似问题