[英]Put Hashmap into Arraylist<HashMap>
I'm new at android. 我是android新手。 I trying to get the data from
Database
and put it into HashMap
. 我试图从
Database
获取数据并将其放入HashMap
。 But I got a little problem here. 但是我这里有点问题。 I got an error when I try to put the data that I get.
尝试放入获取的数据时出现错误。
I put a comment on the error line in my code. 我在代码中的错误行上添加了注释。 You can check it below
您可以在下面检查
Here's my class 这是我的课
private static final String TAG_ITEM_NAME = "item_name";
// Hashmap for ListView
ArrayList<HashMap<String, String>> searchlist;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
searchlist = new ArrayList<HashMap<String, String>>();
Intent search = getIntent();
String searchResult = search.getStringExtra("TAG_SEARCH");
DatabaseHandler db = new DatabaseHandler(
List <AllItem> allItems = new ArrayList<AllItem>();
allItems = db.getAllSearchResult(searchResult);
HashMap<String, AllItem> all_Items = new HashMap<String, AllItem>();
for(AllItem cn : allItems) {
String item_name = cn.getItem_name();
//AllItem item_name = all_Items.put(cn.getItem_name(), cn);
all_Items.put(TAG_ITEM_NAME,item_name); // I got error here
}
searchlist.add(all_Items);
ListAdapter adapter = new SimpleAdapter(
Search.this, searchlist,
R.layout.searchlayout, new String[] {TAG_ITEM_NAME},
new int[] { R.id.category_name}
);
// Assign adapter to ListView
listview.setAdapter(adapter);
}
The error said The Method put(String, Allitem) int type Hashmap<String,Allitem> is not applicable for the argument (String, String)
错误消息:
The Method put(String, Allitem) int type Hashmap<String,Allitem> is not applicable for the argument (String, String)
How to fix this error? 如何解决这个错误? Thanks before :D
之前感谢:D
all_items takes a String
and an AllItem
, but you are placing two String
s into it in this line: all_items接受一个
String
和一个AllItem
,但是您在此行中将两个String
放入其中:
// TAG_ITEM_NAME is a String and item_name is also a String
all_Items.put(TAG_ITEM_NAME,item_name);
You try to put in the map as a value string but you map expect as a value AllItem, so you must modify your code in this way: 您尝试将映射作为值字符串放入映射中,但将期望作为值AllItem映射到映射中,因此必须以这种方式修改代码:
for(AllItem cn : allItems) {
String item_name = cn.getItem_name();
all_Items.put(item_name , cn );
}
This code will add your class object to the map with the key which equals to your object name. 此代码将使用与您的对象名称相同的键将您的类对象添加到地图。
Your HashMap
is as follows: 您的
HashMap
如下:
HashMap<String, AllItem> all_Items = new HashMap<String, AllItem>();
This means it has String
keys and AllItem
values. 这意味着它具有
String
键和AllItem
值。 You can put a AllItem
object inside this HashMap
. 您可以将
AllItem
对象放入此HashMap
。
When you write 当你写
all_Items.put(TAG_ITEM_NAME,item_name); // I got error here
you are putting a String
inside the HashMap
hence it is giving error. 您正在将
String
放入HashMap
因此它给出了错误。
You should be doing: 您应该这样做:
all_Items.put(TAG_ITEM_NAME,cn);
Your hashmap is <String, Allitem>
. 您的哈希图是
<String, Allitem>
。 While you try to put <String, String>
in it. 当您尝试将
<String, String>
放入其中时。
all_Items.put(TAG_ITEM_NAME, item_name);
should be changed to 应该更改为
all_Items.put(TAG_ITEM_NAME, cn);
Hope you got the difference. 希望你能有所作为。
all_Items
is defined as a hashmap to contain <String, AllItem>
key value pairs as defined here: all_Items
定义为一个哈希图,以包含<String, AllItem>
键值对,如下所示:
HashMap<String, AllItem> all_Items = new HashMap<String, AllItem>();
but you are trying to push <String,String>
key value pair into your all_Items
hashmap : 但您尝试将
<String,String>
键值对推入all_Items
哈希图中:
all_Items.put(TAG_ITEM_NAME,item_name); // I got error here
It seems you want to push the AllItem
object against its item_name
, which can be done as: 看来您想将
AllItem
对象推向它的item_name
,可以这样完成:
all_Items.put(item_name, cn);
I dont see connection in the code but using the given information, your error probably is related to your searchlist
variable in your code. 我没有在代码中看到连接,但是使用给定的信息,您的错误可能与代码中的
searchlist
变量有关。
change this: 改变这个:
ArrayList<HashMap<String, String>> searchlist;
to this: 对此:
ArrayList<HashMap<String, AllItem>> searchlist;
it's because you declared HashMap<String, AllItem>
and you try to put all_Items.put(TAG_ITEM_NAME,item_name); which would probably be HashMap<String, String>()
这是因为您声明了
HashMap<String, AllItem>
并尝试放入all_Items.put(TAG_ITEM_NAME,item_name); which would probably be HashMap<String, String>()
all_Items.put(TAG_ITEM_NAME,item_name); which would probably be HashMap<String, String>()
. all_Items.put(TAG_ITEM_NAME,item_name); which would probably be HashMap<String, String>()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.