我有一個使用來自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;
}
}
嗨什麼是聊天組列表在你的情況?我有這行代碼引用活動,而不是。 'listAdapter = new ExpandableListAdapter(this.getActivity(),listDataHeader,listDataChild);' – Zidane
@Zidane對不起延遲。我的chatGroupList是我給適配器的列表,它包含要顯示的對象。在你的情況下,你有2個ist,listDataHeader和listDataChild – Shudy
getContacts方法是唯一讓我困惑的事情,否則其他一切都有意義 – Zidane