我正在尝试将编辑功能添加到我的数据库中,读写功能完美运行。我想要的第一步是显示从我的列表视图中点击另一个布局的记录(我用于添加新记录的相同布局) 在show_activity我有这个代码
show_activity
///ON CREATE
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
datasource = new DBAdapter(this);
datasource.open();
Cursor cursor = datasource.Query(filter);
String[] columns = new String[] { "swimm_pos", "swimm_date","swimm_lap", "swimm_stroke", "swimm_time", "swimm_media", "swimm_efficiency", "swimm_note" };
int[] to = new int[] { R.id.swimm_pos, R.id.swimm_date, R.id.swimm_lap, R.id.swimm_stroke, R.id.swimm_time, R.id.swimm_medialap, R.id.swimm_efficiency, R.id.swimm_note};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
this,
R.layout.list_layout,
cursor,
columns,
to);
this.setListAdapter(adapter);
datasource.close();
'' '''
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent intent = new Intent("com.poolper.Edit_Activity");
Cursor cursor = (Cursor) adapter.getItem(position);
intent.putExtra("idContacto",cursor.getInt(cursor.getColumnIndex("_id")));
startActivity(intent);
}
这是我的编辑活动
的摘录 public class Edit_Activity extends Activity {
int idContacto;
DBAdapter datasource;
GetSet details;
EditText edtSwimm_pos;
.....
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_layout);
edtSwimm_pos = (EditText) findViewById(R.modificacontatto.edswimm_date);
......
LoadDetails();
...
private void LoadDetails(){
idContacto = getIntent().getIntExtra("idContacto", 0);
datasource = new DBAdapter(this);
datasource.open();
details = datasource.getContacto(idContacto);
datasource.close();
edtSwimm_pos.setText(contacto.getswimm_pos());
.....
和我的DBAdapter
public class DBAdapter {
private SQLiteDatabase database;
private DBHelper DBHelper;
private String[] allColumns = {
DBHelper.ID,
DBHelper.SWIMM_POS,
DBHelper.SWIMM_DATE,
DBHelper.SWIMM_LAP,
DBHelper.SWIMM_STROKE,
DBHelper.SWIMM_TIME,
DBHelper.SWIMM_MEDIA,
DBHelper.SWIMM_EFFICIENCY,
DBHelper.SWIMM_NOTE
};
....
private GetSet cursorToContacto(Cursor cursor) {
GetSet contacto = new GetSet(cursor.getLong(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));
return contacto;
}
public GetSet getContacto (int idContacto){
Cursor cursor = database.query(DBHelper.TABLE_NAME, allColumns, DBHelper.ID + " = " + idContacto, null,null, null, null);
cursor.moveToFirst();
return cursorToContacto(cursor);
}
和这个GetSet类
public class GetSet {
private long _id;
private String swimm_pos;
private String swimm_media;
private String swimm_note;
private String swimm_date;
private String swimm_lap;
private String swimm_stroke;
private String swimm_time;
private String swimm_efficiency;
public GetSet(long id, String swimm_pos, String swimm_date, String swimm_lap,String swimm_stroke, String swimm_time, String swimm_media, String swimm_efficiency, String swimm_note){
this._id=id;
this.swimm_pos=swimm_pos;
this.swimm_date=swimm_date;
this.swimm_lap=swimm_lap;
this.swimm_stroke=swimm_stroke;
this.swimm_media=swimm_media;
this.swimm_time=swimm_time;
this.swimm_efficiency=swimm_efficiency;
this.swimm_note=swimm_note;
}
public long getId() {
return _id;
}
public void setId(long id) {
this._id = id;
}
public String getswimm_pos() {
return swimm_pos;
}
public void setswimm_pos(String swimm_pos) {
this.swimm_pos = swimm_pos;
}
但是当我点击记录调试时,在这一行显示NullPointException 光标游标=(光标)adapter.getItem(位置)
答案 0 :(得分:3)
将您的代码更改为:
protected void onListItemClick(ListView l, View v, int position, long id) {
Cursor cursor = ((SimpleCursorAdapter)l.getAdapter()).getCursor();
cursor.moveToPosition(position);
Intent intent = new Intent(Current_Activity.this,Edit_Activity.class);
intent.putExtra("idContacto",cursor.getInt(cursor.getColumnIndex("_id")));
startActivity(intent);
}