2015-12-21 16 views
0

我想在我的可擴展ListView中顯示我的孩子數據只有一次。但不幸的是它顯示不止一次,如你在下面的屏幕截圖中看到的:試圖顯示我的孩子數據只有一次在可擴展列表查看

編輯:我編輯了我的代碼StoreArrayAdapter.javaAllStores.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

如果你想太多看我如何在AllStores.java填充我parentStoresList。然後你可以在下面的鏈接中看到它。

AllStores.java:

鏈接代碼:http://pastebin.com/JQ8iWktu

+0

東西的代碼是不正確你'populateList ()'方法。我應該看到兩個循環,一個用於父數據,另一個用於子數據。你只有一個循環。我看不到數據中的父母/子女關係;對我來說,它看起來像你正在從父列表中創建一個子列表。 –

+0

@krislarson我編輯了'populateList()'的實現。我認爲父母/子女關係現在看起來更清楚。然而,我還沒有辦法只向我的孩子展示一次。你能再看看我的代碼嗎? – superkytoz

回答

0

確定在填充列表的方法,你是我首先是增加了商店的位置。再次遍歷這些商店列表時,這可能會帶來一些不好的結果。我只是使用沒有位置的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) 

編輯:

我沒有注意到第一次另一個錯誤:將外循環聲明父​​店或清除了上10的結果先生增加母公司店,像這樣:

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

這有時會導致怪癖其他代碼段

我再次將編輯填充列表方法

+0

哦,然後改變getChildView方法,然後正確調用groupPosition和childPosition。之前它可能試圖抓取位置0和1時,索引59和60處的項目存在(即使它只有2個項目) – KoalaKoalified

+0

我已經嘗試過您的代碼,並在我的'getChildView'方法中使用以下代碼行:'final Store store =(Store)getChild(groupPosition,childPosition);'不幸的是,它沒有工作,這是結果:鏈接到屏幕截圖:http://i.imgur.com/k0Vd2mC.png – superkytoz

+0

好的檢查編輯和重新實現填充列表並在適配器中進行一些更改 – KoalaKoalified

相關問題