9
में समर्थित नहीं है

मैं विस्तार ListView उदाहरण शुद्धjava.lang.UnsupportedOperationException: addView (देखें, LayoutParams) AdapterView

गतिविधि पर पाया उपयोग कर रहा हूँ:

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

एडाप्टर:

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> 

मैं इस समाधान की कोशिश की है, लेकिन काम नहीं किया :(

Android: Custom ListAdapter extending BaseAdapter crashes on application launch

इसके अशक्त inflater.inflate (R.layout.group_row, "गलत" पर एक तिहाई पैरामीटर जोड़ने के लिए, कहा, झूठे); एक XML लेआउट में AdapterView बच्चों (जैसे 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> 

उपवर्गों (जैसे आप अपने लेआउट में किया था):

+0

क्या यह गतिविधि उस सभी कोड में है? दूसरे प्रश्न का कोड 'inflater.inflate (R.layout.group_row, parent, false) है;' – Luksprog

+0

क्षमा करें, मैंने आपकी टिप्पणी नहीं समझा .... लेकिन जब मैं लॉग का उपयोग करके डीबगिंग कर रहा था, तो त्रुटि setContentView (R के साथ है .layout.main) गतिविधि में ... इसलिए मैंने प्रश्न में main.xml चिपकाया ... क्या आप मदद कर सकते हैं? –

उत्तर

17

ExpandableListView तत्व बाहर TextView ले जाएँ।

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

true उपयोग न करें:

0

तुम मुझे पसंद कर रहे हैं और ExpandableListView का उपयोग किए बिना इस सवाल का पाया, तो inflate विधि के लिए तीसरे पैरामीटर जोड़ने का उपयोग कर, भी here उल्लेख कोशिश के रूप में @Luksprog का उल्लेख है false के बजाय या आपको वही त्रुटि मिल जाएगी जो आपको यहां लाएगी, क्योंकि यह इसे माता-पिता को जोड़ने/जोड़ने की कोशिश कर रही है, इसलिए addView is not supported in AdapterView त्रुटि।

संबंधित मुद्दे