我试图在我的可扩展ListView中只显示我的子数据一次。但遗憾的是,如下面的屏幕截图所示,它不止一次显示:
编辑:我已在StoreArrayAdapter.java
和AllStores.java
中修改了我的代码。我还添加了新的屏幕截图,但我仍然无法仅显示一次我的子数据。但是我认为我的代码现在看起来更清晰了。
在getChildView
方法中,我使用了以下代码行:
final Store store = (Store) getChild(groupPosition, groupPosition);
在下面的屏幕截图中,您可以看到孩子不止一次出现,这是我不想要的。我只想让孩子一次出现。
链接到屏幕截图:http://i.imgur.com/LIJSvvC.png
在getChildView
方法中,我也尝试使用以下代码行:
final Store store = (Store) getChild(groupPosition, childPosition);
在下面的屏幕截图中,您可以看到其他商店的ID。所以没有成功。
链接到屏幕截图:http://i.imgur.com/AcTrDrG.png
下面你可以看到我尝试过的东西。这是代码:
StoreArrayAdapter.java:
public class StoreArrayAdapter extends BaseExpandableListAdapter {
private List<ParentStore> parentStoresList;
private Context context;
public StoreArrayAdapter(Context context, List<ParentStore> parentStoresList) {
this.context = context;
this.parentStoresList = parentStoresList;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
final ParentStore parentStore = (ParentStore) getGroup(groupPosition);
TextView tvID = (TextView) convertView.findViewById(R.id.txtId);
tvID.setText("Facebook ID: " + parentStore.getChildStoresList().get(groupPosition).getFacebookID());
TextView tvName = (TextView) convertView.findViewById(R.id.txtName);
tvName.setText(parentStore.getChildStoresList().get(groupPosition).getName());
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
final Store store = (Store) getChild(groupPosition, groupPosition);
//final Store store = (Store) getChild(groupPosition, childPosition);
TextView title = (TextView) convertView.findViewById(R.id.lblListItem);
title.setText(store.getFacebookID());
return convertView;
}
//More code..
}
下面你可以看到整个StoreArrayAdapter.java
,以防你想看到它。
StoreArrayAdapter.java:
链接到代码:http://pastebin.com/dDn5g2dp
如果您也想看我如何填充parentStoresList
中的AllStores.java
。然后你可以在下面的链接中看到它。
AllStores.java:
答案 0 :(得分:0)
确定您的填充列表方法,首先将商店添加为位置i。当再次遍历这些商店列表时,这可能会产生一些不良后果。我只会使用没有位置的add方法。
编辑:如此:
private void populateList() throws InterruptedException, ExecutionException, IOException {
subprises = new StoreService().getSubprises();
it = subprises.body().iterator();
parentStore = new ParentStore();
parentStoresList = new ArrayList<>();
while(it.hasNext()) {
Store store = (Store) it.next();
parentStore.getChildStoresList().clear();
if (store != null) {
parentStore.getChildStoresList().add(store);
}
parentStoresList.add(parentStore);
}
//The logic of putting this outside the loop means they will all the stores will be part of one parent
}
在尝试检索父商店及其各自的商店对象时,父商店始终处于商家位置,子商店将始终处于子商务位置
Store theStoreImAfter = parentStoresList.get(groupPosition).getChildStoresList().get(childPosition)
编辑:
我第一次没注意到的另一个错误:将声明的父存储放在循环之外,或者清除以前添加到父存储的结果,如下所示:
while (it.hasNext()) {
parentStore.getChildStoresList().clear();
//Continue...
}
此外,当您在getGroupView和getChildView中膨胀视图时,您不会将它们绑定到任何内容。他们应该更像:
convertView = infalInflater.inflate(R.layout.list_group, parent, false);
convertView = infalInflater.inflate(R.layout.list_item, parent, false);
有时会导致其他代码部分出现怪癖
我再次编辑填充列表方法的代码