1

我想獲取getView自定義列表視圖的總計數。但要顯示在不同的佈局。這是我的onCreatedView。我不知道如何膨脹佈局。感謝你的幫助。獲取自定義列表視圖中的總行數Android

private static ListView addDropListView = null; 
private TransactionAddDropAdapter addDropAdapter = null; 

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { 

fragmentPendingTrades = inflater.inflate(R.layout.fragment_transactions_pending, container, false); 
pendingTradesView = inflater; 

return fragmentPendingTrades; 
} 

public void onViewCreated(final View view, final Bundle savedInstanceState) { 

this.addDropListView = (ListView) view.findViewById(R.id.transactions_pending_transactionsListView); 
this.addDropAdapter = new TransactionAddDropAdapter(pendingTradesView); 
this.addDropListView.setAdapter(this.addDropAdapter); 
this.emptyTransationsContainer = view.findViewById(R.id.transactions_pending_transactions_emptyContainer); 



TextView getTotalCount = (TextView) view.findViewById(R.id.transactions_pending_TransactionsAddDropCount); 

getTotalCount.setText(""+addDropListView.getCount()); 
} 

這裏是我的Holderview是得到getView

public class TransactionAddDropAdapter extends BaseAdapter { 

    private LayoutInflater inflater = null; 
    private List<TransactionAddDrop> addDropList = new ArrayList<TransactionAddDrop>(); 

    public TransactionAddDropAdapter(LayoutInflater inflater) { 
     this.inflater = inflater; 
    } 

    public void setAddDropList(List<TransactionAddDrop> addDropList) { 
     clearAddDropList(); 

     for (TransactionAddDrop ad : addDropList) { 
      if (ad.isStateApprove()) { 
       this.addDropApprovalsList.add(ad); 
      } else { 
       this.addDropList.add(ad); 
      } 
     } 
    } 

    public void clearAddDropList() { 
     this.addDropList.clear(); 
     this.addDropApprovalsList.clear(); 
    } 

    @Override 
    public int getCount() { 
     int size = this.addDropList.size(); 

     if (this.addDropApprovalsList.size() > 0) { 
      size += 1; 
     } 

     return size; 
    } 

    @Override 
    public Object getItem(int position) { 
     try { 
      if (this.addDropList == null) { 
       return null; 
      } else if (position < addDropList.size()) { 
       return this.addDropList.get(position); 
      } else { 
       return this.addDropApprovalsList; 
      } 
     } catch (Exception e) { 
      return null; 
     } 
    } 

    @Override 
    public long getItemId(int arg0) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public View getView(final int position, View convertView, 
      ViewGroup parent) { 

     final TransactionAddDrop addDropData = this.addDropList.get(position); 



     TransactionAddDropViewHolder holder = null; 
     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.fragment_pending_transaction_list_item, null); 
      holder = new TransactionAddDropViewHolder(); 

      holder.withdrawButton = convertView.findViewById(R.id.pendingTransactionItem_withdrawButton); 

      holder.addContainer = (LinearLayout) convertView.findViewById(R.id.pendingTransactionItem_addContainer); 
      holder.dropContainer = (LinearLayout) convertView.findViewById(R.id.pendingTransactionItem_dropContainer); 
      holder.rootView = convertView.findViewById(R.id.swipeRight); 
      holder.swipeButtons(); 
      convertView.setTag(holder); 
     } else { 
      holder = (TransactionAddDropViewHolder) convertView.getTag(); 
      holder.swipeButtons(); 
     } 
} 
+0

首先,它看起來像你有「應該」編譯的代碼。我假設它顯示了不正確的行數,對吧?如果是這樣,它顯示了什麼,你期望什麼?其次,你在'getCount'方法中試圖通過在大小上加1來做什麼? – 2013-04-26 12:15:05

+0

嗨史蒂文,其編譯正確。我得到正確的行數。我只是試圖顯示頂部標題部分中的總行數。無視添加一個大小我不使用任何地方。 – dhiku 2013-04-26 13:45:55

+0

那麼現在,它在做什麼?顯示「0」?沒有?其他號碼? – 2013-04-26 14:09:39

回答

1

好吧,我 「覺得」 我知道是怎麼回事。您設置您的ListView並添加其TransactionAddDropAdapter,然後設置項目的總量。

this.addDropListView = (ListView) view.findViewById(R.id.transactions_pending_transactionsListView); 
this.addDropAdapter = new TransactionAddDropAdapter(pendingTradesView); 
this.addDropListView.setAdapter(this.addDropAdapter); 

TextView getTotalCount = (TextView) view.findViewById(R.id.transactions_pending_TransactionsAddDropCount); 
getTotalCount.setText(""+addDropListView.getCount()); 

然而,在這一點上,你有沒有叫上addDropAdaptersetAddDropList(List<TransactionAddDrop> addDropList),所以addDropListgetCount()仍然是一個空數組,所以getCount() == 0,這就是爲什麼你看到0被顯示。

因此,您需要找到您撥打addDropAdapter.setAddDropList()的地方,然後立即致電getTotalCount.setText(""+addDropListView.getCount());以更新總行數。

+0

嗨史蒂文,感謝您的幫助隊友..它的工作。看起來我需要在SetAddDropList之後的更新顯示中添加。謝謝。 – dhiku 2013-04-26 15:19:14

+0

**編輯**真棒:) – 2013-04-26 15:23:12