我已经把机器的时间分成了时间,然后我在sqlite表中的登录类中插入了我的登录用户名和密码,并且我必须插入从其他类中获取的值为&#39 ;广告在登录时创建了一个变量(timedate),在调用方法insertEntry(userName,timedate)时从时间类获取时间,但是如果有人对此有任何想法,我就无法发送它,请帮助我们。
这是我的代码
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class HomeActivity extends Activity implements OnClickListener
{
public static String userName;
EditText editTextUserName,editTextpassword;
Button btnSignIn;
SimpleCursorAdapter adapter;
LoginDataBaseAdapter loginDataBaseAdapter;
String timedate = time.formattedDate;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.loginpage);
// create a instance of SQLite Database
loginDataBaseAdapter=new LoginDataBaseAdapter(this);
loginDataBaseAdapter=loginDataBaseAdapter.open();
editTextUserName=(EditText)findViewById(R.id.editTextUserNameToLogin);
editTextpassword=(EditText)findViewById(R.id.editTextUserPasswordToLogin);
btnSignIn=(Button)findViewById(R.id.buttonSignIn);
btnSignIn.setOnClickListener((OnClickListener) this);
}
public void onClick(View v) {
String userName=editTextUserName.getText().toString();
@SuppressWarnings("unused")
String password=editTextpassword.getText().toString();
if(userName.equals(""))
{
Toast.makeText(getApplicationContext(), "userName does not match", Toast.LENGTH_LONG).show();
return;
}
{
loginDataBaseAdapter.insertEntry(userName,timedate);
Toast.makeText(getApplicationContext(), "Congrats: Login Successfull", Toast.LENGTH_LONG).show();
Toast.makeText(this, timedate, Toast.LENGTH_SHORT).show();
Intent name=new Intent(HomeActivity.this,enteredlogin.class);
startActivity(name);
}}
DBHelper类,
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class LoginDataBaseAdapter
{
static final String DATABASE_NAME = "login.db";
static final int DATABASE_VERSION = 1;
public static final int NAME_COLUMN = 1;
static final String DATABASE_CREATE = "create table "+"LOGIN"+
"( " +"ID"+" integer primary key autoincrement,"+ "USERNAME text,FORMATTEDDATE text); ";
public SQLiteDatabase db;
// Context of the application using the database.
private final Context context;
private DataBaseHelper dbHelper;
public LoginDataBaseAdapter(Context _context)
{
context = _context;
dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public LoginDataBaseAdapter open() throws SQLException
{
db = dbHelper.getWritableDatabase();
return this;
}
public void close()
{
db.close();
}
public SQLiteDatabase getDatabaseInstance()
{
return db;
}
public void insertEntry(String userName,String timedate)
{
ContentValues newValues = new ContentValues();
newValues.put("USERNAME", userName);
newValues.put("FORMATTEDDATE",timedate);
// Insert the row into your table
db.insert("LOGIN", null, newValues);
///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
}
答案 0 :(得分:0)
我自己通过将格式更改为simpledate然后在代码中进行了一些更改来获得解决方案
时间表方法:
public static String getDateTime() {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"dd/MM/yyyy HH:mm:ss", Locale.getDefault());
Date date = new Date();
// Toast.makeText(dateFormat, Toast.LENGTH_SHORT).show();
return dateFormat.format(date);
}
存储在DB中的数据:
public void addLogin(String Name){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_USERNAME,Name);
values.put(KEY_TIME,getDateTime());
db.insert(TABLE_LOGIN,null, values);
db.close();
}
从DB回复:
public Data getLogin(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_LOGIN, new String[] { KEY_ID,
KEY_USERNAME, KEY_TIME }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Data contact = new Data(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
// return contact
return contact;
}