2014-09-27 70 views
0

這是我的MainActivity。關閉應用程序後不顯示Sqlite數據

公共類MainActivity擴展活動{

EditText etName, etEmail; 
DatabaseHelper dbHelper; 
Button save; 

// declare view 
ListView lvEmployees; 
// declare adapter 
CustomizedAdapter adapter; 

// datasource 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    etName = (EditText) findViewById(R.id.etName); 
    etEmail = (EditText) findViewById(R.id.etEmail); 
    save = (Button) findViewById(R.id.btnSave); 
    lvEmployees = (ListView) findViewById(R.id.lvEmployees); 
    dbHelper = new DatabaseHelper(this); 
    save.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      save(v); 

     } 
    }); 

} 

public void save(View v) { 
    String name = etName.getText().toString(); 
    String email = etEmail.getText().toString(); 

    Employee employee = new Employee(name, email); 
    Toast.makeText(getApplicationContext(), employee.toString(), 
      Toast.LENGTH_LONG).show(); 

    long inserted = dbHelper.insertEmployee(employee); 
    if (inserted >= 0) { 
     Toast.makeText(getApplicationContext(), "Data inserted", 
       Toast.LENGTH_LONG).show(); 
    } else { 
     Toast.makeText(getApplicationContext(), "Data insertion failed...", 
       Toast.LENGTH_LONG).show(); 
    } 

    ArrayList<Employee> employees = dbHelper.getAllEmployees(); 
    if (employees != null && employees.size() > 0) { 
     adapter = new CustomizedAdapter(this, employees); 
     lvEmployees.setAdapter(adapter); 

    } 
} 

}

這是我的DataBaseHelper。

公共類DatabaseHelper當我把數據和按下保存按鈕,然後我的數據被保存並顯示在我的ListView擴展SQLiteOpenHelper {

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

public static final String EMPLOYEE_TABLE = "employee"; 
public static final String ID_FIELD = "_id"; 
public static final String NAME_FIELD = "name"; 
public static final String EMAIL_FIELD = "email"; 


public static final String EMPLOYEE_TABLE_SQL = "CREATE TABLE " 
     + EMPLOYEE_TABLE + " (" + ID_FIELD + " INTEGER PRIMARY KEY, " 
     + NAME_FIELD + " TEXT, " + EMAIL_FIELD + " DATETIME);"; 


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

} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    // create tables 
    db.execSQL(EMPLOYEE_TABLE_SQL); 
    Log.e("TABLE CREATE", EMPLOYEE_TABLE_SQL); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    // upgrade logic 

} 

// insert 
public long insertEmployee(Employee emp) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues values = new ContentValues(); 
    values.put(NAME_FIELD, emp.getName()); 
    values.put(EMAIL_FIELD, emp.getEmail()); 
    long inserted = db.insert(EMPLOYEE_TABLE, null, values); 

    db.close(); 
    return inserted; 
} 

// query 
public ArrayList<Employee> getAllEmployees() { 
    ArrayList<Employee> allEmployees = new ArrayList<Employee>(); 
    SQLiteDatabase db = this.getReadableDatabase(); 

    // String[] columns={NAME_FIELD, EMAIL_FIELD, PHONE_FIELD}; 
    // SELECT * FROM EMPLOYEE; 
    Cursor cursor = db.query(EMPLOYEE_TABLE, null, null, null, null, null, 
      null); 

    // Cursor cursor = db.rawQuery("SELECT * FROM EMPLOYEE", null); 
    if (cursor != null && cursor.getCount() > 0) { 
     cursor.moveToFirst(); 
     for (int i = 0; i < cursor.getCount(); i++) { 
      // 
      int id = cursor.getInt(cursor.getColumnIndex(ID_FIELD)); 
      String name = cursor.getString(cursor 
        .getColumnIndex(NAME_FIELD)); 
      String email = cursor.getString(cursor 
        .getColumnIndex(EMAIL_FIELD)); 
      Employee e = new Employee(id, name, email); 
      allEmployees.add(e); 
      cursor.moveToNext(); 
     } 
    } 
    cursor.close(); 
    db.close(); 

    return allEmployees; 
} 

}

。 但是,當我關閉應用程序並打開它,然後我沒有看到我的ListView中的任何數據。 投入數據並按下保存按鈕後,我的新數據和現有數據顯示在我的ListView中。 那麼如何在打開我的應用程序並且不按保存按鈕後在ListView中顯示我現有的數據。

回答

0

如果你想顯示每次應用程序啓動的數據,你需要在onCreate

移動這個代碼移到列表填充代碼onCreate,而不是保存按鈕的onClick

ArrayList<Employee> employees = dbHelper.getAllEmployees(); 
if (employees != null && employees.size() > 0) { 
    adapter = new CustomizedAdapter(this, employees); 
    lvEmployees.setAdapter(adapter); 
} 

希望它可以幫助。

P.S:如果需要在保存按鈕單擊後重新填充列表,請創建一個包含此代碼的單獨函數。並調用THA功能onCreate以及在保存按鈕的onClick

+0

如果我將此代碼移到oncreate,那麼如果我保存我的數據它不顯示。 當我重新打開應用程序,然後顯示。@ MysticMagic – 2014-09-27 17:54:53

+0

@RasedurjamanSojib你讀了P.S?我告訴你,你需要調用相同的代碼,這兩個地方..在onCreate和Save方法中,也是:) – 2014-09-27 17:55:51

0

您正在爲列表視圖中的適配器在您的保存方法只有當你真正按保存按鈕時調用。這是你做這件事的部分。

ArrayList<Employee> employees = dbHelper.getAllEmployees(); 
    if (employees != null && employees.size() > 0) { 
     adapter = new CustomizedAdapter(this, employees); 
     lvEmployees.setAdapter(adapter); 

    } 

這就是爲什麼當您打開應用程序時沒有數據在列表視圖中。

你應該在你的onCreate方法中做到這一點,在你的保存你應該做這樣的事情: 1.聲明僱員arraylist連同listview;

ArrayList<Employee> employees; 
  • 在OnCreate中調用此代碼,您應該保存方法去除

    employees = dbHelper.getAllEmployees(); 
        if (employees != null && employees.size() > 0) { 
         adapter = new CustomizedAdapter(this, employees); 
         lvEmployees.setAdapter(adapter); 
    
        } 
    
  • 在保存方法

    只是多了一個項目添加到列表,並通知適配器它正在顯示的數據發生了變化。

    employees.add(employee); adapter.notifyDataSetChanged();

  • 相關問題