Android网格视图问题

时间:2013-06-07 22:21:43

标签: android gridview scroll simplecursoradapter

我是android开发的初学者。在我正在处理的应用程序中,使用默认的简单游标适配器(不对其进行子类化)将数据从数据库显示到列表视图。

如果用户需要,我想添加选项以在网格视图中显示数据。

我在网上查看了一些示例,它看起来与列表视图完全相同。因此,我再次使用简单的游标适配器从数据库中获取数据并将其显示在网格视图上。

首先正确显示所有项目,直到我开始滚动它。那时候一切都搞不定了。

每个网格视图项目都有2个文本视图,第一个显示其标题,第二个显示其描述。因此,在滚动时,描述会混淆,项目标题会出现错误的描述等。有时整个网格视图也会完全消失。

我的列表视图从未出现过这样的问题,它始终按预期工作。

我在这里缺少什么?非常感谢您的见解。

更新:

滚动时网格视图消失是由于网格视图项目的高度不均匀。

似乎我的另一个问题在于我为适配器设置的取景器。如果我注释掉以下代码,我不再在网格视图项之间得到混合。 这是viewbinder代码:

adapter.setViewBinder(new ViewBinder() {

public boolean setViewValue(View aView, Cursor aCursor, int aColumnIndex) {


    if (aColumnIndex == 3) {

           String color = aCursor.getString(aColumnIndex);
       aView.setBackgroundColor(Color.parseColor(color));

           return true;
         }

        if (aColumnIndex == 4) {

       if (aCursor.getString(5)!=null) {

          ViewGroup parentview = (ViewGroup)aView.getParent();
                       parentview.removeAllViews();

          TableLayout tablelayout = new TableLayout (MainActivity.this);
                       parentview.addView(tablelayout);

          int count = Integer.parseInt(aCursor.getString(5));

          String[] texts = convertStringToArray(aCursor.getString(4));
          String[] cb = convertStringToArray(aCursor.getString(6));

          for (int i =0;i<count;i++) {

           LayoutInflater inflater = getLayoutInflater();
           View checklist_row = inflater.inflate(R.layout.widget_row_layout, null);
           TextView tv = (TextView)checklist_row.findViewById(R.id.textView1);
           ImageView im = (ImageView)checklist_row.findViewById(R.id.imageView1);

           if (cb[i].equalsIgnoreCase("true"))
                im.setBackgroundResource(R.drawable.cbc);

        tv.setText(texts[i]);
        parentview.addView(checklist_row);

        }
         return true;
    }

    else {

       TextView tv = (TextView) aView.findViewById(R.id.textView2);
       tv.setText(aCursor.getString(4));

           return true;

         }

    }

    return false;

 }
});

有关如何解决此问题的任何想法?

1 个答案:

答案 0 :(得分:0)

我建议你的 getView()方法有问题。在Listview和gridview中使用适配器类来显示data.in android很多adapter.so我建议首先阅读如何使用适配器? 在这里,我给了一个链接,有助于理解适配器的使用。BaseAdapter并使用他的链接,我显示了如何使用 getView()方法的整个代码。Use of getView method.我希望你能解决问题。

修改

我建议在这里使用此链接使用游标适配器的简单示例。Simple example of cursor adapter以及与您的问题类似的此链接。Cursor Adapter with Gridview

我希望你能得到答案。 感谢。