该应用有两项活动:
首先是+ button
和listview
。当您点击按钮时,使用edittext
和ADDbutton
打开第二个活动。
单击ADD按钮时,数据写入数据库,然后第二个活动关闭。
那么,当第二项活动关闭时如何制作 - 首次活动刷新中的listview
第一项活动:
public class MainScreen extends FragmentActivity implements OnClickListener {
final String LOG_TAG = "myLogs";
ListView listViewMain;
ImageButton addButton;
SQLiteDatabase db;
DataBase DB;
DBHelper dbHelper;
Cursor cursor;
SimpleCursorAdapter passListViewAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen);
addButton = (ImageButton) findViewById(R.id.addButton);
addButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d(LOG_TAG, "add button : ");
Intent intentMain = new Intent(MainScreen.this, AddItem.class);
startActivity(intentMain);
}
});
DB = new DataBase(this);
listViewMain = (ListView) findViewById(R.id.listViewMain);
String[] from = new String[] { DataBase.COLUMN_TITLE, DataBase.COLUMN_DATE};
int[] to = new int[] { R.id.titleView, R.id.dateView };
if (passListViewAdapter == null) {
passListViewAdapter = new SimpleCursorAdapter(
this,
R.layout.item,
DB.fetchAllPass(),
from,
to
, SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
} else {
passListViewAdapter.notifyDataSetChanged();
}
listViewMain.setAdapter(passListViewAdapter);
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
}
第二个活动(添加行项目):
public class AddItem extends MainScreen implements OnClickListener{
final String LOG_TAG = "myLogs";
EditText comment_enter, link_enter, password_enter, login_enter, title_enter, date_enter;
Button add_item_button;
DBHelper db;
DataBase DB;
SimpleCursorAdapter passListViewAdapter;
SimpleDateFormat sdf;
private Cursor cursor = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_item);
comment_enter = (EditText) findViewById(R.id.comment_enter);
link_enter = (EditText) findViewById(R.id.link_enter);
password_enter = (EditText) findViewById(R.id.password_enter);
login_enter = (EditText) findViewById(R.id.login_enter);
title_enter = (EditText) findViewById(R.id.title_enter);
date_enter = (EditText) findViewById(R.id.date_enter);
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
String date = sdf.format(new Date(System.currentTimeMillis()));
date_enter.setText(date);
add_item_button = (Button) findViewById(R.id.add_item_button);
add_item_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d(LOG_TAG, "add_item_button : ");
String title_str = title_enter.getText().toString();
String login_str = login_enter.getText().toString();
String pass_str = password_enter.getText().toString();
String link_str = link_enter.getText().toString();
String comm_str = comment_enter.getText().toString();
String date_str = date_enter.getText().toString();
Log.d(LOG_TAG, "Strings: " + title_str + " " + login_str + " " + pass_str + " " + link_str + " " + comm_str + " " + date_str);
DataBase DB = new DataBase(AddItem.this);
DB.open();
DB.insertPass(title_str, login_str, pass_str, link_str, comm_str, date_str);
DB.close();
Log.d(LOG_TAG, "after inserting into DB : ");
finish();
}
});
}
@Override
protected void onPause() {
super.onPause();
finish();
Log.d(LOG_TAG, "onPause : ");
}
}
数据库活动:
public long insertPass (String tt, String lg, String ps, String ul,
String cm, String dt) {
openGuard();
ContentValues cv = new ContentValues();
cv.put(COLUMN_TITLE, tt);
cv.put(COLUMN_LOGIN, lg);
cv.put(COLUMN_PASSWORD, ps);
cv.put(COLUMN_URL, ul);
cv.put(COLUMN_COMMENT, cm);
cv.put(COLUMN_DATE, dt);
return db.insert(TABLE_NAME, null, cv);}
public Cursor fetchAllPass() throws SQLException {
openGuard();
return db.query(TABLE_NAME, new String[] {COLUMN_ID, COLUMN_TITLE, COLUMN_DATE}, null, null, null, null, null);}
logcat的
02-12 17:24:44.626: D/AndroidRuntime(21123): Shutting down VM
02-12 17:24:44.626: W/dalvikvm(21123): threadid=1: thread exiting with uncaught exception (group=0x415e3c80)
02-12 17:24:44.626: E/AndroidRuntime(21123): FATAL EXCEPTION: main
02-12 17:24:44.626: E/AndroidRuntime(21123): Process: com.ssd.passwordmanager, PID: 21123
02-12 17:24:44.626: E/AndroidRuntime(21123): java.lang.RuntimeException: Unable to resume activity {com.ssd.passwordmanager/com.ssd.passwordmanager.AddItem}: java.lang.NullPointerException
02-12 17:24:44.626: E/AndroidRuntime(21123): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2808)
02-12 17:24:44.626: E/AndroidRuntime(21123): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2837)
02-12 17:24:44.626: E/AndroidRuntime(21123): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2270)
02-12 17:24:44.626: E/AndroidRuntime(21123): at android.app.ActivityThread.access$800(ActivityThread.java:145)
02-12 17:24:44.626: E/AndroidRuntime(21123): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
02-12 17:24:44.626: E/AndroidRuntime(21123): at android.os.Handler.dispatchMessage(Handler.java:102)
02-12 17:24:44.626: E/AndroidRuntime(21123): at android.os.Looper.loop(Looper.java:136)
02-12 17:24:44.626: E/AndroidRuntime(21123): at android.app.ActivityThread.main(ActivityThread.java:5081)
02-12 17:24:44.626: E/AndroidRuntime(21123): at java.lang.reflect.Method.invokeNative(Native Method)
02-12 17:24:44.626: E/AndroidRuntime(21123): at java.lang.reflect.Method.invoke(Method.java:515)
02-12 17:24:44.626: E/AndroidRuntime(21123): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
02-12 17:24:44.626: E/AndroidRuntime(21123): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-12 17:24:44.626: E/AndroidRuntime(21123): at dalvik.system.NativeStart.main(Native Method)
02-12 17:24:44.626: E/AndroidRuntime(21123): Caused by: java.lang.NullPointerException
02-12 17:24:44.626: E/AndroidRuntime(21123): at com.ssd.passwordmanager.MainScreen.onResume(MainScreen.java:111)
02-12 17:24:44.626: E/AndroidRuntime(21123): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
02-12 17:24:44.626: E/AndroidRuntime(21123): at android.app.Activity.performResume(Activity.java:5310)
答案 0 :(得分:0)
最简单的方法是在第一个活动的onResume
中添加加载代码部分。这样每次显示Activity时它都会刷新。