2017-01-10 39 views
0

我有一個使用來自serverside(reftrofit)的數據填充的ExpandableListView如何使用Adapter獲取對象的特定元素?可展開列表視圖包含父元素和子元素。當我點擊子元素時,我想提取一個與該子元素關聯的ID(SensorID)。如何使用適配器獲取對象的特定元素(如果使用來自服務器端的數據填充了列表視圖)

我有一個Adapter我試圖從對象中提取ID,但我無法。我注意到,對於數組Adapter而言,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; 
    } 


} 

回答

0

我已經或多或少你的情況,我用這個代碼在活動,其中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; 
     } 
    }); 

在我的情況,我得到的子對象發動聊天等,在你的情況,你會ABRE獲得傳感器對象,做你的東西

=== ChatGroup ===

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

=== ChatContact.java ===

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..... 
    } 
+0

嗨什麼是聊天組列表在你的情況?我有這行代碼引用活動,而不是。 'listAdapter = new ExpandableListAdapter(this.getActivity(),listDataHeader,listDataChild);' – Zidane

+0

@Zidane對不起延遲。我的chatGroupList是我給適配器的列表,它包含要顯示的對象。在你的情況下,你有2個ist,listDataHeader和listDataChild – Shudy

+0

getContacts方法是唯一讓我困惑的事情,否則其他一切都有意義 – Zidane

相關問題