1

我正在尝试获取 getView 自定义列表视图的总数。但想在不同的布局中显示。这是我的 onCreatedView。我不知道如何膨胀布局。感谢你的帮助。

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

这是我获取 getView 的 Holderview

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

1 回答 1

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

但是,此时您还没有调用setAddDropList(List<TransactionAddDrop> addDropList)addDropAdapter所以addDropListingetCount()仍然是一个空数组,所以getCount() == 0,这就是您看到显示 0 的原因。

因此,您需要找到您调用的位置addDropAdapter.setAddDropList(),然后立即调用getTotalCount.setText(""+addDropListView.getCount());以更新总行数。

于 2013-04-26T14:42:58.463 回答