9

Tôi đang sử dụng có thể mở rộng ví dụ ListView tìm thấy trên netjava.lang.UnsupportedOperationException: addView (Xem, LayoutParams) không được hỗ trợ trong AdapterView

Hoạt động:

public class ExpandableListViewActivity extends ExpandableListActivity { 
    /** 
    * strings for group elements 
    */ 
    static final String arrGroupelements[] = { "India", "Australia", "England", 
      "South Africa" }; 

    /** 
    * strings for child elements 
    */ 
    static final String arrChildelements[][] = { 
      { "Sachin Tendulkar", "Raina", "Dhoni", "Yuvi" }, 
      { "Ponting", "Adam Gilchrist", "Michael Clarke" }, 
      { "Andrew Strauss", "kevin Peterson", "Nasser Hussain" }, 
      { "Graeme Smith", "AB de villiers", "Jacques Kallis" } }; 

    DisplayMetrics metrics; 
    int width; 
    ExpandableListView expList; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     expList = getExpandableListView(); 
     metrics = new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(metrics); 
     width = metrics.widthPixels; 
     // this code for adjusting the group indicator into right side of the 
     // view 
     expList.setIndicatorBounds(width - GetDipsFromPixel(50), width 
       - GetDipsFromPixel(10)); 
     expList.setAdapter(new ExpAdapter(this)); 

     expList.setOnGroupExpandListener(new OnGroupExpandListener() { 
      public void onGroupExpand(int groupPosition) { 
       Log.e("onGroupExpand", "OK"); 
      } 
     }); 

     expList.setOnGroupCollapseListener(new OnGroupCollapseListener() { 
      public void onGroupCollapse(int groupPosition) { 
       Log.e("onGroupCollapse", "OK"); 
      } 
     }); 

     expList.setOnChildClickListener(new OnChildClickListener() { 
      public boolean onChildClick(ExpandableListView parent, View v, 
        int groupPosition, int childPosition, long id) { 
       Log.e("OnChildClickListener", "OK"); 
       return false; 
      } 

     }); 
    } 

    public int GetDipsFromPixel(float pixels) { 
     // Get the screen's density scale 
     final float scale = getResources().getDisplayMetrics().density; 
     // Convert the dps to pixels, based on density scale 
     return (int) (pixels * scale + 0.5f); 
    } 
} 

Adaptor:

public class ExpAdapter extends BaseExpandableListAdapter { 

    private Context myContext; 

    public ExpAdapter(Context context) { 
     myContext = context; 
    } 

    public Object getChild(int groupPosition, int childPosition) { 
     return null; 
    } 

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

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

     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) myContext 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.child_row, null); 
     } 

     TextView tvPlayerName = (TextView) convertView 
       .findViewById(R.id.tvPlayerName); 
     tvPlayerName 
       .setText(ExpandableListViewActivity.arrChildelements[groupPosition][childPosition]); 

     return convertView; 
    } 

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

    public Object getGroup(int groupPosition) { 
     return null; 
    } 

    public int getGroupCount() { 
     return ExpandableListViewActivity.arrGroupelements.length; 
    } 

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

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

     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) myContext 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.group_row, null); 
     } 

     TextView tvGroupName = (TextView) convertView 
       .findViewById(R.id.tvGroupName); 
     tvGroupName 
       .setText(ExpandableListViewActivity.arrGroupelements[groupPosition]); 

     return convertView; 
    } 

    public boolean hasStableIds() { 
     return false; 
    } 

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

main.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <ExpandableListView 
     android:id="@+id/android:list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:groupIndicator="@drawable/group_indicator" > 

     <TextView 
      android:id="@+id/android:empty" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:text="No Items" > 
     </TextView> 
    </ExpandableListView> 

</LinearLayout> 

tôi đã cố gắng giải pháp này, nhưng đã không làm việc :(

Android: Custom ListAdapter extending BaseAdapter crashes on application launch

có nó bảo phải thêm một tham số thứ ba "false", đến inflater.inflate (R.layout.group_row, null, false);

+0

Đây có phải là tất cả mã trong hoạt động đó không? Mã từ câu hỏi khác là 'inflater.inflate (R.layout.group_row, parent, false);' – Luksprog

+0

xin lỗi tôi đã không hiểu nhận xét của bạn .... nhưng khi tôi đang gỡ lỗi bằng cách sử dụng nhật ký, lỗi là với setContentView (R .layout.main) trong hoạt động ... vì vậy tôi đã dán tệp main.xml vào câu hỏi ... bạn có thể trợ giúp không? –

Trả lời

17

Di chuyển TextView ngoài yếu tố ExpandableListView:

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <ExpandableListView 
     android:id="@+id/android:list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:groupIndicator="@drawable/group_indicator" > 
    </ExpandableListView> 
    <TextView 
      android:id="@+id/android:empty" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:text="No Items" > 
    </TextView> 
</LinearLayout> 

lớp con của AdapterView (như ExpandableListView) không thể có con trong một bố cục xml (như bạn đã làm trong cách bố trí của bạn).

0

Nếu bạn giống như tôi và tìm thấy câu hỏi này mà không sử dụng ExpandableListView, hãy thử sử dụng thêm tham số thứ ba với phương pháp inflate, như @Luksprog đề cập, cũng đề cập đến here:

inflater.inflate(R.layout.group_row, parent, false); 

Không sử dụng true thay vì false hoặc bạn sẽ gặp lỗi tương tự đã đưa bạn đến đây, vì nó đang cố gắng đính kèm/thêm nó vào cấp độ gốc, do đó lỗi addView is not supported in AdapterView.

Các vấn đề liên quan