データベースに従業員テーブルがあり、ユーザーが従業員の詳細をテーブルに挿入するアクティビティがあります
ここで、特定の従業員も更新する必要があるため、同じ xml を使用して、異なるボタンを使用して、更新ボタンと挿入ボタンの可視性をそれぞれ非表示および可視に設定しました。
ユーザーが更新をクリックすると、同じ xml を表示したいのですが、Edittexts には従業員の詳細が既に入力されており、編集可能になります。
どうやってやるの ??
これは私のxmlです
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/background"
android:gravity="top" >
<TextView
android:id="@+id/TV"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="20sp"
android:gravity="center_horizontal"
android:text="Employee Entry details"
android:textColor="#000000"
android:textSize="35sp"
android:textStyle="italic" />
<LinearLayout
android:id="@+id/mainll"
android:layout_width="fill_parent"
android:layout_height="650sp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/TV"
android:layout_marginTop="10dp"
android:orientation="vertical" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50sp">
<TextView
android:id="@+id/enametext"
android:layout_width="260sp"
android:layout_height="wrap_content"
android:text="Employee Name:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"
android:textSize="30sp" />
<EditText
android:id="@+id/enameedit"
android:layout_width="340sp"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp">
<TextView
android:id="@+id/edesignationtext"
android:layout_width="260sp"
android:layout_height="wrap_content"
android:text="Employee Designation:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"
android:textSize="30sp"/>
<EditText
android:id="@+id/edesignationedit"
android:layout_width="340sp"
android:layout_height="wrap_content" >
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp">
<TextView
android:id="@+id/enumbertext"
android:layout_width="260sp"
android:layout_height="wrap_content"
android:text="Employee Phone Number:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"
android:textSize="30sp"/>
<EditText
android:id="@+id/enumberedit"
android:layout_width="340sp"
android:layout_height="wrap_content"
>
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp">
<TextView
android:id="@+id/edobtext"
android:layout_width="260sp"
android:layout_height="wrap_content"
android:text="Employee D.O.B :"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"
android:textSize="30sp"/>
<EditText
android:id="@+id/edobedit"
android:layout_width="250sp"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/dateOfBirth"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date"
android:textColor="#000000"
android:textSize="30sp"
android:background="#ffffff" />
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp">
<TextView
android:id="@+id/ebasicpaytext"
android:layout_width="260sp"
android:layout_height="wrap_content"
android:text="Employee Basic Pay :"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"
android:textSize="30sp"/>
<EditText
android:id="@+id/ebasicpayedit"
android:layout_width="340sp"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow
android:id="@+id/tableRow6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp">
<TextView
android:id="@+id/epftext"
android:layout_width="260sp"
android:layout_height="wrap_content"
android:text="Employee PF deduction :"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"
android:textSize="30sp"/>
<EditText
android:id="@+id/epfedit"
android:layout_width="340sp"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow
android:id="@+id/tableRow6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp">
<TextView
android:id="@+id/eotherdeductionstext"
android:layout_width="260sp"
android:layout_height="wrap_content"
android:text="Employee Other Deductions :"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"
android:textSize="30sp"/>
<EditText
android:id="@+id/eotherdeductionedit"
android:layout_width="340sp"
android:layout_height="wrap_content"
/>
</TableRow>
</LinearLayout>
<TableRow
android:id="@+id/inserttablerow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/mainll"
android:layout_marginTop="61dp"
android:visibility="visible" >
<Button
android:id="@+id/savebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70sp"
android:background="#ffffff"
android:text="Save"
android:textColor="#000000"
android:textSize="30sp" />
<Button
android:id="@+id/cancelbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100sp"
android:background="#ffffff"
android:text="Cancel"
android:textColor="#000000"
android:textSize="30sp" />
<Button
android:id="@+id/addanotheremployeebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:layout_marginLeft="100sp"
android:text="Add new"
android:textColor="#000000"
android:textSize="30sp" />
</TableRow>
<TableRow
android:id="@+id/updatetablerow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/mainll"
android:layout_marginTop="61dp"
android:visibility="gone" >
<Button
android:id="@+id/updatesavebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="160sp"
android:background="#ffffff"
android:text="Update"
android:textColor="#000000"
android:textSize="30sp" />
<Button
android:id="@+id/updatecancelbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100sp"
android:background="#ffffff"
android:text="Cancel"
android:textColor="#000000"
android:textSize="30sp" />
</TableRow>
そして、datahelper クラスの挿入メソッドと更新メソッド
//inserting into employeetable
public void insertDataIntoEmployee(String name_STR, String designation_STR, String phonenumber_STR, String age_STR,
String basicpay_STR, String pfdeduction_STR, String otherdeductions_STR) {
System.out.println(" insertData start ");
cv = new ContentValues();
cv.put("ename", name_STR);
cv.put("edesignation", designation_STR);
cv.put("ephonenumber", phonenumber_STR);
cv.put("eage", age_STR);
cv.put("ebasic", basicpay_STR);
cv.put("epf", pfdeduction_STR);
cv.put("eotherdeductions", otherdeductions_STR);
this.db.insert(EMPLOYEE_TABLE_NAME, null, cv);
System.out.println(" insertData end ");
}
// updating the table
public void updateDataIntoEmployee(long rowId ,String name_STR, String designation_STR, String phonenumber_STR, String age_STR,
String basicpay_STR, String pfdeduction_STR, String otherdeductions_STR) {
System.out.println(" updateData start ");
cv = new ContentValues();
cv.put("ename", name_STR);
cv.put("edesignation", designation_STR);
cv.put("ephonenumber", phonenumber_STR);
cv.put("eage", age_STR);
cv.put("ebasic", basicpay_STR);
cv.put("epf", pfdeduction_STR);
cv.put("eotherdeductions", otherdeductions_STR);
this.db.update(EMPLOYEE_TABLE_NAME, cv, EMPLOYEE_ID + "=" + rowId, null);
System.out.println(" updateData end ");
}
そして、入力されたedittextを表示する必要があるクラスの私のメソッドはここにあります
public void onCreate(Bundle savedState) {
EditText editText = (EditText) findViewById(R.id.my_edittext);
String value = // Query your 'last value'.
editText.setText(value);
}
取得できる文字列値で渡す必要があるクエリは何でしょうか。編集テキストごとに毎回このコードを書き直す必要がありますか? 冗長性を回避する方法はありますか?
ありがとうございました