Android中绑定SQLite到ListActivity

2011-07-27 16:03 5人阅读 评论(0) 收藏 举报 
今天练习Android开发,打算把数据库查询的结果显示在ListActivity上。折腾了好久,总是出错。

后来终于发现,要绑定一个Cursor到ListActivity上,查询结果中必须有一个"_id"

import java.io.File;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;

public class JobList extends ListActivity {
 
 private File f = new File("/sdcard/List.db");
 private SQLiteDatabase mysql;
  
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        

  mysql = SQLiteDatabase.openOrCreateDatabase(f, null);
  Cursor cur = mysql.rawQuery("SELECT No_ as _id, Description FROM JobList", null);
  if (cur != null) {
   ListAdapter adapter = new SimpleCursorAdapter(
     this, android.R.layout.two_line_list_item,
     cur, new String[] {"_id", "Description"},
     new int[] {android.R.id.text1, android.R.id.text2});
   setListAdapter(adapter);   
  }
  mysql.close();
    }
}



 

你可能感兴趣的:(android,mysql,sqlite,String,File,null)