使用自定义 simpleCursorAdapter [英] Using custom simpleCursorAdapter

查看:21
本文介绍了使用自定义 simpleCursorAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用自定义适配器访问列表活动.我在不使用任何自定义适配器的情况下直接尝试了它,它运行良好,但是因为我想在列表视图中添加更多功能,所以我想实现一个自定义适配器.现在我已经尝试过了,但是我得到了一个没有数据可见的空列表视图.列表-活动

I am trying to access a list-activity using custom adapter.I have tried it directly without using any custom adapter it was working good but because I want to add more functions in list-view I want to implement a custom adapter.Now I have tried it but I am getting an empty list-view with no data visible. List-Activity

public class MainActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

         String[] projection = {
                    MediaStore.Audio.Media._ID,
                    MediaStore.Audio.Media.ARTIST,
                    MediaStore.Audio.Media.TITLE,
                    MediaStore.Audio.Media.DATA,
                    MediaStore.Audio.Media.DISPLAY_NAME,
                    MediaStore.Audio.Media.DURATION,

            };
            //query 

            musiccursor = this.managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,projection,selection,null,sortOrder);
            music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);


            int a[]= new int[]{R.id.TitleSong,R.id.Artist};


            Custom_Adapter adapter = new Custom_Adapter(this,R.layout.music_items, musiccursor, new String[]{MediaStore.Audio.Media.TITLE,MediaStore.Audio.Media.ARTIST} ,a);

            this.setAdapter(adapter);

            }
    }

自定义适配器

    public class Custom_Adapter extends SimpleCursorAdapter {


    private Context mContext;
    private Context appContext;
    private int layout;
    private Cursor cr;
    private final LayoutInflater inflater;

    public Custom_Adapter(Context context,int layout, Cursor c,String[] from,int[] to) {
        super(context,layout,c,from,to);
        this.layout=layout;
        this.mContext = context;
        this.inflater=LayoutInflater.from(context);
        this.cr=c;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // TODO Auto-generated method stub
        super.bindView(view, context, cursor);
     view=inflater.inflate(layout, null, false);
        TextView titleS=(TextView)view.findViewById(R.id.TitleSong);
        TextView artistS=(TextView)view.findViewById(R.id.Artist);
        int Title_index;
        int Artist_index;
        cursor.moveToFirst();
        while(cursor.isLast()){

            Title_index=cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
            Artist_index=cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST);
            titleS.setText(cursor.getString(Title_index));
            artistS.setText(cursor.getString(Artist_index));
            cr.moveToNext();
            }

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        return convertView;
    }
}

推荐答案

在扩展游标适配器时,您应该覆盖方法 bindViewnewView.bindView 方法用于将所有数据绑定到给定的视图,例如在 TextView 上设置文本.newView 方法用于膨胀新视图并返回它,此时您没有将任何数据绑定到视图.大多数适配器使用 getView 函数,但在扩展游标适配器时,您应该使用 bindView 和 newView.

When extending a cursor adapter you should override the methods bindView and newView. The bindView method is used to bind all data to a given view such as setting the text on a TextView. The newView method is used to inflate a new view and return it, you don't bind any data to the view at this point. Most adapters use the getView function but when extending a cursor adapter you should use bindView and newView.

    public class Custom_Adapter extends SimpleCursorAdapter {

            private Context mContext;
            private Context appContext;
            private int layout;
            private Cursor cr;
            private final LayoutInflater inflater;

            public Custom_Adapter(Context context,int layout, Cursor c,String[] from,int[] to) {
                super(context,layout,c,from,to);
                this.layout=layout;
                this.mContext = context;
                this.inflater=LayoutInflater.from(context);
                this.cr=c;
            }

            @Override
            public View newView (Context context, Cursor cursor, ViewGroup parent) {
                    return inflater.inflate(layout, null);
            }

            @Override
            public void bindView(View view, Context context, Cursor cursor) {
                super.bindView(view, context, cursor);
                TextView titleS=(TextView)view.findViewById(R.id.TitleSong);
                TextView artistS=(TextView)view.findViewById(R.id.Artist);

                int Title_index=cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
                int Artist_index=cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST);

                titleS.setText(cursor.getString(Title_index));
                artistS.setText(cursor.getString(Artist_index));

            }

    }

这篇关于使用自定义 simpleCursorAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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