我正在尝试将编辑功能添加到数据库中,读写功能运行良好。我想要的第一步是在另一个布局中显示从我的列表视图中单击的记录(与我用于添加新记录的布局相同)
在show_活动中我有这个代码

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());

    .....

从我的数据库适配器
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;
    }

但当我单击record debug时,在这一行显示nullpointexception
游标游标=(游标)适配器.getitem(位置)

最佳答案

将代码更改为:

 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);
}

关于android - 从sqlite onListItemClick NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14023979/

10-09 01:39