Listview自定义适配器

时间:2012-04-21 08:55:30

标签: android sqlite listview simplecursoradapter custom-adapter

我正在使用SimpleCursorAdapter从SQLite数据库中检索数据,并使用自定义布局和listview来显示数据库中的两个字符串。现在我有两个问题:

  1. 如果我想根据特定条件在特定行中显示星标,是否必须创建自定义适配器?我已将图像设置为在我的自定义布局中不可见,并希望根据行数据本身的某些条件将其设置为可见。我实现了所有选项卡以及收藏夹选项卡,所有工作正常,我只需要星形图标。我也将配方图像放到特定的行上来解决这个问题。

  2. 在listviews中动态获取图片的最佳方法是什么?我遵循了懒惰的图像教程,但我不知道如何使用CustomCursorAdapter实现它,因为它是使用baseadapter实现的。使用simplecursor教程有什么链接到延迟加载图像?

  3. public class AlternateRowCursorAdapter extends SimpleCursorAdapter{
        int layoutn;
        Cursor mCursor;
        String[] fromn;
        int[] ton;
    
        LayoutInflater mInflater;
    
        private int[] colors = new int[] { Color.parseColor("#000000"), Color.parseColor("#303030") };
    
        public AlternateRowCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
            super(context, R.layout.listtype, c, from, to);
            this.mCursor = c;
        }
    
        /**
         * Display rows in alternating colors
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            View view = super.getView(position, convertView, parent);
    
            ImageView star = (ImageView)view.findViewById(R.id.favoritesicon); // The star I want to show
    
            if (mCursor.getString(8) == "YES") // shows if the item is in favorites
            {
                star.setVisibility(view.VISIBLE);
            }
    
            int colorPos = position % colors.length;
            view.setBackgroundColor(colors[colorPos]);
    
            return view;
        }
    }
    

1 个答案:

答案 0 :(得分:1)

  

我是否必须创建自定义适配器?

是的,您必须创建自定义适配器并在行中添加星标....

  

在listviews中动态获取图片的最佳方法是什么?

我认为你应该使用通用图像加载器。请参阅GitHub项目 Universal Image Loader for Android