Java 我可以在哪个容器中收集字符串以显示其中任何一个
我有一个LinkedHashMap,它使用循环“for”字符串逐字符串填充db中的数据,当我尝试显示第一个或最后一个字符串时,该方法只能显示日志中的最后一个字符串。但在应用程序listViewContent中已完全填充。所以我不明白为什么我看不到我想要的任何字符串。我需要收集我从db获得的所有字符串,并在将来比较它们 如何收集所有字符串以及应该调用什么方法来显示我想要查看的字符串?不幸的是,我只能检索一个(最后一个而不是第一个)字符串。 下面是我的示例代码:Java 我可以在哪个容器中收集字符串以显示其中任何一个,java,android,list,arraylist,linkedhashmap,Java,Android,List,Arraylist,Linkedhashmap,我有一个LinkedHashMap,它使用循环“for”字符串逐字符串填充db中的数据,当我尝试显示第一个或最后一个字符串时,该方法只能显示日志中的最后一个字符串。但在应用程序listViewContent中已完全填充。所以我不明白为什么我看不到我想要的任何字符串。我需要收集我从db获得的所有字符串,并在将来比较它们 如何收集所有字符串以及应该调用什么方法来显示我想要查看的字符串?不幸的是,我只能检索一个(最后一个而不是第一个)字符串。 下面是我的示例代码: protected void onC
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());
}
}
创建时受保护的void(Bundle saveInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
第一种方法();
}
公共方法(){
SecondMethod newMethod=..//从第二个方法获取数据
}
公共方法(){
接收到响应时的公共void(字符串结果){
试一试{
...
if(posts!=null){
对于(wallpositem post:posts){//此循环
//为帖子创建新地图
Map Map=newlinkedhashmap();
map.put(属性\名称\文本,post.TEXT);
PictureItem postPicture=新PictureItem();
map.put(属性\名称\图像,后期图片);
map.put(属性、名称、日期、发布日期);
sAdapter.notifyDataSetChanged();
};
};
...
List List=newarraylist(GlobalMap.entrySet());
Map.Entry firstInsertedEntry=list.get(0);
Log.w(“FirstEntryOfMap”,“”+FirstInserteEntry);//此日志显示最后一个字符串,而不是第一个字符串
}
如果(isRefresh){
isRefresh=false;
lvSimple.setSelectionAfterHeaderView();
}
}捕获(例外e){
Log.d(“异常”,“post执行后get wall post任务中出现问题:+e.toString());
}
}
您没有将值放入列表中,而是将它们放入映射中(保留键顺序)。我建议您创建一个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<>();
List myAttributes=new ArrayList();
我创建了数据库,但现在如何添加数据库数据?例如,myAttributes.add(post.text)对我不起作用使用构造函数newmyattribute(post.text,postPicture,post.date)
你的意思是我必须执行“newmyattribute(post.text,post.picture,post.date)”;List myAttributes=new ArrayList();“inside”for“循环,然后我可以在输出日志中打印我的myAttributes数据?是否正确?否。在for循环之外创建列表<代码>列表myAttributes=new ArrayList()代码>在循环中使用myAttributes.add(newmyattribute(post.text、postPicture、post.date)),将属性添加到列表中代码>如果要记录它们,可以。如果您想这样做,您可能应该在MyAttribute
中重写toString()
。好的,它工作得很好!谢谢,但我还有一个问题。如何分配myAttributes,或者如何在包含循环的方法之外设置参数(字符串)?我每次都得到空指针或空列表。我试图创建全局变量,但又得到了空指针