【问题标题】:how do I extract a single element of an object from an object which contains many elements如何从包含许多元素的对象中提取对象的单个元素
【发布时间】:2017-05-26 08:18:13
【问题描述】:

从服务器端我基本上得到了一个包含各种元素的对象。我在提取一个元素(sensorID)时需要帮助我很确定我的提取 sensorID 的代码必须稍作更改,因为我使用的是可扩展列表视图。 我已经尝试了下面的代码,但我收到一个错误,显示为

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;
   }*/
}

【问题讨论】:

  • adapter.getChild(groupPosition , childPosition)的返回类型是什么?

标签: java json object adapter expandablelistview


【解决方案1】:

编辑:您的 _listDataHeader 列表和 _listDataChild HashMap 属于字符串类型。因此,当您调用 getChild 时,它返回的是 String 而不是 OrganisationDeviceSensorsResult 对象。


如果无法看到您的适配器类,我建议您更改:

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

【讨论】:

    【解决方案2】:

    下面的代码应该避免类转换异常。该错误看起来您正在尝试将字符串强制转换为 OrganisationDeviceSensorsResult 对象。

    Object obj = adapter.getChild(groupPosition , childPosition);
    
    if(obj instanceof OrganisationDeviceSensorsResult){
    OrganisationDeviceSensorsResult deviceSensor = (OrganisationDeviceSensorsResult)obj;
    sensorID = deviceSensor.SensorID;
    }
    

    【讨论】:

    • @Goldfish 我已经添加了我的适配器类。 @Srikanth 您的代码提取了 sensorID 但值始终为 0.0
    • 好吧,唯一的问题是这一行想要返回一个字符串`return this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosititon);`
    • @Zidane - 标记为已回答,哪个回答有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多