我一直在尝试更新列表视图中的已编辑记录。当用户按下所选列表视图时,它将转到另一个页面以允许编辑信息。但是,当用户返回列表视图时,数据仍然不会更改。如何在列表视图中显示已编辑的数据?
public class NewRecord extends AppCompatActivity {
Context context = this;
public RecordsDataSource recordsdatasource;
String textInEditText;
ArrayAdapter<Records> RecordAdapter;
List<Records> records;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_record);
recordsdatasource = new RecordsDataSource(this);
recordsdatasource.open();
final EditText EmployeeName = (EditText) findViewById(R.id.EmployeeName);
EmployeeName.setText(recordsdatasource.getFirst().getEmployeeName());
final EditText BirthDate = (EditText) findViewById(R.id.BirthDate);
BirthDate.setText(recordsdatasource.getFirst().getBirthDate());
final EditText Address = (EditText) findViewById(R.id.Address);
Address.setText(recordsdatasource.getFirst().getAddress());
final EditText Phone = (EditText) findViewById(R.id.Phone);
Phone.setText(recordsdatasource.getFirst().getPhone());
final EditText DutyType = (EditText) findViewById(R.id.DutyType);
DutyType.setText(recordsdatasource.getFirst().getType());
final EditText BankNo = (EditText) findViewById(R.id.BankNo);
BankNo.setText(recordsdatasource.getFirst().getBankNo());
Button Button4 = (Button) findViewById(R.id.button4);
Button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder a_builder = new AlertDialog.Builder(NewRecord.this);
a_builder.setMessage("Confirm save?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Records newRecord = new Records();
newRecord.setEmployeeName(EmployeeName.getText().toString());
newRecord.setBirthDate(BirthDate.getText().toString());
newRecord.setAddress(Address.getText().toString());
newRecord.setPhone(Phone.getText().toString());
newRecord.setType(DutyType.getText().toString());
newRecord.setBankNo(BankNo.getText().toString());
createRecord(newRecord);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = a_builder.create();
alert.setTitle(Html.fromHtml("<font color='#08ae9e'>Confirmation</font>"));
alert.show();
}
});
Button button9 = (Button) findViewById(R.id.button9);
button9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder a_builder = new AlertDialog.Builder(NewRecord.this);
a_builder.setMessage("Confirm save?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Records newRecord = new Records();
newRecord.setId(1);
newRecord.setEmployeeName(EmployeeName.getText().toString());
newRecord.setBirthDate(BirthDate.getText().toString());
newRecord.setAddress(Address.getText().toString());
newRecord.setPhone(Phone.getText().toString());
newRecord.setType(DutyType.getText().toString());
newRecord.setBankNo(BankNo.getText().toString());
updateRecord(newRecord);
Intent returnIntent = new Intent();
setResult(RESULT_OK, returnIntent);
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = a_builder.create();
alert.setTitle(Html.fromHtml("<font color='#08ae9e'>Confirmation</font>"));
alert.show();
}
});
}
public static void showToast(Context context, String text) {
Toast.makeText(context, text, Toast.LENGTH_LONG).show();
}
private void createRecord (Records record) {
recordsdatasource.createRecord(record);
showToast(getApplicationContext(), "Record added");
}
private void updateRecord (Records record) {
recordsdatasource.updateRecord(record);
showToast(getApplicationContext(), "Record updated");
}
}
Main Page
public class Record extends ListActivity {
public Button NewButton;
public RecordsDataSource Recordsdatasource;
ArrayAdapter<Records> RecordAdapter;
List<Records> records;
ListView listView;
Records selectedRecord;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record);
Addrecords();
//Referencing the Database
Recordsdatasource = new RecordsDataSource(this);
Recordsdatasource.open();
//set the listView to use the custom List Adapter
records = (List<Records>) Recordsdatasource.getAll();
RecordAdapter = new RecordAdapter(this, 0, (ArrayList<Records>) records);
listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(RecordAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectedRecord = records.get(position);
AlertDialog.Builder a_builder = new AlertDialog.Builder(Record.this);
a_builder.setMessage("Are you sure you want to edit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent l = new Intent(Record.this, NewRecord.class);
startActivity(l);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = a_builder.create();
alert.setTitle(Html.fromHtml("<font color='#08ae9e'>Information</font>"));
alert.show();
updateItem(selectedRecord);
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
selectedRecord = records.get(position);
AlertDialog.Builder a_builder = new AlertDialog.Builder(Record.this);
a_builder.setMessage("Are you sure you want to delete?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
deleteItem(selectedRecord);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = a_builder.create();
alert.setTitle(Html.fromHtml("<font color='#08ae9e'>Alert!!</font>"));
alert.show();
return true;
}
});
return;
}
public void Addrecords() {
NewButton = (Button) findViewById(R.id.NewButton);
NewButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent addrecords = new Intent(Record.this, NewRecord.class);
startActivity(addrecords);
}
});
}
public static void showToast(Context context, String text) {
Toast.makeText(context, text, Toast.LENGTH_LONG).show();
}
private void deleteItem(Records selectedRecord) {
Recordsdatasource.removeRecords(selectedRecord);
showToast(this, "Record deleted");
RecordAdapter.notifyDataSetChanged();
RecordAdapter.notifyDataSetInvalidated();
refreshDisplay();
}
private void updateItem(Records selectedRecord) {
Recordsdatasource.updateRecord(selectedRecord);
RecordAdapter.notifyDataSetChanged();
refreshDisplay();
}
private void refreshDisplay() {
records = Recordsdatasource.getAll();
RecordAdapter.clear();
RecordAdapter.addAll(records);
}
}
答案 0 :(得分:2)
您可以执行以下操作:
public void onClick(DialogInterface dialog, int which) {
Intent l = new Intent(Record.this, NewRecord.class);
startActivityForResult(l, REQUEST_CODE_EDIT);
}
完成NewRecord
Activity
后:
setResult(RESULT_OK);
finish();
覆盖onActivityResult
课程中的Record
:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_EDIT && resultCode == RESULT_OK) {
// refreshing your list
RecordAdapter.notifyDataSetChanged();
}
}
答案 1 :(得分:0)
Call this method in your onBackPressed Method
public static void temp(Context context, Class<?> cls){
Intent intent = new Intent(context, cls);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
}