如何获得从列表视图列表项的计数? [英] How to get a count of list items from Listview?

查看:114
本文介绍了如何获得从列表视图列表项的计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面code到斯格特我的名单这是工作的罚款,如果列表项是可见的。

如果名单是滚动的话不可见的项目不访问使用此code。如何遍历所有的列表项这是visble +不可见的项目。

 的for(int i = 0; I< list.getCount;我++)
{
   视图v = list.getChildAt(ⅰ);
   TextView的电视=(TextView中)v.findViewById(R.id.item);
   Log.d(,列表项:+ tv.getText());
}
 

解决方案

最近我做了这件事。 假设我想无形上的listItem一个按钮。然后在列表适配器的getView添加在全局矢量该按钮。如下图所示。

 按钮del_btn = viewCache.getFrame();
 view_vec.add(del_btn);
 

下面视图缓冲是视图缓冲级,这是sumthing像下面的一个对象 -

 类视图缓冲
     {

         私人查看基本视角;
         私人Button按钮;


         公共视图缓冲(查看基本视点)
         {
             this.baseView =基本视角;
         }

         公共按钮getButton(){
             如果(键== NULL){
                 按钮=(按钮)baseView.findViewById(R.id.DeleteChatFrndBtn);
             }
             返回按钮;
         }
     }


 //有必要有时因为否则在某些情况下,该列表的滚动速度很慢。
 

现在你想可见的listItem的按钮onClicking其他一些按钮。然后,code是像下面 -

 公共无效的onClick(视图v){
    // TODO自动生成方法存根

    开关(v.getId()){

        案例R.id.EditChatFrndBtn:
            长度= view_vec.size();
            的for(int i = 0; I<长度;我++){

                按钮BTN =(按钮)view_vec.elementAt(ⅰ);
                btn.setVisibility(View.VISIBLE);

            }
            doneBtn.setVisibility(View.VISIBLE);
            editBtn.setVisibility(View.INVISIBLE);
            打破;
     }
}
 

而不是R.id.EditChatFrndBtn把你的按钮标识上点击它,你会无形/有形ListItem中的按钮。

I'm using below code to travers my list it is working fine if List Items are visible.

If List is scrollable then non visible items are not accessing using this code. How to traverse all the list items which are visble + non visible items.

for(int i=0;i<list.getCount;i++)
{
   View v = list.getChildAt(i);
   TextView tv= (TextView) v.findViewById(R.id.item);
   Log.d("","ListItem :"+tv.getText());
}

解决方案

Recently i did this thing. Suppose i want to invisible a button on a listItem. Then in the getView of the list adapter add that button in a global vector. like below.

 Button del_btn = viewCache.getFrame();
 view_vec.add(del_btn);

Here viewCache is a object of ViewCache class, which is sumthing like below -

   class ViewCache 
     {        

         private View baseView;     
         private Button button;


         public ViewCache(View baseView) 
         {       
             this.baseView = baseView;   
         }  

         public Button getButton() {
             if(button == null) {
                 button = (Button)   baseView.findViewById(R.id.DeleteChatFrndBtn);
             }
             return button;
         }
     }  


 //it is necessary sometimes because otherwise in some cases the list scroll is slow. 

Now you like to visible the listItem's button onClicking some other button. Then the code is like below -

 public void onClick(View v) {
    // TODO Auto-generated method stub

    switch(v.getId()) {

        case R.id.EditChatFrndBtn:
            length = view_vec.size();
            for(int i = 0; i < length; i++) {

                Button btn = (Button) view_vec.elementAt(i);
                btn.setVisibility(View.VISIBLE);

            }
            doneBtn.setVisibility(View.VISIBLE);
            editBtn.setVisibility(View.INVISIBLE);
            break;
     }
}

Instead of R.id.EditChatFrndBtn put your button id on clicking of which you will invisible/visible the listItem's button.

这篇关于如何获得从列表视图列表项的计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆