我创建了包含一个问题和4个选项的数据库和表“TABLE_NAME”。
我的问题是如何使用listview中的选项显示此问题。
我搜索很多文章,但无法找到适当的方法一次添加5个项目。
请给我一些参考或代码。
我想使用可扩展列表视图。但它不显示问题并仅显示
从4选项中的一个选项。
这是我的示例代码。如何在ListView中添加一个带有四个选项的项目?
public class ObjectiveExamActivity extends ExpandableListActivity {
@SuppressWarnings("unchecked")
public void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.displayobjectiveque);
Intent intent=getIntent();
setResult(RESULT_OK, intent);
SimpleExpandableListAdapter expListAdapter =
new SimpleExpandableListAdapter(
this,
createGroupList(),
R.layout.group_row,
new String[] { "Group Item" },
new int[] { R.id.row_name },
createChildList(),
R.layout.child_row,
new String[] {"Sub Item"},
new int[] { R.id.grp_child}
);
setListAdapter(expListAdapter);
}catch(Exception e){
System.out.println("Error+++ " + e.getMessage());
}
}
@SuppressWarnings("unchecked")
private List createGroupList() {
ArrayList result = new ArrayList();
final MySQLiteHelper m=new MySQLiteHelper(getBaseContext());
final List<ObjectiveWiseQuestion> LocWiseProfile= m.getAllObjectiveQuestion();
for (final ObjectiveWiseQuestion cn : LocWiseProfile)
{
HashMap m1=new HashMap();
m1.put("Item","Option:"+cn.getQuestion());
result.add(m1);
}
/* for(int i = 0 ; i < 15 ; ++i) { // 15 groups........
HashMap m = new HashMap();
m.put("Group Item","Group Item " + i); // the key and it's value.
result.add(m);
}*/
return (List)result;
}
@SuppressWarnings("unchecked")
private List createChildList()
{
final MySQLiteHelper m=new MySQLiteHelper(getBaseContext());
final List<ObjectiveWiseQuestion> LocWiseProfile= m.getAllObjectiveQuestion();
ArrayList result = new ArrayList();
for (final ObjectiveWiseQuestion cn : LocWiseProfile)
{ // this -15 is the number of groups(Here it's fifteen)
/* each group need each HashMap-Here for each group we have 3 subgroups */
ArrayList secList = new ArrayList();
for(int n = 0 ; n < 4 ; n++)
{
HashMap child = new HashMap();
child.put("Sub Item", "Sub Item " + m.getAllObjectiveQuestion());
child.put(cn.getOptionA(), cn.getOptionB());
child.put("Option C", cn.getOptionC());
child.put("Option D", cn.getOptionD());
secList.add(child);
}
result.add(secList);
}
return result;
}
public void onContentChanged () {
System.out.println("onContentChanged");
super.onContentChanged();
}
/* This function is called on each child click */
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,int childPosition,long id) {
System.out.println("Inside onChildClick at groupPosition = " + groupPosition +" Child clicked at position " + childPosition);
return true;
}
/* This function is called on expansion of the group */
public void onGroupExpand (int groupPosition) {
try{
System.out.println("Group exapanding Listener => groupPosition = " + groupPosition);
}catch(Exception e){
System.out.println(" groupPosition Errrr +++ " + e.getMessage());
}
}
}
在此先感谢。
尝试expandibleListView ... – ngesh 2012-03-19 08:17:42