我有一个ArrayList,其层次结构具有3个级别。类别,论坛,ChildForum。
但是Forum和ChildForum具有相同的属性(最重要的属性是nodeId和parentNodeId),而Coupe论坛内部没有任何ChildForum。
Category------
|
Forum---------
| |
| ChildForum--------
| |
|______________________Thread
因此,我想找到一种将它们绑定到listviews和onClickItem中的方法,查看哪个Forum是否具有子级,以便我可以将视图导航到正确的片段
这就是我现在所做的:
nodeList = new ArrayList<Node>(); //nodeList contains Category, Forum, and ChildForum
forumNodeList = new ArrayList<Node>();
childList = new ArrayList<Node>(); // temp array
forumChildNodeList = new HashMap<String, ArrayList<Node>>();
填写数据
for (int i = 0; i < nodeList.size(); i++) {
if (nodeList.get(i).getParent_node_id().equals(nodeId)) { //nodeId = categoryId
forumNodeList.add(nodeList.get(i));
}
for (int j = 0; j < forumNodeList.size(); j++) {
if (forumNodeList.get(j).getNodeId()
.equals(nodeList.get(i).getParent_node_id())) {
childList.add(nodeList.get(i));
// Log.d("childList", "" + childList.size());
forumChildNodeList.put(
forumNodeList.get(j).getNodeId(), childList);
}
forumChildNodeList.put(forumNodeList.get(j).getNodeId(),
childList);
}
}
setOnClickItem:
getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
if (forumChildNodeList.get(
forumNodeList.get(position).getNodeId()).size() > 0) {
//Fragment Transaction to ChildForumFragment
} else {
//Fragment Transaction to ThreadFragment
}
}
});
当我显示登录onClick时。似乎所有具有论坛ID的HashMap都具有相同的ChildForum。实际上,其中一些没有任何childForum。
我肯定知道,For循环和if条件有问题。还是我想念什么?请帮忙!
提前致谢!
最佳答案
我发现了我的问题。这是条件问题:
getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
if (forumChildNodeList.get(forumNodeList.get(position)
.getNodeId()) != null) {
//Fragment Transaction to ChildForumFragment
} else {
//Fragment Transaction to ThreadFragment
}
}
});
数组应为ArrayList!= null而不是ArrayList.size()> 0