获取的行数的总数在自定义列表视图的Android [英] Get the total count of number of rows in the Custom Listview Android
问题描述
我试图得到getView定制列表视图的总数。但要在不同的布局来显示。这是我的onCreatedView。我不知道如何膨胀的布局。感谢您的帮助。
私有静态的ListView addDropListView = NULL;
私人TransactionAddDropAdapter addDropAdapter = NULL;公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){fragmentPendingTrades = inflater.inflate(R.layout.fragment_transactions_pending,集装箱,FALSE);
pendingTradesView =吹气;返回fragmentPendingTrades;
}公共无效onViewCreated(最终查看视图,最终捆绑savedInstanceState){this.addDropListView =(ListView控件)view.findViewById(R.id.transactions_pending_transactionsListView);
this.addDropAdapter =新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
公共类TransactionAddDropAdapter延伸BaseAdapter { 私人LayoutInflater吹气= NULL;
私人列表< TransactionAddDrop> addDropList =新的ArrayList< TransactionAddDrop>(); 公共TransactionAddDropAdapter(LayoutInflater气筒){
this.inflater =吹气;
} 公共无效setAddDropList(列表< TransactionAddDrop> addDropList){
clearAddDropList(); 对于(TransactionAddDrop广告:addDropList){
如果(ad.isStateApprove()){
this.addDropApprovalsList.add(广告);
}其他{
this.addDropList.add(广告);
}
}
} 公共无效clearAddDropList(){
this.addDropList.clear();
this.addDropApprovalsList.clear();
} @覆盖
公众诠释的getCount(){
INT大小= this.addDropList.size(); 如果(this.addDropApprovalsList.size()大于0){
大小+ = 1;
} 返回的大小;
} @覆盖
公共对象的getItem(INT位置){
尝试{
如果(this.addDropList == NULL){
返回null;
}否则如果(仓位< addDropList.size()){
返回this.addDropList.get(位置);
}其他{
返回this.addDropApprovalsList;
}
}赶上(例外五){
返回null;
}
} @覆盖
众长getItemId(INT为arg0){
// TODO自动生成方法存根
返回0;
} @覆盖
公共查看getView(最终诠释的立场,观点convertView,
父母的ViewGroup){ 最后TransactionAddDrop addDropData = this.addDropList.get(位置); TransactionAddDropViewHolder支架=无效;
如果(convertView == NULL){
convertView = inflater.inflate(R.layout.fragment_pending_transaction_list_item,NULL);
持有人=新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(保持器);
}其他{
支架=(TransactionAddDropViewHolder)convertView.getTag();
holder.swipeButtons();
}
}
好吧,我觉得我知道是怎么回事。你设置你的的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.
这篇关于获取的行数的总数在自定义列表视图的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!