将 _id 替换为我自己的响应密钥
Replace _id with my own key from response
我正在将我的响应传递给 SQLite 数据库并且一直在工作,直到我开始将 Cursor 适配器绑定到列表视图。现在,当我尝试保存数据并提取数据时,我得到 java.lang.IllegalArgumentException: column '_id' does not exist。我想保留我的主键作为特定响应,因为 Web 服务调用绑定到相同的号码。有没有办法保留我的 KEY_ID 作为员工编号但摆脱 _id 错误?
我希望密钥是员工编号,这样我就可以获取它并将该员工填充到附加到游标适配器的列表中。还需要它,这样我就可以从另一个列表中删除那些我想在第一个列表下填充的用户。
我的数据库处理程序
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "employeeManager";
private static final String TABLE_EMPLOYEE = "employee";
//Employee table columns names
private static final String KEY_ID = "Employee_number";
private static final String KEY_FIRST_NAME = "First_name";
private static final String KEY_LAST_NAME = "Last_name";
private static final String KEY_PHONE_NUMBER_MOBILE = "Phone_mobile";
private static final String KEY_PHONE_NUMBER_OFFICE = "Phone_office";
private static final String KEY_PAYROLL_TITLE = "Payroll_title";
private static final String KEY_HAS_DIRECT_REPORTS = "Has_direct_reports";
private static final String KEY_EMAIL = "Email";
private static final String KEY_COST_CENTER = "Cost_center_id";
private static final String KEY_THUMBNAIL_IMAGE = "ThumbnailData";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_EMPLOYEE_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_EMPLOYEE + "("
+ KEY_ID + " STRING PRIMARY KEY,"
+ KEY_FIRST_NAME + " TEXT,"
+ KEY_LAST_NAME + " TEXT,"
+ KEY_PHONE_NUMBER_MOBILE + " TEXT,"
+ KEY_PHONE_NUMBER_OFFICE + " TEXT,"
+ KEY_PAYROLL_TITLE + " TEXT,"
+ KEY_HAS_DIRECT_REPORTS + " TEXT,"
+ KEY_EMAIL + " TEXT,"
+ KEY_THUMBNAIL_IMAGE + " TEXT,"
+ KEY_COST_CENTER + " TEXT" + ")";
db.execSQL(CREATE_EMPLOYEE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//drop old table if existence
db.execSQL("DROP TABLE IF EXISTS " + TABLE_EMPLOYEE);
//Create table again
onCreate(db);
}
//Add new employee
public boolean addEmployee(Employee employee) {
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID, employee.getEmployee_number());
values.put(KEY_FIRST_NAME, employee.getFirst_name());
values.put(KEY_LAST_NAME, employee.getLast_name());
values.put(KEY_PHONE_NUMBER_MOBILE, employee.getPhone_mobile());
values.put(KEY_PHONE_NUMBER_OFFICE, employee.getPhone_office());
values.put(KEY_HAS_DIRECT_REPORTS, employee.getHas_direct_reports());
values.put(KEY_EMAIL, employee.getEmail());
values.put(KEY_COST_CENTER, employee.getCost_center_id());
values.put(KEY_PAYROLL_TITLE, employee.getPayroll_title());
values.put(KEY_THUMBNAIL_IMAGE, employee.getThumbnailData());
//Inserting Row
database.insert(TABLE_EMPLOYEE, null, values);
database.close();
return true;
}
//Get single employee
public Employee getEmployee(int employeeNumber) {
SQLiteDatabase database = this.getReadableDatabase();
Cursor cursor = database.query(TABLE_EMPLOYEE, new String[] {
KEY_ID, KEY_FIRST_NAME, KEY_LAST_NAME, KEY_PHONE_NUMBER_OFFICE, KEY_PHONE_NUMBER_MOBILE,
KEY_HAS_DIRECT_REPORTS, KEY_EMAIL, KEY_COST_CENTER, KEY_PAYROLL_TITLE, KEY_THUMBNAIL_IMAGE}, KEY_ID + "=?",
new String[]{ String.valueOf(employeeNumber)}, null, null, null, null);
if(cursor != null && cursor.moveToFirst()) {
return new Employee(cursor.getString(0),
cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4),
cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8),
cursor.getString(9), cursor.getString(10), cursor.getString(11), cursor.getString(12),
cursor.getString(12), cursor.getString(14), cursor.getString(15), cursor.getString(16),
cursor.getString(17), cursor.getString(18), cursor.getString(19), cursor.getString(20),
cursor.getString(21), cursor.getString(22), cursor.getString(23), cursor.getString(24),
cursor.getString(24), cursor.getString(25), cursor.getString(26));
}
return null;
}
//Get All Employees
public ArrayList<Employee> getAllEmployees() {
ArrayList<Employee> employeeList = new ArrayList<>();
//Select all query
String selectQuery = "SELECT * FROM " + TABLE_EMPLOYEE;
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
//looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Employee employee = new Employee();
employee.setEmployee_number(cursor.getString(cursor.getColumnIndex(KEY_ID)));
employee.setFirst_name(cursor.getString(cursor.getColumnIndex(KEY_FIRST_NAME)));
employee.setLast_name(cursor.getString(cursor.getColumnIndex(KEY_LAST_NAME)));
employee.setPhone_office(cursor.getString(cursor.getColumnIndex(KEY_PHONE_NUMBER_MOBILE)));
employee.setPhone_mobile(cursor.getString(cursor.getColumnIndex(KEY_PHONE_NUMBER_OFFICE)));
employee.setHas_direct_reports(cursor.getString(cursor.getColumnIndex(KEY_HAS_DIRECT_REPORTS)));
employee.setEmail(cursor.getString(cursor.getColumnIndex(KEY_EMAIL)));
employee.setCost_center_id(cursor.getString(cursor.getColumnIndex(KEY_COST_CENTER)));
employee.setPayroll_title(cursor.getString(cursor.getColumnIndex(KEY_PAYROLL_TITLE)));
employee.setThumbnailData(cursor.getString(cursor.getColumnIndex(KEY_THUMBNAIL_IMAGE)));
} while (cursor.moveToNext());
}
//return employees list
return employeeList;
}
//Get Employee Count
public int getEmployeeCount() {
String countQuery = "SELECT * FROM " + TABLE_EMPLOYEE;
SQLiteDatabase database = this.getReadableDatabase();
Cursor cursor = database.rawQuery(countQuery, null);
cursor.close();
return cursor.getCount();
}
//Updating single employee
public int updateEmployee(Employee employee){
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_FIRST_NAME, employee.getFirst_name());
values.put(KEY_LAST_NAME, employee.getLast_name());
values.put(KEY_PHONE_NUMBER_MOBILE, employee.getPhone_mobile());
values.put(KEY_PHONE_NUMBER_OFFICE, employee.getPhone_office());
values.put(KEY_HAS_DIRECT_REPORTS, employee.getHas_direct_reports());
values.put(KEY_EMAIL, employee.getEmail());
values.put(KEY_COST_CENTER, employee.getCost_center_id());
values.put(KEY_PAYROLL_TITLE, employee.getPayroll_title());
values.put(KEY_THUMBNAIL_IMAGE, employee.getThumbnailData());
return database.update(TABLE_EMPLOYEE, values, KEY_ID + " = ?",
new String[] {String.valueOf(employee.getEmployee_number())});
}
//Delete single employee
public void deleteEmployee(Employee employee) {
SQLiteDatabase database = this.getWritableDatabase();
database.delete(TABLE_EMPLOYEE, KEY_ID + " = ?",
new String[] {String.valueOf(employee.getEmployee_number())});
database.close();
}
}
我在哪里创建、添加和获取数据库
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTopList = (ListView) findViewById(R.id.mTopList);
TopListCursorAdapter topAdapter = new TopListCursorAdapter(this, topViewList(mTopCursor));
mTopList.setAdapter(topAdapter);
directReportListView = (ListView) findViewById(R.id.mDirectReportList);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
mProgressBar.setVisibility(View.VISIBLE);
mBottomListViewAdapter = new BottomListViewAdapter(this, mEmployees);
directReportListView.setAdapter(mBottomListViewAdapter);
getBottomViewXMLData();
//GUI for seeing android SQLite Database in Chrome Dev Tools
Stetho.InitializerBuilder inBuilder = Stetho.newInitializerBuilder(this);
inBuilder.enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this));
Stetho.Initializer in = inBuilder.build();
Stetho.initialize(in);
//Top List View work
// mTopListViewAdapter = new TopListCursorAdapter(this, mEmployees);
}
public void getBottomViewXMLData() {
OkHttpClient client = getUnsafeOkHttpClient();
Request request = new Request.Builder()
.url(getString(R.string.API_FULL_URL))
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
final String responseData = response.body().string();
final InputStream stream = new ByteArrayInputStream(responseData.getBytes());
final XMLPullParserHandler parserHandler = new XMLPullParserHandler();
DatabaseHandler databasehandler = new DatabaseHandler(getApplicationContext());
final ArrayList<Employee> employees = (ArrayList<Employee>) parserHandler.parse(stream);
for (Employee e : employees) {
databasehandler.addEmployee(e);
}
Log.i("ADAM", databasehandler.toString());
mEmployees.clear();
mEmployees.addAll(employees);
//tell adapter on the UI thread its data changed
runOnUiThread(new Runnable() {
@Override
public void run() {
mBottomListViewAdapter.notifyDataSetChanged();
directReportListView.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.GONE);
}
});
}
});
}
public Cursor topViewList(Cursor employeeCursor) {
//always starts with Mark
int startingEmployeeNumber = startingEmployeeNumber(int);
DatabaseHandler mDatabase = new DatabaseHandler(this);
SQLiteDatabase database = mDatabase.getWritableDatabase();
mDatabase.getEmployee(startingEmployeeNumber);
employeeCursor = database.rawQuery("SELECT * FROM employee", null);
return employeeCursor;
}
}
TopListCursorAdapter
public class TopListCursorAdapter extends CursorAdapter {
public TopListCursorAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.contact_cardview_layout, parent, false);
}
@Override
public void bindView(View view, final Context context, Cursor cursor) {
TextView tvFirstName = (TextView) view.findViewById(R.id.personFirstName);
TextView tvLastName = (TextView) view.findViewById(R.id.personLastName);
TextView tvTitle = (TextView) view.findViewById(R.id.personTitle);
ImageView mPeepPic = (ImageView) view.findViewById(R.id.person_photo);
ImageView mDetailsButton = (ImageButton) view.findViewById(R.id.fullDetailButton);
CardView mCardView = (CardView) view.findViewById(R.id.home_screen_cardView);
String mFirstName = cursor.getString(cursor.getColumnIndexOrThrow("First_name"));
String mLastName = cursor.getString(cursor.getColumnIndexOrThrow("Last_name"));
String mPayrollTitle = cursor.getString(cursor.getColumnIndexOrThrow("Payroll_title"));
String mPicData = cursor.getString(cursor.getColumnIndexOrThrow("ThumbnailData"));
//parse image from database
byte[] imageAsBytes = Base64.decode(mPicData.getBytes(), Base64.DEFAULT);
Bitmap parsedImage = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
mPeepPic.setImageBitmap(parsedImage);
tvFirstName.setText(mFirstName);
tvLastName.setText(mLastName);
tvTitle.setText(mPayrollTitle);
mDetailsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "This is working well here also", Toast.LENGTH_SHORT).show();
}
});
}
}
许多与 SQLite 数据库交互的 Android API 小部件假定 Cursor
包含名为 _id
的列。您至少有两种选择来解决此错误:
添加一个名为 _id
的列。您可以使其包含与 Employee_number
相同的数据。最简单的方法是 SELECT Employee_number AS _id
.
避免使用 Android API 中使用 _id
的部分,并编写您自己的这些方法版本。
我正在将我的响应传递给 SQLite 数据库并且一直在工作,直到我开始将 Cursor 适配器绑定到列表视图。现在,当我尝试保存数据并提取数据时,我得到 java.lang.IllegalArgumentException: column '_id' does not exist。我想保留我的主键作为特定响应,因为 Web 服务调用绑定到相同的号码。有没有办法保留我的 KEY_ID 作为员工编号但摆脱 _id 错误?
我希望密钥是员工编号,这样我就可以获取它并将该员工填充到附加到游标适配器的列表中。还需要它,这样我就可以从另一个列表中删除那些我想在第一个列表下填充的用户。
我的数据库处理程序
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "employeeManager";
private static final String TABLE_EMPLOYEE = "employee";
//Employee table columns names
private static final String KEY_ID = "Employee_number";
private static final String KEY_FIRST_NAME = "First_name";
private static final String KEY_LAST_NAME = "Last_name";
private static final String KEY_PHONE_NUMBER_MOBILE = "Phone_mobile";
private static final String KEY_PHONE_NUMBER_OFFICE = "Phone_office";
private static final String KEY_PAYROLL_TITLE = "Payroll_title";
private static final String KEY_HAS_DIRECT_REPORTS = "Has_direct_reports";
private static final String KEY_EMAIL = "Email";
private static final String KEY_COST_CENTER = "Cost_center_id";
private static final String KEY_THUMBNAIL_IMAGE = "ThumbnailData";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_EMPLOYEE_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_EMPLOYEE + "("
+ KEY_ID + " STRING PRIMARY KEY,"
+ KEY_FIRST_NAME + " TEXT,"
+ KEY_LAST_NAME + " TEXT,"
+ KEY_PHONE_NUMBER_MOBILE + " TEXT,"
+ KEY_PHONE_NUMBER_OFFICE + " TEXT,"
+ KEY_PAYROLL_TITLE + " TEXT,"
+ KEY_HAS_DIRECT_REPORTS + " TEXT,"
+ KEY_EMAIL + " TEXT,"
+ KEY_THUMBNAIL_IMAGE + " TEXT,"
+ KEY_COST_CENTER + " TEXT" + ")";
db.execSQL(CREATE_EMPLOYEE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//drop old table if existence
db.execSQL("DROP TABLE IF EXISTS " + TABLE_EMPLOYEE);
//Create table again
onCreate(db);
}
//Add new employee
public boolean addEmployee(Employee employee) {
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID, employee.getEmployee_number());
values.put(KEY_FIRST_NAME, employee.getFirst_name());
values.put(KEY_LAST_NAME, employee.getLast_name());
values.put(KEY_PHONE_NUMBER_MOBILE, employee.getPhone_mobile());
values.put(KEY_PHONE_NUMBER_OFFICE, employee.getPhone_office());
values.put(KEY_HAS_DIRECT_REPORTS, employee.getHas_direct_reports());
values.put(KEY_EMAIL, employee.getEmail());
values.put(KEY_COST_CENTER, employee.getCost_center_id());
values.put(KEY_PAYROLL_TITLE, employee.getPayroll_title());
values.put(KEY_THUMBNAIL_IMAGE, employee.getThumbnailData());
//Inserting Row
database.insert(TABLE_EMPLOYEE, null, values);
database.close();
return true;
}
//Get single employee
public Employee getEmployee(int employeeNumber) {
SQLiteDatabase database = this.getReadableDatabase();
Cursor cursor = database.query(TABLE_EMPLOYEE, new String[] {
KEY_ID, KEY_FIRST_NAME, KEY_LAST_NAME, KEY_PHONE_NUMBER_OFFICE, KEY_PHONE_NUMBER_MOBILE,
KEY_HAS_DIRECT_REPORTS, KEY_EMAIL, KEY_COST_CENTER, KEY_PAYROLL_TITLE, KEY_THUMBNAIL_IMAGE}, KEY_ID + "=?",
new String[]{ String.valueOf(employeeNumber)}, null, null, null, null);
if(cursor != null && cursor.moveToFirst()) {
return new Employee(cursor.getString(0),
cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4),
cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8),
cursor.getString(9), cursor.getString(10), cursor.getString(11), cursor.getString(12),
cursor.getString(12), cursor.getString(14), cursor.getString(15), cursor.getString(16),
cursor.getString(17), cursor.getString(18), cursor.getString(19), cursor.getString(20),
cursor.getString(21), cursor.getString(22), cursor.getString(23), cursor.getString(24),
cursor.getString(24), cursor.getString(25), cursor.getString(26));
}
return null;
}
//Get All Employees
public ArrayList<Employee> getAllEmployees() {
ArrayList<Employee> employeeList = new ArrayList<>();
//Select all query
String selectQuery = "SELECT * FROM " + TABLE_EMPLOYEE;
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
//looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Employee employee = new Employee();
employee.setEmployee_number(cursor.getString(cursor.getColumnIndex(KEY_ID)));
employee.setFirst_name(cursor.getString(cursor.getColumnIndex(KEY_FIRST_NAME)));
employee.setLast_name(cursor.getString(cursor.getColumnIndex(KEY_LAST_NAME)));
employee.setPhone_office(cursor.getString(cursor.getColumnIndex(KEY_PHONE_NUMBER_MOBILE)));
employee.setPhone_mobile(cursor.getString(cursor.getColumnIndex(KEY_PHONE_NUMBER_OFFICE)));
employee.setHas_direct_reports(cursor.getString(cursor.getColumnIndex(KEY_HAS_DIRECT_REPORTS)));
employee.setEmail(cursor.getString(cursor.getColumnIndex(KEY_EMAIL)));
employee.setCost_center_id(cursor.getString(cursor.getColumnIndex(KEY_COST_CENTER)));
employee.setPayroll_title(cursor.getString(cursor.getColumnIndex(KEY_PAYROLL_TITLE)));
employee.setThumbnailData(cursor.getString(cursor.getColumnIndex(KEY_THUMBNAIL_IMAGE)));
} while (cursor.moveToNext());
}
//return employees list
return employeeList;
}
//Get Employee Count
public int getEmployeeCount() {
String countQuery = "SELECT * FROM " + TABLE_EMPLOYEE;
SQLiteDatabase database = this.getReadableDatabase();
Cursor cursor = database.rawQuery(countQuery, null);
cursor.close();
return cursor.getCount();
}
//Updating single employee
public int updateEmployee(Employee employee){
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_FIRST_NAME, employee.getFirst_name());
values.put(KEY_LAST_NAME, employee.getLast_name());
values.put(KEY_PHONE_NUMBER_MOBILE, employee.getPhone_mobile());
values.put(KEY_PHONE_NUMBER_OFFICE, employee.getPhone_office());
values.put(KEY_HAS_DIRECT_REPORTS, employee.getHas_direct_reports());
values.put(KEY_EMAIL, employee.getEmail());
values.put(KEY_COST_CENTER, employee.getCost_center_id());
values.put(KEY_PAYROLL_TITLE, employee.getPayroll_title());
values.put(KEY_THUMBNAIL_IMAGE, employee.getThumbnailData());
return database.update(TABLE_EMPLOYEE, values, KEY_ID + " = ?",
new String[] {String.valueOf(employee.getEmployee_number())});
}
//Delete single employee
public void deleteEmployee(Employee employee) {
SQLiteDatabase database = this.getWritableDatabase();
database.delete(TABLE_EMPLOYEE, KEY_ID + " = ?",
new String[] {String.valueOf(employee.getEmployee_number())});
database.close();
}
}
我在哪里创建、添加和获取数据库
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTopList = (ListView) findViewById(R.id.mTopList);
TopListCursorAdapter topAdapter = new TopListCursorAdapter(this, topViewList(mTopCursor));
mTopList.setAdapter(topAdapter);
directReportListView = (ListView) findViewById(R.id.mDirectReportList);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
mProgressBar.setVisibility(View.VISIBLE);
mBottomListViewAdapter = new BottomListViewAdapter(this, mEmployees);
directReportListView.setAdapter(mBottomListViewAdapter);
getBottomViewXMLData();
//GUI for seeing android SQLite Database in Chrome Dev Tools
Stetho.InitializerBuilder inBuilder = Stetho.newInitializerBuilder(this);
inBuilder.enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this));
Stetho.Initializer in = inBuilder.build();
Stetho.initialize(in);
//Top List View work
// mTopListViewAdapter = new TopListCursorAdapter(this, mEmployees);
}
public void getBottomViewXMLData() {
OkHttpClient client = getUnsafeOkHttpClient();
Request request = new Request.Builder()
.url(getString(R.string.API_FULL_URL))
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
final String responseData = response.body().string();
final InputStream stream = new ByteArrayInputStream(responseData.getBytes());
final XMLPullParserHandler parserHandler = new XMLPullParserHandler();
DatabaseHandler databasehandler = new DatabaseHandler(getApplicationContext());
final ArrayList<Employee> employees = (ArrayList<Employee>) parserHandler.parse(stream);
for (Employee e : employees) {
databasehandler.addEmployee(e);
}
Log.i("ADAM", databasehandler.toString());
mEmployees.clear();
mEmployees.addAll(employees);
//tell adapter on the UI thread its data changed
runOnUiThread(new Runnable() {
@Override
public void run() {
mBottomListViewAdapter.notifyDataSetChanged();
directReportListView.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.GONE);
}
});
}
});
}
public Cursor topViewList(Cursor employeeCursor) {
//always starts with Mark
int startingEmployeeNumber = startingEmployeeNumber(int);
DatabaseHandler mDatabase = new DatabaseHandler(this);
SQLiteDatabase database = mDatabase.getWritableDatabase();
mDatabase.getEmployee(startingEmployeeNumber);
employeeCursor = database.rawQuery("SELECT * FROM employee", null);
return employeeCursor;
}
}
TopListCursorAdapter
public class TopListCursorAdapter extends CursorAdapter {
public TopListCursorAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.contact_cardview_layout, parent, false);
}
@Override
public void bindView(View view, final Context context, Cursor cursor) {
TextView tvFirstName = (TextView) view.findViewById(R.id.personFirstName);
TextView tvLastName = (TextView) view.findViewById(R.id.personLastName);
TextView tvTitle = (TextView) view.findViewById(R.id.personTitle);
ImageView mPeepPic = (ImageView) view.findViewById(R.id.person_photo);
ImageView mDetailsButton = (ImageButton) view.findViewById(R.id.fullDetailButton);
CardView mCardView = (CardView) view.findViewById(R.id.home_screen_cardView);
String mFirstName = cursor.getString(cursor.getColumnIndexOrThrow("First_name"));
String mLastName = cursor.getString(cursor.getColumnIndexOrThrow("Last_name"));
String mPayrollTitle = cursor.getString(cursor.getColumnIndexOrThrow("Payroll_title"));
String mPicData = cursor.getString(cursor.getColumnIndexOrThrow("ThumbnailData"));
//parse image from database
byte[] imageAsBytes = Base64.decode(mPicData.getBytes(), Base64.DEFAULT);
Bitmap parsedImage = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
mPeepPic.setImageBitmap(parsedImage);
tvFirstName.setText(mFirstName);
tvLastName.setText(mLastName);
tvTitle.setText(mPayrollTitle);
mDetailsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "This is working well here also", Toast.LENGTH_SHORT).show();
}
});
}
}
许多与 SQLite 数据库交互的 Android API 小部件假定 Cursor
包含名为 _id
的列。您至少有两种选择来解决此错误:
添加一个名为
_id
的列。您可以使其包含与Employee_number
相同的数据。最简单的方法是SELECT Employee_number AS _id
.避免使用 Android API 中使用
_id
的部分,并编写您自己的这些方法版本。