且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取的行数的总数在自定义列表视图的Andr​​oid

更新时间:2023-10-13 20:11:58

好吧,我觉得我知道是怎么回事。你设置你的的ListView 并添加了 TransactionAddDropAdapter ,然后设置项目的总金额。

  this.addDropListView =(ListView控件)view.findViewById(R.id.transactions_pending_transactionsListView);
this.addDropAdapter =新TransactionAddDropAdapter(pendingTradesView);
this.addDropListView.setAdapter(this.addDropAdapter);TextView的getTotalCount =(TextView中)view.findViewById(R.id.transactions_pending_TransactionsAddDropCount);
getTotalCount.setText(+ addDropListView.getCount());

不过,在这一点上,你有没有叫 setAddDropList(列表< TransactionAddDrop> addDropList) addDropAdapter ,所以 addDropList getCount将()仍然是一个空数组,所以 getCount将()= = 0 ,这就是为什么你看到显示0。

所以,你需要找到你叫 addDropAdapter.setAddDropList(),然后调用 getTotalCount.setText(+ addDropListView.getCount( )); 权后,以更新的行总数

I am trying to getting the total Count of the getView custom listview. But want to display in the different layout. Here is my onCreatedView. I am not sure how to inflate the layout. Thanks for all your help.

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

Here is my Holderview that get the 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(); 
        }
}

Ok, I "think" I know what's going on here. You setup your ListView and add its TransactionAddDropAdapter, and then set the total amount of items.

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

However, at this point, you haven't called setAddDropList(List<TransactionAddDrop> addDropList) on addDropAdapter, so addDropList in getCount() is still an empty array, so getCount() == 0, which is why you are seeing 0 being displayed.

So, you need to find where you call addDropAdapter.setAddDropList() and then call getTotalCount.setText(""+addDropListView.getCount()); right after in order to update the total number of rows.