Получите общее количество строк в Custom Listview Android

Я пытаюсь получить общее количество пользовательского списка 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(); 
        }
}

person dhiku    schedule 26.04.2013    source источник
comment
Во-первых, похоже, что у вас есть код, который нужно скомпилировать. Я предполагаю, что он показывает вам неправильное количество строк, верно? Если да, то что он показывает и чего вы ожидаете? Во-вторых, что вы пытаетесь сделать в своем методе getCount, добавляя единицу к размеру?   -  person Steven Byle    schedule 26.04.2013
comment
Привет Стивен, его компиляция правильная. И я получаю правильное количество строк. я просто пытаюсь отобразить общее количество строк в верхнем разделе заголовка. Не обращайте внимания на добавление одного к размеру, который я нигде не использую.   -  person dhiku    schedule 26.04.2013
comment
Итак, что он делает сейчас? Отображается 0? ничего такого? какой-то другой номер?   -  person Steven Byle    schedule 26.04.2013
comment
Просто показывает 0.   -  person dhiku    schedule 26.04.2013


Ответы (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());, чтобы обновить общее количество строк.

person Steven Byle    schedule 26.04.2013
comment
Привет, Стивен. Спасибо за помощь, приятель. Это сработало. Похоже, мне нужно добавить в обновление Display после SetAddDropList. Спасибо. - person dhiku; 26.04.2013