ListView有"空白"行

时间:2015-06-04 13:46:23

标签: android android-layout

我的列表视图大小没有根据适配器进行更新,我不知道为什么 (这是在我根据某个类别进行过滤后发生的,因此我也会发布类别过滤器) 在查看图像后,您可以看到已过滤的选项卡中有额外的空白行。 " All"的图像标签: enter image description here

enter image description here

这是我的getView方法:

public View getView(int position,View view,ViewGroup parent) {
        LayoutInflater inflater = ctx.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.list_item, null, true);
        try {
            Item i = getForName(names.get(position));

            if (i.isCategory()) {
                rowView = inflater.inflate(R.layout.cat_item, null, true);
                TextView text = (TextView) rowView.findViewById(R.id.catTxt);
                text.setText(i.getTitle());
                text.setTypeface(null, Typeface.BOLD);
            } else {
                TextView text = (TextView) rowView.findViewById(R.id.listText);
                ImageView image = (ImageView) rowView.findViewById(R.id.listImage);
                try {
                    text.setText(names.get(position));
                    image.setImageResource(imageIds.get(position));
                } catch (Exception e) {
                }
            }
        } catch(Exception a ) {
             a.printStackTrace();
        }
        return rowView;
    };

这是类别过滤方法:

public void filterCategories(Context context) {
        ArrayList<String> currNames = new ArrayList<>(); //List of the names in the current category (When we start looping)
        ArrayList<Integer> currImaages = new ArrayList<>(); //List of the drawable IDs in the current category (When we start looping)
        ArrayList<String> n = new ArrayList<>(); //Will be the finalized names list
        ArrayList<Integer > is = new ArrayList<>(); //Will be the finalized id list
        for(String name : names) {
            Item item = getForName(name);
            if(item != null)
                if(item.isCategory()) {
                    String cat = item.getAcronym();
                    for(String n1 : names) {
                        Item i1 = getForName(n1);
                        if(i1 != null && !i1.isCategory() && i1.getParent() != null && !currNames.contains(i1.getTitle())) {
                            if(i1.getParent().equals(cat)) {
                                currNames.add(n1);
                                currImaages.add(context.getResources().getIdentifier(getForName(n1).getThumbnail(), "drawable", context.getPackageName()));
                            }
                        }
                    }
                    n.add(name);
                    is.add(0);
                    int c = 0;
                    for(String x : currNames) {
                        n.add(x);
                        is.add(currImaages.get(c));
                        c++;
                    }

                }
            currNames = new ArrayList<>();
            currImaages = new ArrayList<>();
        }
        names = new ArrayList<>();
        imageIds = new ArrayList<>();
        for(String a : n) {
            names.add(a);
        }
        for(Integer a : is) {
            imageIds.add(a);
        }
        notifyDataSetChanged();
    }

这里是a.printStackTrace()的输出:

06-04 09:51:38.835  17900-17900/packagename W/System.err﹕ java.lang.IndexOutOfBoundsException: Invalid index 9, size is 7
06-04 09:51:38.835  17900-17900/packagename W/System.err﹕ at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
06-04 09:51:38.835  17900-17900/packagename W/System.err﹕ at java.util.ArrayList.get(ArrayList.java:308)
06-04 09:51:38.835  17900-17900/packagename W/System.err﹕ at packagename.util.ListAdapter.getView(ListAdapter.java:46)
06-04 09:51:38.835  17900-17900/packagename W/System.err﹕ at android.widget.AbsListView.obtainView(AbsListView.java:2387)
06-04 09:51:38.836  17900-17900/packagename W/System.err﹕ at android.widget.ListView.makeAndAddView(ListView.java:1864)
06-04 09:51:38.836  17900-17900/packagename W/System.err﹕ at android.widget.ListView.fillDown(ListView.java:698)
06-04 09:51:38.836  17900-17900/packagename W/System.err﹕ at android.widget.ListView.fillSpecific(ListView.java:1356)
06-04 09:51:38.836  17900-17900/packagename W/System.err﹕ at android.widget.ListView.layoutChildren(ListView.java:1651)
06-04 09:51:38.836  17900-17900/packagename W/System.err﹕ at android.widget.AbsListView.onLayout(AbsListView.java:2191)
06-04 09:51:38.836  17900-17900/packagename W/System.err﹕ at android.view.View.layout(View.java:15654)
06-04 09:51:38.836  17900-17900/packagename W/System.err﹕ at android.view.ViewGroup.layout(ViewGroup.java:4967)
06-04 09:51:38.836  17900-17900/packagename W/System.err﹕ at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1705)
06-04 09:51:38.836  17900-17900/packagename W/System.err﹕ at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1559)
06-04 09:51:38.836  17900-17900/packagename W/System.err﹕ at android.widget.LinearLayout.onLayout(LinearLayout.java:1468)
06-04 09:51:38.836  17900-17900/packagename W/System.err﹕ at android.view.View.layout(View.java:15654)
06-04 09:51:38.836  17900-17900/packagename W/System.err﹕ at android.view.ViewGroup.layout(ViewGroup.java:4967)
06-04 09:51:38.836  17900-17900/packagename W/System.err﹕ at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:907)
06-04 09:51:38.836  17900-17900/packagename W/System.err﹕ at android.view.View.layout(View.java:15654)
06-04 09:51:38.836  17900-17900/packagename W/System.err﹕ at android.view.ViewGroup.layout(ViewGroup.java:4967)
06-04 09:51:38.836  17900-17900/packagename W/System.err﹕ at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
06-04 09:51:38.836  17900-17900/packagename W/System.err﹕ at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
06-04 09:51:38.836  17900-17900/packagename W/System.err﹕ at android.view.View.layout(View.java:15654)
06-04 09:51:38.836  17900-17900/packagename W/System.err﹕ at android.view.ViewGroup.layout(ViewGroup.java:4967)
06-04 09:51:38.836  17900-17900/packagename W/System.err﹕ at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:493)
06-04 09:51:38.836  17900-17900/packagename W/System.err﹕ at android.view.View.layout(View.java:15654)
06-04 09:51:38.836  17900-17900/packagename W/System.err﹕ at android.view.ViewGroup.layout(ViewGroup.java:4967)
06-04 09:51:38.836  17900-17900/packagename W/System.err﹕ at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
06-04 09:51:38.836  17900-17900/packagename W/System.err﹕ at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
06-04 09:51:38.836  17900-17900/packagename W/System.err﹕ at android.view.View.layout(View.java:15654)
06-04 09:51:38.836  17900-17900/packagename W/System.err﹕ at android.view.ViewGroup.layout(ViewGroup.java:4967)
06-04 09:51:38.836  17900-17900/packagename W/System.err﹕ at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1705)
06-04 09:51:38.836  17900-17900/packagename W/System.err﹕ at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1559)
06-04 09:51:38.837  17900-17900/packagename W/System.err﹕ at android.widget.LinearLayout.onLayout(LinearLayout.java:1468)
06-04 09:51:38.837  17900-17900/packagename W/System.err﹕ at android.view.View.layout(View.java:15654)
06-04 09:51:38.837  17900-17900/packagename W/System.err﹕ at android.view.ViewGroup.layout(ViewGroup.java:4967)
06-04 09:51:38.837  17900-17900/packagename W/System.err﹕ at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
06-04 09:51:38.837  17900-17900/packagename W/System.err﹕ at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
06-04 09:51:38.837  17900-17900/packagename W/System.err﹕ at android.view.View.layout(View.java:15654)
06-04 09:51:38.837  17900-17900/packagename W/System.err﹕ at android.view.ViewGroup.layout(ViewGroup.java:4967)
06-04 09:51:38.837  17900-17900/packagename W/System.err﹕ at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2102)
06-04 09:51:38.837  17900-17900/packagename W/System.err﹕ at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1859)
06-04 09:51:38.837  17900-17900/packagename W/System.err﹕ at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1078)
06-04 09:51:38.837  17900-17900/packagename W/System.err﹕ at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5875)
06-04 09:51:38.837  17900-17900/packagename W/System.err﹕ at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
06-04 09:51:38.837  17900-17900/packagename W/System.err﹕ at android.view.Choreographer.doCallbacks(Choreographer.java:580)
06-04 09:51:38.837  17900-17900/packagename W/System.err﹕ at android.view.Choreographer.doFrame(Choreographer.java:550)
06-04 09:51:38.837  17900-17900/packagename W/System.err﹕ at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
06-04 09:51:38.837  17900-17900/packagename W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
06-04 09:51:38.837  17900-17900/packagename W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
06-04 09:51:38.837  17900-17900/packagename W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
06-04 09:51:38.841  17900-17900/packagename W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5299)
06-04 09:51:38.841  17900-17900/packagename W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
06-04 09:51:38.841  17900-17900/packagename W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
06-04 09:51:38.841  17900-17900/packagename W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
06-04 09:51:38.841  17900-17900/packagename W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

4 个答案:

答案 0 :(得分:1)

问题是您处理的getView方法有误。在这里,我将添加一个示例代码。

第1步 你需要一个像这样的viewHolder类。在这里添加你的膨胀的xml小部件:

 /********* Create a holder Class to contain inflated xml file elements *********/
         public static class ViewHolder{

             public TextView text;
             public TextView text1;
             public TextView textWide;
             public ImageView image;

         }

第2步

在你的getView方法

/****** Depends upon data size called for each row , Create each ListView row *****/


         public View getView(int position, View convertView, ViewGroup parent) {

                 View vi = convertView;
                 ViewHolder holder;

                 if(convertView==null){

                     /****** Inflate tabitem.xml file for each row ( Defined below ) *******/
                     vi = inflater.inflate(R.layout.tabitem, null);

                     /****** View Holder Object to contain tabitem.xml file elements ******/

                     holder = new ViewHolder();
                     holder.text = (TextView) vi.findViewById(R.id.text);
                     holder.text1=(TextView)vi.findViewById(R.id.text1);
                     holder.image=(ImageView)vi.findViewById(R.id.image);

                    /************  Set holder with LayoutInflater ************/
                     vi.setTag( holder );
                 }
                 else 
                     holder=(ViewHolder)vi.getTag();
     return vi;
    }

这是示例问任何疑问

答案 1 :(得分:1)

在arraylist或hashmap中添加您的值并发送到您的Adapter。使用ViewHolder实现有效的ui元素可访问性。

答案 2 :(得分:1)

找到答案,原来我是个白痴。

只需要覆盖getCount

@Override
    public int getCount() {
        return names.size();
    }

答案 3 :(得分:0)

我无法从您发布的代码中看到,但我认为您的适配器没有看到任何更改,因为它仍然引用您使用的原始列表。过滤时,不要为适配器创建新列表,而是更新原始列表。

所以,而不是使用它:

ArrayList<String> n = new ArrayList<>(); //Will be the finalized names list

刚刚更新了names数组

更多信息:只是一个有用的提示,您的getView()每次都会让视图膨胀而且价格昂贵。一种流行的方法是使用viewHolder模式。 Lars Vogella有一个很棒的教程: http://www.vogella.com/tutorials/AndroidListView/article.html#adapterperformance_holder