我正在尝试从联系人表中读取联系人并将它们全部存储到列表变量中..但无法弄清楚为什么它似乎不起作用..在执行时强制关闭。需要发送联系人通过 json 对象发送到 Web 服务器。
private static final String TABLE_CONTACTS = "contacts";
// contacts JSONArray
JSONArray contacts = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
List<contact> l=new ArrayList<contact>();
String query="SELECT * FROM"+ TABLE_CONTACTS;
DBHelper help= new DBHelper(getBaseContext());
SQLiteDatabase db=help.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
if(cursor.moveToFirst())
{
do{
contact contact = new contact();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setPhoneNumber(cursor.getString(2));
Toast.makeText(this,contact.name,Toast.LENGTH_LONG);
l.add(contact);
}
while(cursor.moveToNext());
}
清单文件有2个权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
和 DBHelper 类,用于将 sqlitopenhelper 扩展为 getWritableDatabase..
class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "contactsManager";
// Contacts table name
private static final String TABLE_CONTACTS = "contacts";
DBHelper(Context context) {
super(context,DATABASE_NAME, null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}