gpt4 book ai didi

android - 使用自定义 simpleCursorAdapter

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:58:28 27 4
gpt4 key购买 nike

我正在尝试使用自定义适配器访问列表 Activity 。我已经在不使用任何自定义适配器的情况下直接进行了尝试,它运行良好,但因为我想在 ListView 中添加更多功能,所以我想实现一个自定义适配器。现在我已经尝试过了,但我得到的是一个空的 ListView ,没有任何数据可见。列表 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 方法用于将所有数据绑定(bind)到给定 View ,例如在 TextView 上设置文本。 newView 方法用于扩充新 View 并返回它,此时您不将任何数据绑定(bind)到 View 。大多数适配器使用 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));

}

}

关于android - 使用自定义 simpleCursorAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17708971/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com