SQLite에서 편집 텍스트가 포함 된 양식에서 직원 정보를 가져온 데이터베이스를 만들었으며 다른 활동의 목록보기에 직원의 이름을 표시하려고합니다. 단일 이름을 클릭하면 내 앱이 다음 활동에서 완전한 정보를 표시하게하고, 친절하게도 목록보기를 만들고 데이터베이스에서 이름을 가져 와서 클릭 할 수있게 만들어 emloyee의 전체 정보를 엽니 다. . 여기 SQLite 데이터베이스에서 목록보기에 단일 항목을 표시하는 방법
이 내 InformationDataBase 어댑터 클래스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 });
}
}
인 코드 입니다 그리고 이것은
공용 클래스 DbHelper가 {
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);
}
}
SQLiteOpenHelper를 확장 내 데이터베이스 헬퍼 클래스그리고 이것은 자바 cl입니다 정보 양식 활동의 엉덩이
공용 클래스 정보 그것은 많은 코드를 필요로의 활동 {
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();
}
}