2012-09-06 17 views
0

這就像他們根本就沒有。它編譯,但每個組中都沒有可見或不可選的內容。這裏是截圖:可擴展的ListView兒童不會顯示

enter image description here

這是我的Activity類。爲孩子們充氣的xml幾乎沒有在那裏。

package com.anthonyce.mcathomie; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseExpandableListAdapter; 
import android.widget.ExpandableListView; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class PlayoptionsActivity extends Activity { 
ExpandableListView Mtopics; 
ExpandableAdapter MtopicsAdapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_playoptions); 
    //set up adapter 
    MtopicsAdapter = new ExpandableAdapter(); 
    Mtopics = (ExpandableListView) findViewById(R.id.mtopicsListView); 
    Mtopics.setAdapter(MtopicsAdapter); 
    //Mtopics.setGroupIndicator(null); 
} 

public class ExpandableAdapter extends BaseExpandableListAdapter { 
     private String[] groups = { "People Names", "Dog Names", "Cat Names", 
     "Fish Names" }; 
private String[][] children = { { "Arnold" }, { "Ace" }, { "Fluffy" }, 
     { "Goldy" } }; 

    public Object getChild(int groupPosition, int childPosition) { 
      return children[groupPosition][childPosition]; 
    } 

    public long getChildId(int groupPosition, int childPosition) { 
      return childPosition; 
    } 

    public View getChildView(final int groupPosition, final int childPosition, 
      boolean isLastChild, View convertView, ViewGroup parent) { 

     final class ExpandChildRow extends LinearLayout { 
      public ExpandChildRow(Context context) { 
       super(context); 
       LayoutInflater childInflate = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       LinearLayout childView = (LinearLayout) childInflate.inflate(R.layout.mtopics_childrow, this, true); 

       TextView childtxt = (TextView)childView.findViewById(R.id.mtopicchildtext); 
       childtxt.setText(getChild(groupPosition, childPosition).toString()); 
      } 
     } 

     ExpandChildRow ChildRow = new ExpandChildRow(getBaseContext()); 
     return ChildRow; 
    } 

    public int getChildrenCount(int groupPosition) { 
     return children[groupPosition].length; 
    } 

    public Object getGroup(int groupPosition) { 
     return groups[groupPosition]; 
    } 

    public int getGroupCount() { 
     return groups.length; 
    } 

    public long getGroupId(int groupPosition) { 
     return groupPosition; 
    } 

    public View getGroupView(final int groupPosition, boolean isExpanded, 
      View convertView, ViewGroup parent) { 

     final class ExpandGroupRow extends LinearLayout { 
      public ExpandGroupRow(Context context) { 
       super(context); 
       LayoutInflater groupInflate = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       LinearLayout groupView = (LinearLayout) groupInflate.inflate(R.layout.mtopics_grouprow, this, true); 

       TextView grouptxt = (TextView)groupView.findViewById(R.id.mtopicgrouptext); 
       grouptxt.setText(getGroup(groupPosition).toString()); 
      } 
     } 

     ExpandGroupRow GroupRow = new ExpandGroupRow(getBaseContext()); 
     return GroupRow; 

    } 

    public boolean hasStableIds() { 
     return true; 
    } 

    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return true; 
    } 

} 
} 

回答

0

哦,我很高興。它表現出來的原因是因爲複選框在組別行的任何地方與組分隔符或類似的東西衝突。也許組分隔線本身就是一個複選框。我只是刪除複選框,一切都再次運作。我想我的佈局XML文件需要修訂。

1

我認爲這個問題是這個:class ExpandGroupRow extends LinearLayout 您正在嘗試使用這個類的UI元素,但在構造函數this你不設置佈局PARAMS。 使用由inflater而不是此類返回的LinearLayout可能更容易。例如:

public View getGroupView(final int groupPosition, boolean isExpanded, 
     View convertView, ViewGroup parent) { 

      LayoutInflater groupInflate = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      LinearLayout groupView = (LinearLayout) groupInflate.inflate(R.layout.mtopics_grouprow, parent, false); 

      TextView grouptxt = (TextView)groupView.findViewById(R.id.mtopicgrouptext); 
      grouptxt.setText(getGroup(groupPosition).toString()); 
      return groupView; 

} 
+0

感謝您的協助。說實話,我也喜歡這樣做,但如果我這樣做,我會得到上下文的錯誤。我不確定我是否可以正確設置構造函數。我確實是一個初學者 – mango

+0

哦,只需在上面的代碼中用'getBaseContext()'代替'context'就行。 – Sameer

+0

對不起,我想通了。我真笨。 – mango