我有一个LinkedHashMap,它填充db中的数据,循环“for”逐字符串,当我尝试显示第一个或最后一个String时,该方法只能显示日志中的最后一个String。但是在应用程序中listViewContent被完全填充。所以我不明白为什么我看不到任何我想要的字符串。我需要收集从db获得的所有字符串并在将来比较它们。
我如何收集所有字符串以及我应该调用哪种方法来显示我想要看到的字符串?不幸的是我只能检索一个(和最后一个而不是第一个)字符串。 这是我的示例代码:
protected void onCreate(Bundle saveInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FirstMethod();
}
public FirstMethod() {
SecondMethod newMethod = .. // getting data from the second method
}
public SecondMethod() {
public void onResponseReceived(String result) {
try {
...
if (posts != null) {
for (WallPostItem post : posts) { // this loop
//create new map for a post
Map<String, Object> map = new LinkedHashMap<String, Object>();
map.put(ATTRIBUTE_NAME_TEXT, post.text);
PictureItem postPicture = new PictureItem();
map.put(ATTRIBUTE_NAME_IMAGE, postPicture);
map.put(ATTRIBUTE_NAME_DATE, post.date);
sAdapter.notifyDataSetChanged();
};
};
...
List<Map.Entry<String, Object>> list = new ArrayList<Map.Entry<String, Object>>(GlobalMap.entrySet());
Map.Entry<String, Object> firstInsertedEntry = list.get(0);
Log.w("FirstEntryOfMap",""+firstInsertedEntry); // this log shows me the last string instead of the first
}
if (isRefresh) {
isRefresh = false;
lvSimple.setSelectionAfterHeaderView();
}
} catch (Exception e) {
Log.d("exceptions", "problem in get wall post task after post execute: " + e.toString());
}
}
答案 0 :(得分:2)
您没有将您的值放入List
,而是将它们放入Map
(保留关键顺序)。我建议你创建一个POJO类,
class MyAttribute {
final String postName;
final PictureItem postPicture;
final Date postDate;
public MyAttribute(String postName, PictureItem postPicture, Date postDate) {
this.postName = postName;
this.postPicture = postPicture;
this.postDate = postDate;
}
public String getPostName() {
return postName;
}
public Date getPostDate() {
return postDate;
}
public PictureItem getPostPicture() {
return postPicture;
}
}
然后你可以创建一个
List<MyAttribute> myAttributes = new ArrayList<>();