我在SQLite中创建了一个数据库,它从包含编辑文本的表单中获取员工信息,我想在另一个活动的列表视图中显示员工的姓名,当我单击一个名称时,我想要我的应用程序在下一个活动中显示完整信息,请帮助我创建列表视图并从数据库中获取名称并使其可单击以打开emloyee的完整信息。
这是代码 这是我的InformationDataBase Adapter类
public class InformationDataBaseAdapter {
public static final String ID_COLUMN = "id";
public static final String NAME_COLUMN2 = "name";
public static final String FNAME_COLUMN = "fname";
public static final String EMPLOYEE_DOB = "dob";
public static final String EMPLOYEE_INTEREST = "interest";
public static final String EMPLOYEE_ADDRESS = "address";
public static final String EMPLOYEE_CONTACT = "contact";
static final String DATABASE_NAME = "information.db";
static final String EMPLOYEE_TABLE = "employee";
static final int DATABASE_VERSION = 1;
static final String CREATE_EMPLOYEE = "CREATE TABLE "
+ EMPLOYEE_TABLE + " (" + ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ NAME_COLUMN2 + " TEXT NOT NULL, " + FNAME_COLUMN + " TEXT NOT NULL, " + EMPLOYEE_INTEREST + " TEXT, "
+ EMPLOYEE_DOB + " DATE, " + EMPLOYEE_ADDRESS + " TEXT, " + EMPLOYEE_CONTACT + " INTEGER );";
public SQLiteDatabase db;
private final Context context;
private DbHelper dbHelper;
public InformationDataBaseAdapter(Context _context) {
context = _context;
dbHelper = new DbHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public InformationDataBaseAdapter open() throws SQLException {
db = dbHelper.getWritableDatabase();
return this;
}
public void close() {
db.close();
}
public SQLiteDatabase getDatabaseInstance() {
return db;
}
public void insertEntry(String Name, String FName, String Interest ,String DOB,String Address,String Contact) {
ContentValues newValues = new ContentValues();
newValues.put("NAME", Name);
newValues.put("Fname", FName);
newValues.put("Interest", Interest);
newValues.put("DOB", DOB);
newValues.put("ADDRESS", Address);
newValues.put("CONTACT", Contact);
db.insert("EMPLOYEE_TABLE", null, newValues);
}
public int deleteEntry(String Name) {
String where = "NAME=?";
int numberOFEntriesDeleted = db.delete("INFORMATION", where,
new String[] { Name });
return numberOFEntriesDeleted;
}
public String getSinlgeEntry(String Name) {
Cursor cursor = db.query("EMPLOYEE", null, " NAME=?",
new String[] { Name }, null, null, null);
if (cursor.getCount() < 1) // UserName Not Exist
{
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String info = cursor.getString(cursor.getColumnIndex("INFO"));
cursor.close();
return info;
}
public void updateEntry(String ID, String Name, String FName, String Interest ,String DOB,String Address,String Contact) {
ContentValues updatedValues = new ContentValues();
updatedValues.put("NAME", Name);
updatedValues.put("Fname", FName);
updatedValues.put("Interest", Interest);
updatedValues.put("DOB", DOB);
updatedValues.put("ADDRESS", Address);
updatedValues.put("CONTACT", Contact);;
String where = "NAME = ?";
db.update("INFORMTION", updatedValues, where, new String[] { Name });
}
}
这是我的数据库助手类
public class DbHelper扩展了SQLiteOpenHelper {
public DbHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(LoginDataBaseAdapter.DATABASE_CREATE);
db.execSQL(InformationDataBaseAdapter.CREATE_EMPLOYEE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int _oldVersion, int _newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + "TEMPLATE");
onCreate(db);
}
}
这是java类信息表单活动
公共类信息扩展活动{
TextView Tv1,Tv2,Tv3,Tv4,Tv5,Tv6,Tv7;
EditText Etname,EtFname,Etinterest,EtDOB,EtAddress,EtContact;
Button Bsubmit;
InformationDataBaseAdapter informationDataBaseAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.information);
informationDataBaseAdapter=new InformationDataBaseAdapter(this);
informationDataBaseAdapter=informationDataBaseAdapter.open();
Tv1=(TextView)findViewById(R.id.InfoTV1);
Tv2=(TextView)findViewById(R.id.InfoTV2);
Tv3=(TextView)findViewById(R.id.InfoTV3);
Tv4=(TextView)findViewById(R.id.InfoTV4);
Tv5=(TextView)findViewById(R.id.InfoTV5);
Tv6=(TextView)findViewById(R.id.InfoTV6);
Tv7=(TextView)findViewById(R.id.InfoTV7);
Etname=(EditText)findViewById(R.id.InfoET1);
EtFname=(EditText)findViewById(R.id.InfoET2);
Etinterest=(EditText)findViewById(R.id.InfoET3);
EtDOB=(EditText)findViewById(R.id.InfoET4);
EtAddress=(EditText)findViewById(R.id.InfoET5);
EtContact=(EditText)findViewById(R.id.InfoET6);
Bsubmit=(Button)findViewById(R.id.bsubmitInfo);
Bsubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String Name=Etname.getText().toString();
String FName=EtFname.getText().toString();
String Interest=Etinterest.getText().toString();
String DOB=EtDOB.getText().toString();
String Address=EtAddress.getText().toString();
String Contact=EtContact.getText().toString();
// check if any of the fields are vaccant
if(Name.equals("")||FName.equals("")||Interest.equals("")||DOB.equals("")||Address.equals("")||Contact.equals(""))
{
AlertDialog.Builder alertBuilder=new AlertDialog.Builder(Information.this);
alertBuilder.setTitle("Invalid Data");
alertBuilder.setMessage("Please, Enter valid data");
alertBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertBuilder.create().show();
}
else
{
// Save the Data in Database
informationDataBaseAdapter.insertEntry(Name,FName,Interest,DOB,Address,Contact);
Toast.makeText(getApplicationContext(), "Information successfully added", Toast.LENGTH_LONG).show();
Intent i2= new Intent(Information.this,Menu.class);
startActivity(i2);
}
}
});
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
informationDataBaseAdapter.close();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
答案 0 :(得分:0)
这需要很多代码。我想告诉你我对程序结构的看法。无论如何,你必须自己做你想做的事。
在Android中使用数据库应该通过ContentProvider https://developer.android.com/reference/android/content/ContentProvider.html。这是开发人员指南https://developer.android.com/guide/topics/providers/content-provider-basics.html,但我认为,您应该从github下载一些项目,看看它是如何工作的。从此处克隆所有代码https://github.com/bazli/android_packages_apps_AlarmClock/blob/donut/src/com/android/alarmclock/AlarmProvider.java并修改。
要从数据库加载数据,请使用CursorLoader https://developer.android.com/reference/android/content/CursorLoader.html。关于装载机,您可以在这里阅读https://developer.android.com/guide/components/loaders.html。
使用SimpleCursorAdapter https://developer.android.com/reference/android/support/v4/widget/SimpleCursorAdapter.html处理来自数据库的数据并在ListView中显示
我认为,您可以尽早使用片段,因此您只需在用户点击ListView中的某个项目后启动新的活动
希望它有所帮助!祝好运!