与列表视图适配器问题 [英] Problems with Listview adapter

查看:95
本文介绍了与列表视图适配器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用一个ListView显示讲座的应用程序。该讲座是根据其类型的颜色codeD。我用了一个自定义适配器来控制每个讲座不同的颜色。我用下面的code调用适配器 -

I have an app which uses a Listview to display Lectures. The Lectures are colour coded according to their type. I used a custom adapter to control the different colours of each lecture. I call the adapter using the code below -

    cursor = myDbHelper.getReadableDatabase().rawQuery(sql, null);       
    startManagingCursor(cursor);
    adapter = new Lectures_Adapter(this,R.layout.menu_item,cursor,FROM,TO);        
    menuList.setAdapter(adapter);

这一切工作正常,直到我重新排序的讲座,按位置说。在code我用的是 -

This all works ok until I re-order the Lectures, say by Location. The code I use is -

    Cursor newCursor = myDbHelper.getReadableDatabase().rawQuery(sqlStr, null);
    adapter.changeCursor(newCursor);

在我的自定义适配器(Lectures_Adapter)的code以下,但经过重新排序的讲座时不叫。

The code in my custom adapter (Lectures_Adapter) is below but is not called when the Lectures are re-ordered.

     public class Lectures_Adapter extends SimpleCursorAdapter {
         private Context appContext;
         private int layout;
         private Cursor mycursor;

         public Lectures_Adapter(Context context, int layout, Cursor c, String[] from,int[] to) {
              super(context, layout, c, from, to);
              this.appContext=context;
              this.layout=layout;
              this.mycursor=c;               
         }

         @Override
         public View getView(int position, View convertView, ViewGroup parent)     
         {   
              View view = super.getView(position, convertView, parent);   
              try {             
                 if (position > 0)
                 {               
                    RelativeLayout rowFill = (RelativeLayout) convertView.findViewById(R.id.rowFill);
                    String title = mycursor.getString(1);                
                    int myColor = 0;
                    int myPos = title.indexOf("Nursing");
                    int myPos2 = title.indexOf("Masterclass");
                    if (myPos >= 0)
                    {
                        myColor = Color.parseColor("#99FF66");
                    }
                    else if (myPos2  >= 0)
                    {
                        myColor = Color.parseColor("#FF99FF");
                    }
                    else
                    {
                        myColor = Color.parseColor("#FFFF66");
                    }
                    convertView.findViewById(R.id.rowFill).setBackgroundColor(myColor);                 
                  }         
               }catch(Exception e) {

               }

              if (convertView == null) {
                  LayoutInflater inflator = (LayoutInflater) this.appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                  convertView = inflator.inflate(this.layout,null);
              } else {
                  convertView = (View) convertView;
              }
              return view;  
          }

       }

有人可以告诉我怎样才能重新排序动态我的ListView和打电话给我,每次自定义适配器。

Can somebody please tell me how I can re-order my Listview dynamically and call me custom adapter each time.

推荐答案

现在的问题是,在getView()的code从类变量获取数据 mycursor ,但是当你调用changeCursor,在 mycursor 变量没有得到更新,所以你仍然可以看到原来的清单。而不是使用 mycursor ,你应该叫 getCursor()代替。

The problem is that your code in getView() gets its data from the class variable mycursor, but when you call changeCursor, the mycursor variable is not getting updated, so you still see the original list. Rather than using mycursor, you should call getCursor() instead.

这篇关于与列表视图适配器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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