获取自定义列表视图Android中的行数总数

时间:2013-04-26 01:57:23

标签: android android-layout listview android-listview android-custom-view

我正在尝试获取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());
}

这是我的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(); 
        }
}

1 个答案:

答案 0 :(得分: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,因此addDropList中的getCount()仍为空数组,因此getCount() == 0 ,这就是为什么你会看到0显示。

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