Java 如何解决android中的hashmap逻辑错误?
我面临着一些已知的可解决的问题,但却在这里出局了。在我的代码中,值在第一次for循环执行时存储在ArrayList中。但是,在第二个循环中,这些值会被ArrayList中的最终值覆盖。最后,最后输入的值单独存储为列表大小的倍数Java 如何解决android中的hashmap逻辑错误?,java,android,arraylist,hashmap,Java,Android,Arraylist,Hashmap,我面临着一些已知的可解决的问题,但却在这里出局了。在我的代码中,值在第一次for循环执行时存储在ArrayList中。但是,在第二个循环中,这些值会被ArrayList中的最终值覆盖。最后,最后输入的值单独存储为列表大小的倍数 lstall = (ListView)findViewById(R.id.lvall); sampleArrayList = new ArrayList<HashMap<String, String>>(); ListAdapter sample
lstall = (ListView)findViewById(R.id.lvall);
sampleArrayList = new ArrayList<HashMap<String, String>>();
ListAdapter sampleListAdapter;
dh = new DBHelper(this);
HashMap<String, String> sampleObjectMap;
List<String> lvall = dh.selectAll();
sampleObjectMap= new HashMap<String, String>();
for(int i=0;i<lvall.size();i++)
{
for (@SuppressWarnings("unused") String sampleObj : lvall)
{
sampleObjectMap.put("title", dh.val1(i));
sampleObjectMap.put("person", dh.pers(i));
sampleObjectMap.put("priorty", setpriority(String.valueOf(dh.prioirty(i))));
sampleObjectMap.put("dat", getDate(Long.valueOf(dh.time(i)),"dd/MM/yyyy"));
}
sampleArrayList.add(sampleObjectMap);
}
lstall=(ListView)findViewById(R.id.lvall);
sampleArrayList=新建ArrayList();
ListAdapter样本ListAdapter;
dh=新的DBHelper(this);
HashMap-sampleObjectMap;
List lvall=dh.selectAll();
sampleObjectMap=newHashMap();
对于(inti=0;i我没有测试它,但尝试创建新的映射
sampleObjectMap = new HashMap<String, String>();
sampleObjectMap=newhashmap();
在您的内部循环中,在放置之前。测试它。稍后尝试使用1个map对象,并在sampleArrayList中记录映射后调用clear()。我没有测试它,但尝试创建新映射
sampleObjectMap = new HashMap<String, String>();
sampleObjectMap=newhashmap();
在内部循环中,在执行放置之前。测试它。稍后尝试使用1个map对象,并在sampleArrayList中记录映射后调用clear()。尝试:
for(int i=0;i<lvall.size();i++)
{
sampleObjectMap= new HashMap<String, String>();
sampleObjectMap.put("title", dh.val1(i));
sampleObjectMap.put("person", dh.pers(i));
sampleObjectMap.put("priorty", setpriority(String.valueOf(dh.prioirty(i))));
sampleObjectMap.put("dat", getDate(Long.valueOf(dh.time(i)),"dd/MM/yyyy"));
sampleArrayList.add(sampleObjectMap);
}
for(int i=0;i尝试一下:
for(int i=0;i<lvall.size();i++)
{
sampleObjectMap= new HashMap<String, String>();
sampleObjectMap.put("title", dh.val1(i));
sampleObjectMap.put("person", dh.pers(i));
sampleObjectMap.put("priorty", setpriority(String.valueOf(dh.prioirty(i))));
sampleObjectMap.put("dat", getDate(Long.valueOf(dh.time(i)),"dd/MM/yyyy"));
sampleArrayList.add(sampleObjectMap);
}
for(int i=0;i列表中的所有元素都具有相同Map
实例的引用,并且您正在为每个迭代覆盖相同的Map
条目
for(int i=0;i<lvall.size();i++) {
// move it inside the loop
sampleObjectMap= new HashMap<String, String>();
for(int i=0;i列表中的所有元素都具有相同Map
实例的引用,并且您正在为每个迭代覆盖相同的Map
条目
for(int i=0;i<lvall.size();i++) {
// move it inside the loop
sampleObjectMap= new HashMap<String, String>();
for(int i=0;i您正在创建一个HashMap
并重复覆盖其中的条目
事实上,这是嵌套for循环的两倍,没有明显的原因。我相信您希望:
for (int i = 0; i < lvall.size(); i++)
{
HashMap<String, String> sampleObjectMap = new HashMap<String, String>();
sampleObjectMap.put("title", dh.val1(i));
sampleObjectMap.put("person", dh.pers(i));
sampleObjectMap.put("priorty", setpriority(String.valueOf(dh.prioirty(i))));
sampleObjectMap.put("dat", getDate(Long.valueOf(dh.time(i)),"dd/MM/yyyy"));
sampleArrayList.add(sampleObjectMap);
}
for(int i=0;i
使用列表大小也不实际使用列表中的值似乎很奇怪……您可能需要考虑重构代码…
您正在创建一个单独的<代码> HashMap < /代码>,并反复重写其中的条目。
事实上,这是嵌套for循环的两倍,没有明显的原因。我相信您希望:
for (int i = 0; i < lvall.size(); i++)
{
HashMap<String, String> sampleObjectMap = new HashMap<String, String>();
sampleObjectMap.put("title", dh.val1(i));
sampleObjectMap.put("person", dh.pers(i));
sampleObjectMap.put("priorty", setpriority(String.valueOf(dh.prioirty(i))));
sampleObjectMap.put("dat", getDate(Long.valueOf(dh.time(i)),"dd/MM/yyyy"));
sampleArrayList.add(sampleObjectMap);
}
for(int i=0;i
使用列表的大小,但实际上不使用列表中的值,这似乎也很奇怪……您可能需要考虑重构代码…
@ PrasHyMaya:我也将变量的声明移到循环中以限制范围。t?你反对这个答案吗?非常感谢…这是我犯的主要错误…非常完美worked@PareshMayani:为了限制作用域,我还将变量的声明移到了循环中。我认为这是添加了实际的解释,但这有帮助。@Downvoter:要不要评论一下?您对此有何异议回答?非常感谢…这是我犯的主要错误…非常有效