使用自定义simpleCursorAdapter [英] Using custom simpleCursorAdapter
问题描述
我试图访问使用自定义adapter.I列表,活动已经尝试过的情况下直接使用的自定义适配器,这是工作不错,但因为我要在列表视图中添加更多的功能,我想实现一个自定义的适配器。现在,我已经尝试过,但我得到一个空列表视图没有数据显示。 清单效
公共类MainActivity扩展ListActivity {
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
!= 0字符串选择= MediaStore.Audio.Media.IS_MUSIC +;
的String []投影= {
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,
};
//查询
musiccursor = this.managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,投影,选择,空,排序顺序);
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
诠释一个[] =新的INT [] {R.id.TitleSong,R.id.Artist};
Custom_Adapter适配器=新Custom_Adapter(这一点,R.layout.music_items,musiccursor,新的String [] {MediaStore.Audio.Media.TITLE,MediaStore.Audio.Media.ARTIST},A);
this.setAdapter(适配器);
}
}
自定义适配器
公共类Custom_Adapter扩展SimpleCursorAdapter {
私人语境mContext;
私人语境appContext;
私人诠释布局;
私人光标CR;
私人最终LayoutInflater充气;
公共Custom_Adapter(上下文的背景下,INT布局,光标C,的String []从,INT []键){
超(背景下,布局,C,从,到);
this.layout =布局;
this.mContext =背景;
this.inflater = LayoutInflater.from(上下文);
this.cr = C;
}
@覆盖
公共无效bindView(查看视图,上下文的背景下,光标光标){
// TODO自动生成方法存根
super.bindView(查看,背景,光标);
鉴于= inflater.inflate(布局,空,假);
TextView的标题=(TextView中)view.findViewById(R.id.TitleSong);
TextView的艺术家=(TextView中)view.findViewById(R.id.Artist);
INT Title_index;
INT Artist_index;
cursor.moveToFirst();
而(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();
}
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
// TODO自动生成方法存根
返回convertView;
}
}
在延伸光标适配器,你应该覆盖的方法<一href="http://developer.android.com/reference/android/widget/CursorAdapter.html#bindView%28android.view.View,%20android.content.Context,%20android.database.Cursor%29">bindView和<一href="http://developer.android.com/reference/android/widget/CursorAdapter.html#newView%28android.content.Context,%20android.database.Cursor,%20android.view.ViewGroup%29">newView.所述bindView方法用于所有的数据以给定的视图绑定诸如设置上一个TextView的文本。该NewView的方法用于充气一个新的视图,并返回它,你没有任何数据绑定到视图在这一点上。大多数适配器使用getView功能,但扩展游标适配器时,你应该使用bindView和NewView的。
公共类Custom_Adapter扩展SimpleCursorAdapter {
私人语境mContext;
私人语境appContext;
私人诠释布局;
私人光标CR;
私人最终LayoutInflater充气;
公共Custom_Adapter(上下文的背景下,INT布局,光标C,的String []从,INT []键){
超(背景下,布局,C,从,到);
this.layout =布局;
this.mContext =背景;
this.inflater = LayoutInflater.from(上下文);
this.cr = C;
}
@覆盖
公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){
返回inflater.inflate(布局,NULL);
}
@覆盖
公共无效bindView(查看视图,上下文的背景下,光标光标){
super.bindView(查看,背景,光标);
TextView的标题=(TextView中)view.findViewById(R.id.TitleSong);
TextView的艺术家=(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));
}
}
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);
}
}
Custom-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;
}
}
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屋!