2014-09-27 54 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();

  • 相关问题