如何将数据从SQlite显示到许多textview

时间:2013-02-20 03:29:56

标签: textview android-sqlite android

我有问题从sqlite到很多textview显示数据。例如,对于名称,我想放在textviewName。下面是我的代码。

protected void onCreate(Bundle savedInstanceState) 
{   
    super.onCreate(savedInstanceState);
    setContentView(R.layout.profile);

    TextView TV_Name = (TextView)findViewById(R.id.TVName);
    TextView TV_DOB = (TextView)findViewById(R.id.TVDOB);
    TextView TV_Address = (TextView)findViewById(R.id.TVAddress);
    TextView TV_BloodType = (TextView)findViewById(R.id.TVBloodType);
    TextView TV_Height =    (TextView)findViewById(R.id.TVHeight);
    TextView TV_DrName =    (TextView)findViewById(R.id.TVDrName);
    TextView TV_DrContact = (TextView)findViewById(R.id.TVDrContact);
    TextView TV_CaretakerName = (TextView)findViewById(R.id.TVCaretakerName);
    TextView TV_CaretakerContact = (TextView)findViewById(R.id.TVCaretakerContact);
    TextView TV_HospitalName = (TextView)findViewById(R.id.TVHospitalName);
    TextView TV_HospitalContact = (TextView)findViewById(R.id.TVHospitalContact);

    DBHelper helper = new DBHelper(this);
    database = helper.getWritableDatabase();

    String sql = "select * from " + DBHelper.TABLE_PATIENT;
    Cursor cursor = database.rawQuery(sql, null);
    if (cursor.moveToFirst()) {
       do {
           idList.add(cursor.getLong(0));
           list.add(cursor.getString(1));
           list.add(cursor.getString(2));
           list.add(cursor.getString(3));
           list.add(cursor.getString(4));
           list.add(cursor.getString(5));
           list.add(cursor.getString(6));
           list.add(cursor.getString(7));
           list.add(cursor.getString(8));
           list.add(cursor.getString(9));
           list.add(cursor.getString(10));
           list.add(cursor.getString(11));
           list.add(cursor.getString(12));
           list.add(cursor.getString(13));

       } while (cursor.moveToNext());
    }

    TV_Name.setText(cursor.getString(1)); 
        TV_DOB.setText(cursor.getString(2));
    cursor.close(); 

}

但是我收到错误java.lang.NullPointerException

更新: 这是我的数据库

public class DBHelper extends SQLiteOpenHelper{

public static final String DB_NAME = "MyDocDB";
public static final int DB_VERSION = 1; 

//Table Patient Details//
    public static final String TABLE_PATIENT = "patient";
    public static final String PATIENT_ID = "id";
    public static final String NAME = "name";
    public static final String DOB = "dob";
    public static final String ADDRESS = "address";
    public static final String BLOODTYPE = "bloodtype";
    public static final String HEIGHT = "height";
    public static final String DRNAME = "drname";
    public static final String DRCONTACT = "drcontact";
    public static final String GUARDIANNAME1 = "guardianname1";
    public static final String GUARDIANCONTACT1 = "guardiancontact1";
    public static final String GUARDIANNAME2 = "guardianname2";
    public static final String GUARDIANCONTACT2 = "guardiancontact2";
    public static final String HOSPITALNAME = "hospitalname";
    public static final String HOSPITALCONTACT = "hospitalcontact";

public DBHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    createTable(db);    
            testInsert(db);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}   

//create table
public void createTable(SQLiteDatabase db)
{
    String query = "create table "+ TABLE_PATIENT +" (" +
            PATIENT_ID + " integer primary key autoincrement," +
            NAME + " text," +
            DOB + " text," +
            ADDRESS + " text," +
            BLOODTYPE + " text," +
            HEIGHT + " text," +
            DRNAME + " text," +
            DRCONTACT + " text," +
            GUARDIANNAME1 + " text," +
            GUARDIANCONTACT1 + " text," +
            GUARDIANNAME2 + " text," +
            GUARDIANCONTACT2 + " text," +
            HOSPITALNAME + " text," +
            HOSPITALCONTACT + " text)";
    db.execSQL(query);  
}


//insert
public void insertDataProfile(SQLiteDatabase db, String name, String dob,
        String address, String bloodtype, String height,
        String drname, String drcontact, String guardianname1, 
        String guardiancontact1, String guardianname2, 
        String guardiancontact2, String hospitalname, String     hospitalcontact)
{
    ContentValues cvPatient = new ContentValues();
    cvPatient.put(NAME, name);
    cvPatient.put(DOB, dob);
    cvPatient.put(ADDRESS, address);
    cvPatient.put(BLOODTYPE, bloodtype);
    cvPatient.put(HEIGHT, height);
    cvPatient.put(DRNAME, drname);
    cvPatient.put(DRCONTACT, drcontact);
    cvPatient.put(GUARDIANNAME1, guardianname1);
    cvPatient.put(GUARDIANCONTACT1, guardiancontact1);
    cvPatient.put(GUARDIANNAME2, guardianname2);
    cvPatient.put(GUARDIANCONTACT2, guardiancontact2);
    cvPatient.put(HOSPITALNAME, hospitalname);
    cvPatient.put(HOSPITALCONTACT, hospitalcontact);
    db.insert(TABLE_PATIENT, null, cvPatient);
}

public void testInsert(SQLiteDatabase db)
{
    db.execSQL("INSERT INTO "
    + TABLE_PATIENT
    + "name, dob, address, bloodtype, height, drname, "
    + "drcontact, guardianname1, guardiancontact1, " 
    + "guardianname2, guardiancontact2,hospitalname, "
    + "hospitalcontact"
    + " VALUES ('muna', '11/02/90', 'No 34, Taman Seri Mengkuang 8', " +
    "'0', '158', 'dr nurul', '12344', 'dada', '141'," +
    "'fafa', '34141', 'faga', '14141');");

public Cursor getAllData(SQLiteDatabase db)
{

    return db.query(TABLE_PATIENT, new String[]{PATIENT_ID, NAME, ADDRESS,
            BLOODTYPE, HEIGHT, DRNAME, DRCONTACT, GUARDIANNAME1, GUARDIANCONTACT1,
            GUARDIANNAME2, GUARDIANCONTACT2, HOSPITALNAME, HOSPITALCONTACT},
            null, null, null, null, null);  
}

}

2 个答案:

答案 0 :(得分:1)

{

 protected void onCreate(Bundle savedInstanceState) 
 {   
  super.onCreate(savedInstanceState);
  setContentView(R.layout.profile);

  TextView TV_Name = (TextView)findViewById(R.id.TVName);
  TextView TV_DOB = (TextView)findViewById(R.id.TVDOB);
  TextView TV_Address = (TextView)findViewById(R.id.TVAddress);
  TextView TV_BloodType = (TextView)findViewById(R.id.TVBloodType);
  TextView TV_Height =    (TextView)findViewById(R.id.TVHeight);
  TextView TV_DrName =    (TextView)findViewById(R.id.TVDrName);
  TextView TV_DrContact = (TextView)findViewById(R.id.TVDrContact);
  TextView TV_CaretakerName = (TextView)findViewById(R.id.TVCaretakerName);
  TextView TV_CaretakerContact = (TextView)findViewById(R.id.TVCaretakerContact);
  TextView TV_HospitalName = (TextView)findViewById(R.id.TVHospitalName);
  TextView TV_HospitalContact = (TextView)findViewById(R.id.TVHospitalContact);

DBHelper helper = new DBHelper(this);
database = helper.getWritableDatabase();

String sql = "select * from " + DBHelper.TABLE_PATIENT;
Cursor cursor = database.rawQuery(sql, null);
if (cursor.moveToFirst()) {
   do {
       idList.add(cursor.getLong(0));
       list.add(cursor.getString(1));
       list.add(cursor.getString(2));
       list.add(cursor.getString(3));
       list.add(cursor.getString(4));
       list.add(cursor.getString(5));
       list.add(cursor.getString(6));
       list.add(cursor.getString(7));
       list.add(cursor.getString(8));
       list.add(cursor.getString(9));
       list.add(cursor.getString(10));
       list.add(cursor.getString(11));
       list.add(cursor.getString(12));
       list.add(cursor.getString(13));

   } while (cursor.moveToNext());
}

cursor.moveToFirst();
TV_Name.setText(cursor.getString(1)); 
TV_DOB.setText(cursor.getString(2));
cursor.close(); 

}

你试过这个吗?

答案 1 :(得分:0)

是不是因为你已经使用moveToNext将光标移动到了最后?在我再次访问数据之前调用MoveToFirst。