在片段扩展的ListView [英] Expandable ListView in Fragment
问题描述
我有一个工作expandablelistview code,工作正常作为一个独立的。 我有另一个工作$ C $下滑,我分别写标签视图。
I have a working expandablelistview code, which works fine as a standalone. I have another working code for sliding tab views which I wrote separately.
这两个打算通了许多博客,Android开发人员和计算器问题后写的。
Both these were written after going thru a number of blogs, android dev and stackoverflow questions.
当我尝试结合起来,并把扩展列表视图中的片段中的一个,我结束了一个错误,我无法去解决。
When I try to combine and put the expandable listview in one of the fragments, I end up with an error, I am unable to resolve.
下面是从碎片一块code:
Here's the code from the Fragment piece:
public class Fragment1 extends Fragment {
private static final String NAME = "NAME";
private static final String IS_EVEN = "IS_EVEN";
private ExpandableListAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
for (int i = 0; i < 10; i++) {
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(NAME, "Group " + i);
curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");
List<Map<String, String>> children = new ArrayList<Map<String, String>>();
for (int j = 0; j < 2; j++) {
Map<String, String> curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(NAME, "Child " + j);
curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd");
}
childData.add(children);
}
// Set up our adapter
mAdapter = new SimpleExpandableListAdapter(
getActivity(),
groupData,
android.R.layout.simple_expandable_list_item_1,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 },
childData,
android.R.layout.simple_expandable_list_item_2,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 }
);
setListAdapter(mAdapter);
}
}`
我把最后一行得到错误:的方法setListAdapter(ExpandableListAdapter)是不确定的片段1的类型
的code,其余显示没有错误。
The rest of the code shows no error.
请帮忙。我怎样才能解决这一问题。
Please help. How can I correct this problem.
推荐答案
我是能够解决自己的问题。对于将来参考,这里的code。也许是凌乱的,需要清理,但它现在!
I was able to solve the problem myself.. For future reference, here's the code. Might be messy and needs cleanup, but it works for now!
public class HotDeals extends Fragment {
private ExpandableListAdapter mAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.saved_tabs, null);
return v;
}
private static final String NAME = "NAME";
private static final String IS_EVEN = "IS_EVEN";
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
for (int i = 0; i < 10; i++) {
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(NAME, "Group " + i);
curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");
List<Map<String, String>> children = new ArrayList<Map<String, String>>();
for (int j = 0; j < 2; j++) {
Map<String, String> curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(NAME, "Child " + j);
curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd");
}
childData.add(children);
}
ExpandableListView lv = (ExpandableListView) getActivity().findViewById(R.id.list);
// Set up our adapter
mAdapter = new SimpleExpandableListAdapter(
getActivity(),
groupData,
android.R.layout.simple_expandable_list_item_1,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 },
childData,
android.R.layout.simple_expandable_list_item_2,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 }
);
lv.setAdapter(mAdapter);
}
}
@Divers和放大器; @Dixit - 感谢名单
@Divers & @Dixit - Thanx.
这篇关于在片段扩展的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!