2017-01-11 10 views
0

サーバー側からは、基本的にさまざまな要素を含むオブジェクトが戻ってきます。私は1つの要素(sensorID)を抽出するのに助けが必要です。sensorIDを抽出するコードがexpandableListViewを使用しているので、ちょっと変わっています。 私は以下のコードを試してみましたが、私は読み込みエラーを取得する多くの要素を含むオブジェクトからオブジェクトの1つの要素を抽出するには

にjava.lang.ClassCastException:java.lang.Stringでは com.xera.deviceinsight.api.OrganisationDeviceSensorsResultにキャストすることはできません

private void load(View view) 
    { 
     final Context context = getContext(); 
     expListView = (ExpandableListView) view.findViewById(R.id.lvExp); 
     prepareListData(); 
     //listAdapter = new ExpandableListAdapter(this.getActivity(), listDataHeader, listDataChild); 
     listAdapter = new com.xera.deviceinsight.home.ExpandableListAdapter(this.getActivity(), listDataHeader, listDataChild); 
     expListView.setAdapter(listAdapter); 
     expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
     @Override 
     public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { 

      System.err.println("child clicked"); 
      Toast.makeText(getActivity(), "child clicked", Toast.LENGTH_SHORT).show(); 
      EventBus.getDefault().post(new ItemClickedEvent(SensorInformationChildFragment.TAB_CALL)); 
      //ExpandableListAdapter adapter = (ExpandableListAdapter)parent.getAdapter(); 
      ExpandableListAdapter adapter = (ExpandableListAdapter)parent.getExpandableListAdapter(); 
      Object sensorData = adapter.getChild(groupPosition , childPosition); 
      OrganisationDeviceSensorsResult deviceSensor = (OrganisationDeviceSensorsResult) adapter.getChild(groupPosition , childPosition); 
      sensorID = deviceSensor.SensorID; 

      ReportingGroup.get(childPosition); 
      return true; 
     } 
     }); 
    } 

私のアダプタクラス

public class ExpandableListAdapter extends BaseExpandableListAdapter { 

    private Context _context; 
    private List<String> _listDataHeader; // header titles 
    // child data in format of header title, child title 
    private HashMap<String, List<String>> _listDataChild; 
    public List<OrganisationDeviceSensorsResult> Items; 
    List<String> ReportingGroup= new ArrayList<String>(); 

    public ExpandableListAdapter(Context context, List<String> listDataHeader, 
           HashMap<String, List<String>> listChildData) { 
     this._context = context; 
     this._listDataHeader = listDataHeader; 
     this._listDataChild = listChildData; 
    } 

    public ExpandableListAdapter(Context context) { 
    } 

    @Override 
    public Object getChild(int groupPosition, int childPosititon) { 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
      .get(childPosititon); 
    } 

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

    public Object getItemAtPosition(int index) 
    { 
     return getItemAtPosition(index); 
     //return this.getItem(index); 
    // return this.getChild(index , index2); 
     //return this.getItem(index); 
     //return index; 
     //return this.get 
     //return OrganisationDeviceSensorsResult.class; 
    } 

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

     final String childText = (String) getChild(groupPosition, childPosition); 
     //OrganisationDeviceSensorsResult deviceSensor = getItemAtPosition(childPosition); 
     if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) this._context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.list_item, null); 
     } 

     TextView txtListChild = (TextView) convertView 
      .findViewById(R.id.lblListItem); 

     txtListChild.setText(childText); 
     String child=(String) getChild(groupPosition, childPosition); 
    // int s = deviceSensor.SensorID; 
    // Log.i(TAG, "value selected: " + deviceSensor.SensorID); 
     return convertView; 
    } 

    /*private OrganisationDeviceSensorsResult getItemAtPosition(int childPosition) { 
    }*/ 

    @Override 
    public int getChildrenCount(int groupPosition) { 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
      .size(); 
    } 

    @Override 
    public Object getGroup(int groupPosition) { 
     return this._listDataHeader.get(groupPosition); 
    } 

    @Override 
    public int getGroupCount() { 
     return this._listDataHeader.size(); 
    } 

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

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, 
          View convertView, ViewGroup parent) { 
     String headerTitle = (String) getGroup(groupPosition); 
     if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) this._context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.list_group, null); 
     } 

     TextView lblListHeader = (TextView) convertView 
      .findViewById(R.id.lblListHeader); 
     lblListHeader.setTypeface(null, Typeface.BOLD); 
     lblListHeader.setText(headerTitle); 

     return convertView; 
    } 

    @Override 
    public boolean hasStableIds() { 
     return false; 
    } 

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


    public void get(int groupPosition) { 
    } 


    /*public int getName() { 
     return name; 
    }*/ 
} 
+0

'adapter.getChild(groupPosition、childPosition)の戻りタイプは、'ものです? – jhamon

答えて

0

次のコードは、クラスキャスト例外を回避すべきです。エラーは、OrganisationDeviceSensorsResultオブジェクトに文字列をキャストしようとしているようです。

+0

@Goldfishアダプタクラスを追加しました。 @ SrikanthあなたのコードはsensorIDを抽出しますが、値は常に0. – Zidane

+0

です。唯一の問題は、この行が文字列を返すことが欲しいということです。 'return this._listDataChild.get(this._listDataHeader.get(groupPosition)) .get childPosititon); ' – Zidane

+0

@ジダン - 答えたものをマークしてください。 –

2

編集:_listDataHeaderリストと_listDataChild HashMapの型はStringです。したがって、getChildを呼び出すと、OrganisationDeviceSensorsResultオブジェクトではなくStringが返されます。私は変更することをお勧めあなたのアダプタクラスを見ることができず


@Override 
public Object/String getChild(int groupPosition, int childPosition) { 
    return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
     .get(childPosititon); 
} 

@Override 
public OrganisationDeviceSensorsResult getChild(int groupPosition, int childPosition) { 
    return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
     .get(childPosititon); 
} 
関連する問題