2012-03-19 51 views
0

嗨,我有一個表「TABLE_OBJECTIVE」與1個問題和4個選項,我接受從
EditText。我的問題是如何在可擴展列表視圖中將此問題顯示爲父項,並將4選項顯示爲子項
。我使用一些代碼,但它不顯示問題和僅顯示
4個選項之一。
這是我的示例代碼。從SQLite中檢索數據到可擴展列表視圖?

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; 
} 

我真的不明白我的代碼中的問題在哪裏。
請給我一些提示或參考。
這裏是只顯示一個項目的模擬器的圖像。
我有4個選項(Google,Samsung,Nokia,Onida)。
在此先感謝。
enter image description here

回答

2

你好,請試試這個代碼其工作正常,我的一個問題,但爲我所用數組列表和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); 
     } 
    } 
} 
相關問題