Android 是否将数据从SQLite检索到可展开列表视图?
您好,我有一个表“table_OBJECTIVE”,其中有1个问题和4个选项,我接受Android 是否将数据从SQLite检索到可展开列表视图?,android,sqlite,expandablelistview,Android,Sqlite,Expandablelistview,您好,我有一个表“table_OBJECTIVE”,其中有1个问题和4个选项,我接受 编辑文本。我的问题是如何将此问题显示为家长,将4选项显示为孩子 在可展开列表视图中。我使用了一些代码,但它不显示问题,只显示 四选一。 这是我的示例代码 public class ObjectiveExamActivity extends ExpandableListActivity { @SuppressWarnings("unchecked") public void onCreate(Bundle sa
编辑文本。我的问题是如何将此问题显示为家长,将4选项显示为孩子
在可展开列表视图中。我使用了一些代码,但它不显示问题,只显示
四选一。
这是我的示例代码
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("Errrr +++ " + e.getMessage());
}
}
@SuppressWarnings("unchecked")
private List createGroupList() {
ArrayList result = new ArrayList();
final MySQLiteHelper m=new MySQLiteHelper(getBaseContext());
final ArrayList<ObjectiveWiseQuestion> LocWiseProfile= (ArrayList<ObjectiveWiseQuestion>) m.getAllObjectiveQuestion();
for (final ObjectiveWiseQuestion cn : LocWiseProfile)
{
HashMap m1=new HashMap();
m1.put("Question",cn.getQuestion());
result.add(m1);
}
return (List)result;
}
@SuppressWarnings("unchecked")
private List createChildList()
{
final MySQLiteHelper m=new MySQLiteHelper(getBaseContext());
final ArrayList<ObjectiveWiseQuestion> LocWiseProfile= (ArrayList<ObjectiveWiseQuestion>) m.getAllObjectiveQuestion();
ArrayList result = new ArrayList();
for (final ObjectiveWiseQuestion cn : LocWiseProfile)
{
/* each group need each HashMap-Here for each group we have 4 subgroups */
ArrayList secList = new ArrayList();
for(ObjectiveWiseQuestion cn1: LocWiseProfile)
{
HashMap child = new HashMap();
//child.putAll(cn.getOptionA(),cn.getOptionB(),cn.getOptionC());
child.put( "Sub Item", "Sub Item " + cn.getOptionA() );
child.put("", cn.getOptionB());
child.put("Option C", cn.getOptionC());
child.put("Option D", cn.getOptionD());
secList.add( child );
}
result.add( secList );
}
return result;
}
公共类ObjectiveExamActivity扩展了ExpandableListActivity{
@抑制警告(“未选中”)
创建时的公共void(Bundle savedInstanceState){
试一试{
super.onCreate(savedInstanceState);
setContentView(R.layout.displayobjectiveque);
Intent=getIntent();
设置结果(结果正常,意图);
SimpleExpandableListAdapter解释适配器=
新的SimpleExpandableListAdapter(
这
createGroupList(),
R.layout.group_行,
新字符串[]{“组项”},
新int[]{R.id.row_name},
createChildList(),
R.layout.child_行,
新字符串[]{“子项”},
新int[]{R.id.grp_child}
);
setListAdapter(expListAdapter);
}捕获(例外e){
System.out.println(“Errrr++”+e.getMessage());
}
}
@抑制警告(“未选中”)
私有列表createGroupList(){
ArrayList结果=新建ArrayList();
final MySQLiteHelper m=新的MySQLiteHelper(getBaseContext());
最终ArrayList LocWiseProfile=(ArrayList)m.getAllObjectiveQuestion();
对于(最终目标WiseQuestion cn:LocWiseProfile)
{
HashMap m1=新的HashMap();
m1.提出(“问题”,cn.getQuestion());
结果:添加(m1);
}
返回(列表)结果;
}
@抑制警告(“未选中”)
私有列表createChildList()
{
final MySQLiteHelper m=新的MySQLiteHelper(getBaseContext());
最终ArrayList LocWiseProfile=(ArrayList)m.getAllObjectiveQuestion();
ArrayList结果=新建ArrayList();
对于(最终目标WiseQuestion cn:LocWiseProfile)
{
/*每个组需要每个HashMap,每个组有4个子组*/
ArrayList secList=新的ArrayList();
对于(ObjectiveWiseQuestion cn1:LocWiseProfile)
{
HashMap child=新的HashMap();
//putAll(cn.getOptionA(),cn.getOptionB(),cn.getOptionC());
child.put(“子项”、“子项”+cn.getOptionA());
child.put(“,cn.getOptionB());
child.put(“Option C”,cn.getOptionC());
child.put(“期权D”,cn.getOptionD());
secList.add(子项);
}
结果。添加(secList);
}
返回结果;
}
我真的不明白代码中的问题在哪里。请给我一些提示或参考。
这是只显示一项的emulator图像。
我有4个选项(谷歌、三星、诺基亚、Onida)。
提前感谢。
您好,请尝试一下这段代码。对于一个问题,这段代码对我来说效果很好,但由于我使用了数组列表和hashmap,您可以对多个问题使用长度和大小函数
public class ShowQuiz extends Activity {
/** Called when the activity is first created. */
ExpandableListView expandableListView;
HashMap<String, Options> hashMap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Options options=new Options();
hashMap=new HashMap<String, Options>(1);
hashMap.put("Which is Domestic Animal?", options);
expandableListView=(ExpandableListView) findViewById(R.id.ev);
expandableListView.setAdapter(new QuizQuestionAdapter());
}
class QuizQuestionAdapter extends BaseExpandableListAdapter
{
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return hashMap.get(0).fetchOptions().get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView textView=new TextView(ShowQuiz.this);
textView.setText(hashMap.get("Which is Domestic Animal?").getQuestion(childPosition).toString());
return textView;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return 4;
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return hashMap.get(0);
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return 1;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView textView=new TextView(ShowQuiz.this);
textView.setText("Which is Domestic Animal?");
return textView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
}
class Options
{
ArrayList<String> optns;
Options()
{
optns=new ArrayList<String>(4);
optns.add("rat");
optns.add("cat");
optns.add("dog");
optns.add("tiger");
}
public ArrayList<String> fetchOptions()
{
return this.optns;
}
public String getQuestion(int position)
{
return optns.get(position);
}
}
}
公共类showquick扩展活动{
/**在首次创建活动时调用*/
ExpandableListView ExpandableListView;
HashMap;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
选项=新选项();
hashMap=新的hashMap(1);
hashMap.put(“哪个是家畜?”,选项);
expandableListView=(expandableListView)findViewById(R.id.ev);
setAdapter(新的QuizQuestionAdapter());
}
类QuizQuestionAdapter扩展了BaseExpandableListAdapter
{
@凌驾
公共对象getChild(int-groupPosition,int-childPosition){
//TODO自动生成的方法存根
返回hashMap.get(0.fetchOptions().get(childPosition);
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition){
//TODO自动生成的方法存根
返回0;
}
@凌驾
公共视图getChildView(int-groupPosition、int-childPosition、,
布尔值isLastChild、视图转换视图、视图组父级){
//TODO自动生成的方法存根
TextView TextView=新的TextView(showquick.this);
textView.setText(hashMap.get(“哪是家畜?”).getQuestion(childPosition.toString());
返回文本视图;
}
@凌驾
公共整数getChildrenCount(整数组位置){
//TODO自动生成的方法存根
返回4;
}
@凌驾
公共对象getGroup(int-groupPosition){
//TODO自动生成的方法存根
返回hashMap.get(0);
}
@凌驾
public int getGroupCount(){
//TODO自动生成的方法存根
返回1;
}
@凌驾
公共长getGroupId(int-groupPosition){
//TODO自动生成的方法存根
返回0;
}
@凌驾
公共视图getGroupView(int-groupPosition,布尔值isExpanded,
视图(视图、视图组父级){
//TODO自动生成的方法存根
TextView TextView=新的TextView(showquick.this);
setText(“哪个是家畜?”);
返回文本视图;
}
@凌驾
公共布尔表ID(){
//TODO自动生成的方法存根
返回false;
}
@凌驾
公共布尔值isChildSelectable(int-groupPosition,int-childPosition){
//TODO自动生成的方法存根
返回false;
}
}
类选项
{
ArrayList optns;
选项()
{