使用自定义simpleCursorAdapter

时间:2013-07-17 19:51:06

标签: android listview android-listview cursor simplecursoradapter

我正在尝试使用自定义适配器访问列表活动。我已经直接尝试了它而没有使用任何自定义适配器它工作正常,但因为我想在列表视图中添加更多功能我想实现自定义适配器。现在我已经尝试了但是我得到一个没有数据可见的空列表视图。 列表 - 活性

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

2 个答案:

答案 0 :(得分:13)

扩展游标适配器时,应覆盖方法bindViewnewView。 bindView方法用于将所有数据绑定到给定视图,例如在TextView上设置文本。 newView方法用于给新视图充气并返回它,此时不会将任何数据绑定到视图。大多数适配器使用getView函数,但在扩展游标适配器时,应使用bindView和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));

            }

    }

答案 1 :(得分:1)

您最好在customadapter getview函数中编写这些代码行。它对我有用,对你有用,很简单。

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