2014-09-27 31 views
0

MainActivity類。Sqlite數據庫日期顯示在ListView中

公共類MainActivity擴展活動{

EditText etName, etEmail, etPhone, etDesignation; 
DatabaseHelper dbHelper; 

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

// datasource 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    etName = (EditText) findViewById(R.id.etName); 
    lvEmployees = (ListView) findViewById(R.id.lvEmployees); 
    dbHelper = new DatabaseHelper(this); 
    Employee employee; 
} 

public void save(View v) { 
    String name = etName.getText().toString(); 
    SimpleDateFormat sm = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a"); 
    Date today = Calendar.getInstance().getTime(); 
    String reportDate = sm.format(today); 

    Employee employee = new Employee(name, reportDate); 
    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(); 
    } 

} 

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

} 

}

這是我DataBasehelper

公共類DatabaseHelper擴展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 TIME_DATE = "time_date"; 

public static final String EMPLOYEE_TABLE_SQL = "CREATE TABLE " 
     + EMPLOYEE_TABLE + " (" + ID_FIELD + " INTEGER PRIMARY KEY, " 
     + NAME_FIELD + " TEXT, " + TIME_DATE + " 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()); 
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a"); 
    String date = sdf.format(emp.getDate()); 
    values.put(TIME_DATE, date); 
    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 datetime = cursor.getString(cursor 
        .getColumnIndex(TIME_DATE)); 
      Employee e = new Employee(name, datetime); 
      allEmployees.add(e); 
      cursor.moveToNext(); 
     } 
    } 
    cursor.close(); 
    db.close(); 

    return allEmployees; 
} 

}

自定義適配器..

公共類CustomizedAdapter擴展ArrayAdapter {

Activity con; 
ArrayList<Employee> employeeList; 
Employee employee; 

public CustomizedAdapter(Context context, ArrayList<Employee> employees) { 
    super(context, R.layout.list_item, employees); 
    this.con = (Activity) context; 
    this.employeeList = employees; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View v = null; 
    if (convertView == null) { 
     // generate a view and return 
     LayoutInflater inflater = con.getLayoutInflater(); 
     v = inflater.inflate(R.layout.list_item, null); 

     TextView txtName = (TextView) v.findViewById(R.id.txtName); 
     TextView datee = (TextView) v.findViewById(R.id.txtEmail); 
     Employee e = employeeList.get(position); 
     Date date = e.getDate(); 
     // Date date = e.get(position).getWishDate(); 
     datee.setText(DateFormat.format("dd/MM/yyyy hh:mm:ss a", date)); 
     txtName.setText(e.getName()); 
     datee.setText(e.getDatetime()); 

    } else { 
     v = convertView; 
    } 
    return v; 
} 

}

這裏是logcat的:

09-27 16:11:35.374: E/AndroidRuntime(32300): FATAL EXCEPTION: main 

09-27 16:11:35.374: E/AndroidRuntime(32300): java.lang.IllegalStateException: Could not execute method of the activity 

09-27 16:11:35.374: E/AndroidRuntime(32300): at android.view.View$1.onClick(View.java:3609) 

09-27 16:11:35.374: E/AndroidRuntime(32300): at android.view.View.performClick(View.java:4102) 

09-27 16:11:35.374: E/AndroidRuntime(32300): at android.view.View$PerformClick.run(View.java:17085) 

09-27 16:11:35.374: E/AndroidRuntime(32300): at android.os.Handler.handleCallback(Handler.java:615) 

09-27 16:11:35.374: E/AndroidRuntime(32300): at android.os.Handler.dispatchMessage(Handler.java:92) 

09-27 16:11:35.374: E/AndroidRuntime(32300): at android.os.Looper.loop(Looper.java:155) 
09-27 16:11:35.374: E/AndroidRuntime(32300): at 
android.app.ActivityThread.main(ActivityThread.java:5511) 

09-27 16:11:35.374: E/AndroidRuntime(32300): at java.lang.reflect.Method.invokeNative(Native Method) 

09-27 16:11:35.374: E/AndroidRuntime(32300): at java.lang.reflect.Method.i 
nvoke(Method.java:511) 

09-27 16:11:35.374: E/AndroidRuntime(32300): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029) 

09-27 16:11:35.374: E/AndroidRuntime(32300): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796) 

09-27 16:11:35.374: E/AndroidRuntime(32300): at dalvik.system.NativeStart.main(Native Method) 

09-27 16:11:35.374: E/AndroidRuntime(32300): Caused by: java.lang.reflect.InvocationTargetException 

09-27 16:11:35.374: E/AndroidRuntime(32300): at java.lang.reflect.Method.invokeNative(Native Method) 
09-27 16:11:35.374: E/AndroidRuntime(32300): at java.lang.reflect.Method.invoke(Method.java:511) 

09-27 16:11:35.374: E/AndroidRuntime(32300): at android.view.View$1.onClick(View.java:3604) 
09-27 16:11:35.374: E/AndroidRuntime(32300): ... 11 more 

09-27 16:11:35.374: E/AndroidRuntime(32300): Caused by: java.lang.NullPointerException 

09-27 16:11:35.374: E/AndroidRuntime(32300): at java.util.Calendar.setTime(Calendar.java:1324) 

09-27 16:11:35.374: E/AndroidRuntime(32300): at 
java.text.SimpleDateFormat.formatImpl(SimpleDateFormat.java:536) 

09-27 16:11:35.374: E/AndroidRuntime(32300): at java.text.SimpleDateFormat.format(SimpleDateFormat.java:821) 

09-27 16:11:35.374: E/AndroidRuntime(32300): at java.text.DateFormat.format(DateFormat.java:376) 
09-27 16:11:35.374: E/AndroidRuntime(32300): at 

com.shikkhok.taskmanagement.DatabaseHelper.insertEmployee(DatabaseHelper.java:51) 
09-27 16:11:35.374: E/AndroidRuntime(32300): at 

com.shikkhok.taskmanagement.MainActivity.save(MainActivity.java:48) 

09-27 16:11:35.374: E/AndroidRuntime(32300): ... 14 more 

09-27 16:11:35.394: E/EmbeddedLogger(426): App crashed! Process: com.shikkhok.taskmanagement 

09-27 16:11:35.394: E/EmbeddedLogger(426): App crashed! Package: com.shikkhok.taskmanagement v1 (1.0) 

09-27 16:11:35.394: E/EmbeddedLogger(426): Application Label: My Task Management 

09-27 16:11:36.124: E/Trace(32345): error opening trace file: No such file or directory (2) 

我想告訴我的當前時間和日期到我ListView並想將其保存到數據庫中。 我想選擇日期和時間,並希望將它保存爲我的Sqlite數據庫中的字符串。 當我想保存我的日期時,應用程序停止。 那麼如何解決呢?

回答

0

方法format() in SimpleDateFromat類獲取Date值並返回一個字符串。正如你在這裏做的:

Date today = Calendar.getInstance().getTime(); 
String reportDate = sm.format(today); 
Employee employee = new Employee(name, reportDate); 

所以emp.getDate()返回一個字符串和你重新格式化它,當你說:

String date = sdf.format(emp.getDate()); 

其實你要轉換的字符串值不是一個Date值。

你有兩種方法。如果你想真正把Date價值,你的表(我不知道Sqlite支持這一點),你應該使用這樣的:

values.put(NAME_FIELD, emp.getName()); 
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a"); 
Date date = sdf.parse(emp.getDate()); 
values.put(TIME_DATE, date); 
long inserted = db.insert(EMPLOYEE_TABLE, null, values); 

另一種方法是,你從DATETIMETEXT更改列類型:

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

這種變化之後,你可以很容易地使用此:

values.put(NAME_FIELD, emp.getName()); 
values.put(TIME_DATE, emp.getDate()); 
long inserted = db.insert(EMPLOYEE_TABLE, null, values); 

我希望這可以幫助你。

+0

我會事先知情同意的日期和時間,然後希望將其保存在數據庫中的字符串,@ Misagh Emamverdi – 2014-09-27 09:40:29

+0

@RasedurjamanSojib發表您的logcat – 2014-09-27 09:47:23

+0

1.java.lang.IllegalStateException:無法執行活動 2.FATAL EXCEPTION的方法:主 3.at android.view.View $ 1.onClick(View.java:3609) @Misagh Emamverdi – 2014-09-27 10:05:59

相關問題