Using custom simpleCursorAdapter

安稳与你 提交于 2019-11-27 11:59:24

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));

            }

    }
zek

you better write these lines of code in your customadapter getview function. It work for me and will work for you, it's simple.

if (convertView == null) {
    music_column_index = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
    myCursor.moveToPosition(position);
    id = myCursor.getString(music_column_index);
    music_column_index = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
    myCursor.moveToPosition(position);
    id += " Size(KB):" + myCursor.getString(music_column_index);
    Log.d("TAG", "id::" + id);
    tv.setText(id);
} else
    tv = (TextView) convertView;
    return tv;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!