我的Eclipse里面Simplecursoradapter优化怎么警告被弃用了

作为一个android新手,在绑定数据的时候是这样的
ListView listview=(ListView)this.findViewById(R.id.listView1);
XJDal xj=new XJDal(MainActivity.this);
Cursor cur=xj.Query();
ListAdapter ad=new SimpleCursorAdapter(this,
android.R.layout.simple_expandable_list_item_2,
new String[]{&timeflag&,&type&},
new int[]{android.R.id.text1,android.R.id.text2} );
listview.setAdapter(ad);
public Cursor Query()
return db.query(&XJ&, new String[]{&timeflag&,&type&}, null, null, null, null, null);
这个时候运行总是报错,其中有一行
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sclock/com.sclock.MainActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
缺少了一列_id。
_id是用来干什么的?
SimpleCursorAdapter只识别_id作为主键
所以我们需要把上面查询的代码修改一下,添加一个_id的值,如果你的数据库中没有的话可以将主键 as _id。
public Cursor Query()
return db.query(&XJ&, new String[]{&_id&,&timeflag&,&type&}, null, null, null, null, null);
阅读(...) 评论()近期评论功能1354人阅读
public class TTActivity extends ListActivity {
&&& private String[] mCursorC//columns
&&& TrackListAdapter mA
&&& Cursor mTrackC
&&& public void onCreate(Bundle savedInstanceState) {
&&&&&&& super.onCreate(savedInstanceState);
&&&&&&& setContentView(R.layout.main);
&&&&&&& mTrackCursor =
&&&&&&& mCursorCols = new String[] {
&&&&&&&&&&&&&&& MediaStore.Audio.Media._ID,
&&&&&&&&&&&&&&& MediaStore.Audio.Media.TITLE,
&&&&&&&&&&&&&&& MediaStore.Audio.Media.DATA,
&&&&&&&&&&&&&&& MediaStore.Audio.Media.ALBUM,
&&&&&&&&&&&&&&& MediaStore.Audio.Media.ARTIST,
&&&&&&&&&&&&&&& MediaStore.Audio.Media.ARTIST_ID,
&&&&&&&&&&&&&&& MediaStore.Audio.Media.DURATION
&&&&&&& };
&&&&&&& mAdapter = new TrackListAdapter(this, R.layout.track_list_item, null, new String[]{}, new int[]{},this);
&&&&&&& setListAdapter(mAdapter);//fill data
&&&&&&& getTrackCursor(mAdapter.getQueryHandler());
&&& Cursor getTrackCursor(TrackListAdapter.TrackQueryHandler queryHandler){
&&&&&&& StringBuilder where = new StringBuilder();
&&&&&&& where.append(MediaStore.Audio.Media.TITLE + & != ''&);
&&&&&&& where.append(& AND & + MediaStore.Audio.Media.IS_MUSIC + &=1&);
&&&&&&& ret = queryHandler.doQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
&&&&&&&&&&&&&&& mCursorCols, where.toString() , null, null);
&&& class TrackListAdapter extends SimpleCursorAdapter{
&&&&&&& TrackQueryHandler mQueryH
&&&&&&& TTActivity mActivity =
&&&&&&& public TrackListAdapter(Context context, int layout, Cursor c, String[] from,
&&&&&&&&&&&&&&& int[] to,TTActivity currentActivity) {
&&&&&&&&&&& super(context, layout, c, from, to);
&&&&&&&&&&& mQueryHandler = new TrackQueryHandler(context.getContentResolver());
&&&&&&&&&&& mActivity = currentA
&&&&&&&&&&& // TODO Auto-generated constructor stub
&&&&&&& @Override
&&&&&&& public void bindView(View view, Context context, Cursor cursor) {
&&&&&&&&&&& super.bindView(view, context, cursor);
&&&&&&&&&&& System.out.println(&bindView&);
&&&&&&&&&&& System.out.println(&duration:&+cursor.getString(cursor.getColumnIndexOrThrow(&duration&)));
&&&&&&&&&&& System.out.println(&title:&+cursor.getString(cursor.getColumnIndexOrThrow(&title&)));
&&&&&&&&&&& System.out.println(&data:&+cursor.getString(cursor.getColumnIndexOrThrow(&_data&)));
&&&&&&&&&&& ViewHolder vh = (ViewHolder) view.getTag();
&&&&&&&&&&& vh.line1.setText(cursor.getString(cursor.getColumnIndexOrThrow(&title&)));
&&&&&&&&&&& vh.line2.setText(cursor.getString(cursor.getColumnIndexOrThrow(&artist&)));
&&&&&&& @Override
&&&&&&& public void changeCursor(Cursor c) {//call this to trigger newView() and bindView()
&&&&&&&&&&& // TODO Auto-generated method stub
&&&&&&&&&&& super.changeCursor(c);
&&&&&&&&&&& System.out.println(&changeCursor&);
&&&&&&& @Override
&&&&&&& public View newView(Context context, Cursor cursor, ViewGroup parent) {
&&&&&&&&&&& // TODO Auto-generated method stub
&&&&&&&&&&& System.out.println(&newView&);
&&&&&&&&&&& View v = super.newView(context, cursor, parent);
&&&&&&&&&&& ViewHolder vh = new ViewHolder();
&&&&&&&&&&& vh.line1 = (TextView) v.findViewById(R.id.line1);
&&&&&&&&&&& vh.line2 = (TextView) v.findViewById(R.id.line2);
&&&&&&&&&&& v.setTag(vh);
&&&&&&&&&&&
&&&&&&& class ViewHolder {
&&&&&&&&&&& TextView line1;
&&&&&&&&&&& TextView line2;
&&&&&&& class TrackQueryHandler extends AsyncQueryHandler{
&&&&&&&&&&& public TrackQueryHandler(ContentResolver cr) {
&&&&&&&&&&&&&&& super(cr);
&&&&&&&&&&&&&&& // TODO Auto-generated constructor stub
&&&&&&&&&&& }
&&&&&&&&&&& Cursor doQuery(Uri uri, String[] projection,
&&&&&&&&&&&&&&&&&&& String selection, String[] selectionArgs,
&&&&&&&&&&&&&&&&&&& String orderBy) {
&&&&&&&&&&&&&&& System.out.println(&doQuery&);
&&&&&&&&&&&&&&& startQuery(0, null, uri, projection, selection, selectionArgs, orderBy);
&&&&&&&&&&&&&&&
&&&&&&&&&&& }
&&&&&&&&&&& @Override
&&&&&&&&&&& protected void onQueryComplete(int token, Object cookie,
&&&&&&&&&&&&&&&&&&& Cursor cursor) {//cursor: The cursor holding the results from the query
&&&&&&&&&&&&&&& // TODO Auto-generated method stub
&&&&&&&&&&&&&&& super.onQueryComplete(token, cookie, cursor);
&&&&&&&&&&&&&&& System.out.println(&onQueryComplete&);
&&&&&&&&&&&&&&& changeCursor(cursor);//call this, newView() and bindView() will execute
&&&&&&&&&&& }
&&&&&&&&&&&
&&&&&&& public TrackQueryHandler getQueryHandler() {
&&&&&&&&&&& return mQueryH
&&& public void init(Cursor cursor) {
&&&&&&& // TODO Auto-generated method stub
&&&&&&& mAdapter.changeCursor(cursor);
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:327239次
积分:2942
积分:2942
排名:第8101名
原创:33篇
转载:75篇
评论:35条
(2)(1)(1)(1)(1)(1)(2)(1)(4)(3)(3)(6)(3)(4)(4)(4)(6)(8)(2)(3)(7)(2)(1)(2)(2)(2)(5)(15)(2)(2)(2)(3)(3)Android项目开发实战-4...
老师,麻烦你讲解下面构造器的最新用法,我发现你讲的用法好像已经弃用了,最新的API说明是,在to 的后面还有一个参数:int flags请问flags如何赋值?谢谢public SimpleCursorAdapter ( context int layout
from int[] to int flags)Added in Standard constructor.ParameterscontextThe context where the ListView associated with this SimpleListItemFactory is runninglayoutresource identifier of a layout file that defines the views for this list item. The layout file should include at least those named views defined in "to"cThe database cursor. Can be null if the cursor is not available yet.fromA list of column names representing the data to bind to the UI. Can be null if the cursor is not available yet.toThe views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter. Can be null if the cursor is not available yet.flagsFlags used to determine the behavior of the adapter as per CursorAdapter(Context Cursor int).
可以提到这里。老师们看到会评估安排录制
Android项目开发实战-4...}

我要回帖

更多关于 simplecursoradapter 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信