Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果使用来自服务器端的数据填充listview,如何使用适配器获取对象的特定元素_Java_Android_Expandablelistview - Fatal编程技术网

Java 如果使用来自服务器端的数据填充listview,如何使用适配器获取对象的特定元素

Java 如果使用来自服务器端的数据填充listview,如何使用适配器获取对象的特定元素,java,android,expandablelistview,Java,Android,Expandablelistview,我有一个ExpandableListView,它是使用来自服务器端(reftrofit)的数据填充的。我如何使用适配器来获取对象的特定元素?可展开列表视图包含父元素和子元素。单击子元素时,我希望提取与该子元素关联的ID(SensorID) 我有一个适配器,试图从对象中提取ID,但无法。我注意到,对于数组适配器使用getItemAtPosition()方法获取索引,然后从对象中提取元素(sensorID)。 但是,我不确定这是否是应用于ExpandableListAdapter的相同方法,因为我看

我有一个
ExpandableListView
,它是使用来自服务器端(reftrofit)的数据填充的。我如何使用
适配器来获取对象的特定元素?可展开列表视图包含父元素和子元素。单击子元素时,我希望提取与该子元素关联的ID(SensorID)

我有一个
适配器
,试图从对象中提取ID,但无法。我注意到,对于数组
适配器
使用
getItemAtPosition()
方法获取索引,然后从对象中提取元素(sensorID)。 但是,我不确定这是否是应用于
ExpandableListAdapter
的相同方法,因为我看到了使用方法
getChild()
的代码片段

每次单击时我都会看到错误

无法将ExpandableListConnector强制转换为 com.xera.deviceinsight.home.ExpandableListAdapter

所讨论的方法

 private void load(View view)
   {

      expListView = (ExpandableListView) view.findViewById(R.id.lvExp);
      prepareListData();
      listAdapter = new 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();
            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;

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

   @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 OrganisationDeviceSensorsResult getItemAtPosition(int index)
   {
      return getItemAtPosition(index);
      //return this.getItem(index);
      //return this.getItem(index);
      //return index;
      //return this.get
      //return OrganisationDeviceSensorsResult.
   }

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

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


}
公共类ExpandableListAdapter扩展了BaseExpandableListAdapter{
私人语境(private Context)(私人语境);;
私有列表_listDataHeader;//头标题
//标题标题、子标题格式的子数据
私有HashMap_listDataChild;
公共清单项目;
公共ExpandableListAdapter(上下文、列表listDataHeader、,
HashMap listChildData){
这._context=context;
这。_listDataHeader=listDataHeader;
这。_listDataChild=listChildData;
}
@凌驾
公共对象getChild(int-groupPosition、int-ChildPosition){
返回此。_listDataChild.get(此。_listDataHeader.get(groupPosition))
.get(childpositionon);
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition){
返回子位置;
}
公共组织设备传感器结果getItemAtPosition(int索引)
{
返回getItemAtPosition(索引);
//返回此.getItem(索引);
//返回此.getItem(索引);
//收益指数;
//把这个还给我,快
//返回组织设备传感器结果。
}
@凌驾
公共视图getChildView(int groupPosition,final int childPosition,
布尔值isLastChild、视图转换视图、视图组父级){
最终字符串childText=(字符串)getChild(groupPosition,childPosition);
组织设备传感器结果设备传感器=getItemAtPosition(childPosition);
if(convertView==null){
LayoutInflater infalInflater=(LayoutInflater)this.\u上下文
.getSystemService(上下文布局\充气机\服务);
convertView=infalInflater.充气(R.layout.list_项,空);
}
TextView txtListChild=(TextView)convertView
.findviewbyd(R.id.lblListItem);
setText(childText);
返回视图;
}
@凌驾
公共整数getChildrenCount(整数组位置){
返回此。_listDataChild.get(此。_listDataHeader.get(groupPosition))
.size();
}
@凌驾
公共对象getGroup(int-groupPosition){
返回此。\u listDataHeader.get(groupPosition);
}
@凌驾
public int getGroupCount(){
返回此值。_listDataHeader.size();
}
@凌驾
公共长getGroupId(int-groupPosition){
返回组位置;
}
@凌驾
公共视图getGroupView(int-groupPosition,布尔值isExpanded,
视图(视图、视图组父级){
字符串头文件=(字符串)getGroup(groupPosition);
if(convertView==null){
LayoutInflater infalInflater=(LayoutInflater)this.\u上下文
.getSystemService(上下文布局\充气机\服务);
convertView=infalInflater.充气(R.layout.list_组,空);
}
TextView lblistheader=(TextView)convertView
.findviewbyd(R.id.lblistheader);
lblListHeader.setTypeface(null,Typeface.BOLD);
lblListHeader.setText(标题);
返回视图;
}
@凌驾
公共布尔表ID(){
返回false;
}
@凌驾
公共布尔值isChildSelectable(int-groupPosition,int-childPosition){
返回true;
}
}

我或多或少了解了您的情况,我在放置listview的活动中使用了以下代码:

        contactListExpandable = (ExpandableListView) findViewById(R.id.contactlist);

        contactListAdapter = new ContactListAdapter(this, chatGroupList);
        contactListExpandable.setAdapter(contactListAdapter);
    contactListExpandable.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View view, int groupPosition, int childPosition, long id) {
            ChatContact contact = chatGroupList.get(groupPosition).getContacts().get(childPosition);
            launchChat(contact, null, false, "");
            return false;
        }
    });
在我的例子中,我让子对象启动聊天,等等,在你的例子中,你将获得传感器对象并完成你的工作

==聊天组===

public class ChatGroup  implements Comparable{
    final public String name;
    private List<ChatContact> contacts;

    public ChatGroup(String name){
        this.name = name;
        this.contacts = new ArrayList<>();
    }

    public void addContact(ChatContact contact){
        this.contacts.add(contact);
    }

    public List<ChatContact> getContacts(){
        return this.contacts;
    }

    @Override
    public int compareTo(Object o) {
        ChatGroup cg = (ChatGroup)o;
        return name.compareTo(cg.name);
    }
}

您好,您的聊天组列表是什么?我有一行代码引用活动
listAdapter=new ExpandableListAdapter(this.getActivity(),listDataHeader,listDataChild)@Zidane抱歉耽搁了。我的chatGroupList是我给适配器的列表,它包含要显示的对象。在你的例子中,你有2个ist、listDataHeader和ListDataChild,getContacts方法是唯一让我困惑的东西,否则其他一切都有意义。你能给我一个链接到一个例子吗?@Zidane我添加了类ChatGroup和ChatContact。变量chatGroupList为-->列表chatGroupList=。。。。;所以,我有一个聊天组对象列表。当我单击子对象时,我获取,首先指向对象引用(父对象),然后使用getContacts方法获取该组的联系人,并选择位置中的子对象。
    public class ChatContact implements Parcelable, Comparable {
        final public String name;
        final public String jid;
        public Status status;

        public ChatContact(String name, String jid) {
            this.name = name;
            this.jid = jid;
        }
        .....getters/setters/etc.....
   }