直到应用程序被销毁并重新启动(sqlite 游标),文本才会更新
text is not updated until application is destroyed and restarted (sqlite cursor)
我正在开发一个 android 应用程序,该应用程序使用 sqlite 数据库来包含用户输入的信息。我有两个活动。当前主要 activity 显示来自数据库的用户信息。第二个 activity 仅用于输入该数据。当第二个 activity 打开时,我在编辑文本框中看到保存的数据。我可以修改和保存数据。但是,当返回到第一个 activity 时,那里显示的数据不会更新。如果我关闭应用程序并重新启动它,新数据将正确显示在第一个 activity 中,但我需要它立即显示。我已经尝试覆盖 onResume() 认为这会有所帮助。但到目前为止它对我没有帮助。请帮我找出问题所在。这是我的主要 activity,第一个 activity:
public class LaunchActivity extends AppCompatActivity{
final String SCOPE ="https://www.googleapis.com/auth/gmail.send";
private static final int REQUEST_RESOLVE_AUTH_ERROR = 1006;
private static final int REQUEST_SMS = 645;
private ImageView mImageView;
private TextView nameField;
private TextView phoneField;
private TextView emailField;
private ClientInfoDBHelper mInfoDBHelper;
private Cursor mCursor;
private String mPhoneNumber = "**********";
private String[] permissions = new String[]{Manifest.permission.SEND_SMS, Manifest.permission.READ_PHONE_STATE};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
nameField = (TextView) findViewById(R.id.nameView);
phoneField = (TextView) findViewById(R.id.phoneView);
emailField = (TextView) findViewById(R.id.emailView);
mInfoDBHelper = new ClientInfoDBHelper(this);
SQLiteDatabase dbReader = mInfoDBHelper.getReadableDatabase();
String[] projection = {
ClientInfoContract.ClientInfoEntry.FIRST_NAME,
ClientInfoContract.ClientInfoEntry.LAST_NAME,
ClientInfoContract.ClientInfoEntry.PHONE_NUMBER,
ClientInfoContract.ClientInfoEntry.EMAIL_ADDRESS
};
String where = ClientInfoContract.ClientInfoEntry._ID + " = ?";
String[] whereArgs = {"1"};
String sortOrder = ClientInfoContract.ClientInfoEntry.FIRST_NAME + " DESC";
mCursor = dbReader.query(
ClientInfoContract.ClientInfoEntry.TABLE_NAME,
projection,
where,
whereArgs,
null,
null,
sortOrder
);
mCursor.moveToNext();
updateDisplayInfo(mCursor);
//request permissions
int MyVersion = Build.VERSION.SDK_INT;
if (MyVersion> Build.VERSION_CODES.LOLLIPOP_MR1){
if (hasPermission()){
ActivityCompat.requestPermissions(this, permissions, REQUEST_SMS);
}
}
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Are you certain you'd like to alert your attorney?", Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.snack_bar_send, new View.OnClickListener(){
@Override
public void onClick(View v){
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(mPhoneNumber, null, "testing", null, null);
new MailTask().execute();
}
}).show();
}
});
mImageView = (ImageView) findViewById(R.id.imageView);
mImageView.setImageDrawable(getResources().getDrawable(R.drawable.no_handcuffs));
}
@Override
public void onResume(){
super.onResume();;
updateDisplayInfo(mCursor);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_launch, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
switch (id){
case R.id.action_settings:
//null
break;
case R.id.userInfo:
Intent infoIntent = new Intent(this, UserInfoActivity.class);
startActivity(infoIntent);
break;
}
return super.onOptionsItemSelected(item);
}
private boolean hasPermission(){
int result = ContextCompat.checkSelfPermission(this, permissions[0]);
if (result==PackageManager.PERMISSION_DENIED){
return true;
}else {
return false;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
//temporarily null// may be used in future to handle multiple permissions
}
public void updateDisplayInfo(Cursor curse){
//curse.moveToNext();
StringBuilder sb = new StringBuilder();
sb.append(curse.getString(curse.getColumnIndex(ClientInfoContract.ClientInfoEntry.FIRST_NAME)))
.append(" ")
.append(curse.getString(curse.getColumnIndex(ClientInfoContract.ClientInfoEntry.LAST_NAME)));
nameField.setText(sb.toString());
}
}
你是从第二个 activity 回到第一个 activity 吗?理想情况下 onCreate()
应该只用于初始化数据或附加到视图。如果您的数据正在更改,您应该在 onStart()
或 onResume()
中进行操作
因此,在您的 onStart()
方法中添加以下代码并从 onCreate()
中删除。
mInfoDBHelper = new ClientInfoDBHelper(this);
SQLiteDatabase dbReader = mInfoDBHelper.getReadableDatabase();
String[] projection = {
ClientInfoContract.ClientInfoEntry.FIRST_NAME,
ClientInfoContract.ClientInfoEntry.LAST_NAME,
ClientInfoContract.ClientInfoEntry.PHONE_NUMBER,
ClientInfoContract.ClientInfoEntry.EMAIL_ADDRESS
};
String where = ClientInfoContract.ClientInfoEntry._ID + " = ?";
String[] whereArgs = {"1"};
String sortOrder = ClientInfoContract.ClientInfoEntry.FIRST_NAME + " DESC";
mCursor = dbReader.query(
ClientInfoContract.ClientInfoEntry.TABLE_NAME,
projection,
where,
whereArgs,
null,
null,
sortOrder
);
mCursor.moveToNext();
updateDisplayInfo(mCursor);
onCreate()
仅在创建视图时调用一次。然后在您的 onResume()
中,您尝试使用相同的光标,因此您不会获得更新的数据。所以你将不得不再次查询以获取更新的数据。
或者您可以 startActivityForResult()
并在 onActivityResult()
方法中通过触发新查询来更新您的数据。
我正在开发一个 android 应用程序,该应用程序使用 sqlite 数据库来包含用户输入的信息。我有两个活动。当前主要 activity 显示来自数据库的用户信息。第二个 activity 仅用于输入该数据。当第二个 activity 打开时,我在编辑文本框中看到保存的数据。我可以修改和保存数据。但是,当返回到第一个 activity 时,那里显示的数据不会更新。如果我关闭应用程序并重新启动它,新数据将正确显示在第一个 activity 中,但我需要它立即显示。我已经尝试覆盖 onResume() 认为这会有所帮助。但到目前为止它对我没有帮助。请帮我找出问题所在。这是我的主要 activity,第一个 activity:
public class LaunchActivity extends AppCompatActivity{
final String SCOPE ="https://www.googleapis.com/auth/gmail.send";
private static final int REQUEST_RESOLVE_AUTH_ERROR = 1006;
private static final int REQUEST_SMS = 645;
private ImageView mImageView;
private TextView nameField;
private TextView phoneField;
private TextView emailField;
private ClientInfoDBHelper mInfoDBHelper;
private Cursor mCursor;
private String mPhoneNumber = "**********";
private String[] permissions = new String[]{Manifest.permission.SEND_SMS, Manifest.permission.READ_PHONE_STATE};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
nameField = (TextView) findViewById(R.id.nameView);
phoneField = (TextView) findViewById(R.id.phoneView);
emailField = (TextView) findViewById(R.id.emailView);
mInfoDBHelper = new ClientInfoDBHelper(this);
SQLiteDatabase dbReader = mInfoDBHelper.getReadableDatabase();
String[] projection = {
ClientInfoContract.ClientInfoEntry.FIRST_NAME,
ClientInfoContract.ClientInfoEntry.LAST_NAME,
ClientInfoContract.ClientInfoEntry.PHONE_NUMBER,
ClientInfoContract.ClientInfoEntry.EMAIL_ADDRESS
};
String where = ClientInfoContract.ClientInfoEntry._ID + " = ?";
String[] whereArgs = {"1"};
String sortOrder = ClientInfoContract.ClientInfoEntry.FIRST_NAME + " DESC";
mCursor = dbReader.query(
ClientInfoContract.ClientInfoEntry.TABLE_NAME,
projection,
where,
whereArgs,
null,
null,
sortOrder
);
mCursor.moveToNext();
updateDisplayInfo(mCursor);
//request permissions
int MyVersion = Build.VERSION.SDK_INT;
if (MyVersion> Build.VERSION_CODES.LOLLIPOP_MR1){
if (hasPermission()){
ActivityCompat.requestPermissions(this, permissions, REQUEST_SMS);
}
}
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Are you certain you'd like to alert your attorney?", Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.snack_bar_send, new View.OnClickListener(){
@Override
public void onClick(View v){
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(mPhoneNumber, null, "testing", null, null);
new MailTask().execute();
}
}).show();
}
});
mImageView = (ImageView) findViewById(R.id.imageView);
mImageView.setImageDrawable(getResources().getDrawable(R.drawable.no_handcuffs));
}
@Override
public void onResume(){
super.onResume();;
updateDisplayInfo(mCursor);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_launch, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
switch (id){
case R.id.action_settings:
//null
break;
case R.id.userInfo:
Intent infoIntent = new Intent(this, UserInfoActivity.class);
startActivity(infoIntent);
break;
}
return super.onOptionsItemSelected(item);
}
private boolean hasPermission(){
int result = ContextCompat.checkSelfPermission(this, permissions[0]);
if (result==PackageManager.PERMISSION_DENIED){
return true;
}else {
return false;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
//temporarily null// may be used in future to handle multiple permissions
}
public void updateDisplayInfo(Cursor curse){
//curse.moveToNext();
StringBuilder sb = new StringBuilder();
sb.append(curse.getString(curse.getColumnIndex(ClientInfoContract.ClientInfoEntry.FIRST_NAME)))
.append(" ")
.append(curse.getString(curse.getColumnIndex(ClientInfoContract.ClientInfoEntry.LAST_NAME)));
nameField.setText(sb.toString());
}
}
你是从第二个 activity 回到第一个 activity 吗?理想情况下 onCreate()
应该只用于初始化数据或附加到视图。如果您的数据正在更改,您应该在 onStart()
或 onResume()
因此,在您的 onStart()
方法中添加以下代码并从 onCreate()
中删除。
mInfoDBHelper = new ClientInfoDBHelper(this);
SQLiteDatabase dbReader = mInfoDBHelper.getReadableDatabase();
String[] projection = {
ClientInfoContract.ClientInfoEntry.FIRST_NAME,
ClientInfoContract.ClientInfoEntry.LAST_NAME,
ClientInfoContract.ClientInfoEntry.PHONE_NUMBER,
ClientInfoContract.ClientInfoEntry.EMAIL_ADDRESS
};
String where = ClientInfoContract.ClientInfoEntry._ID + " = ?";
String[] whereArgs = {"1"};
String sortOrder = ClientInfoContract.ClientInfoEntry.FIRST_NAME + " DESC";
mCursor = dbReader.query(
ClientInfoContract.ClientInfoEntry.TABLE_NAME,
projection,
where,
whereArgs,
null,
null,
sortOrder
);
mCursor.moveToNext();
updateDisplayInfo(mCursor);
onCreate()
仅在创建视图时调用一次。然后在您的 onResume()
中,您尝试使用相同的光标,因此您不会获得更新的数据。所以你将不得不再次查询以获取更新的数据。
或者您可以 startActivityForResult()
并在 onActivityResult()
方法中通过触发新查询来更新您的数据。