2013-10-04 35 views
0

無法從展開式列表視圖中的子項獲取值。我在列表中有兩個 參數..無法從展開式列表視圖中的子項獲取值

私人地圖> ExpCollection;私人 列出筆記本電腦;

並嘗試從價格TextView中的getChildView()和setText()中獲取ExpCollection的值。其投擲空指針異常..

請幫助...

適配器代碼: -

public class ExpandableListAdapter extends BaseExpandableListAdapter { 

    private Activity context; 
    private Map<String, List<String>> ExpCollection; 
    private List<String> laptops; 


    public ExpandableListAdapter(Activity context, List<String> laptops, 
      Map<String, List<String>> ExpCollection) { 
     this.context = context; 
     this.ExpCollection = ExpCollection; 
     this.laptops = laptops; 
    } 

    public Object getChild(int groupPosition, int childPosition) { 
     return ExpCollection.get(laptops.get(groupPosition)).get(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 String laptop = (String) getChild(groupPosition, childPosition); 

     LayoutInflater inflater = context.getLayoutInflater(); 

     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.child_item, null); 
     } 
     TextView item = (TextView) convertView.findViewById(R.id.laptop); 
     TextView price = (TextView) convertView.findViewById(R.id.runTime1); 
     TextView discountPrice = (TextView) convertView.findViewById(R.id.runTime); 
     System.out.println("ExpCollection******** "+ExpCollection.toString()); 
    // price.setText(ExpCollection.get("price").toString()); 
     //discountPrice.setText(ExpCollection.get("discountPrice").toString()); 

     RelativeLayout delete = (RelativeLayout) convertView 
       .findViewById(R.id.layout); 

     item.setText(laptop); 
     return convertView; 
    } 

    public int getChildrenCount(int groupPosition) { 
     return ExpCollection.get(laptops.get(groupPosition)).size(); 
    } 

    public Object getGroup(int groupPosition) { 
     return laptops.get(groupPosition); 
    } 

    public int getGroupCount() { 
     return laptops.size(); 
    } 

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

    public View getGroupView(int groupPosition, boolean isExpanded, 
      View convertView, ViewGroup parent) { 
     String laptopName = (String) getGroup(groupPosition); 
     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.group_item, null); 
     } 
     TextView item = (TextView) convertView.findViewById(R.id.laptop); 
     item.setTypeface(null, Typeface.BOLD); 
     item.setText(laptopName); 
     return convertView; 
    } 

    public boolean hasStableIds() { 
     return true; 
    } 

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

請發表您的logcat那麼只有一個能夠提供你的問題的解決方案。 – GrIsHu

+0

您是否嘗試過調試您的代碼並檢查「laptop」值是否爲空? – GrIsHu

回答

0

使用如下:

 if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater)this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.child_item, null); 
相關問題