ログイン後、サーバーからのデータが同期され、データベースに保存されます。データが読み込まれ、カスタムリストビューに表示されます。しかし、それは働いていない、私はそのデータが表示されますバック主な活動に、新しいアクティビティを開く必要があり、ここでMainActivityは次のとおりです。最初の起動時にカスタムリストビューにデータを表示する方法
public class NavigationDrawer extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener {
// danh sach thu
private UserDatabase userDb;
private SQLiteDatabase mSQLite;
private dbLaiXuat laiXuatDb;
private SQLiteDatabase mDbLaiXuat;
private dbThu dbthu;
private SQLiteDatabase mDbthu;
private Cursor mCursorthu;
// danh sach chi
private dbChi dbchi;
private SQLiteDatabase mDbchi;
private Cursor mCursorchi;
private BaoCao objectchi2;
private Firebase usersRef;
private ArrayList<BaoCao> arrthu;
private ArrayList<BaoCao> arrchi;
// test
private ArrayList<TienThuChi> sapxepthu = null;
private ArrayList<TienThuChi> sapxepchi = null;
private ArrayAdapter<String> adapterthu = null;
private ArrayAdapter<String> adapterchi = null;
private Context context = this;
// income
private ListView listthu;
private ListView listchi;
//sua thu
private EditText suatienthu;
private Spinner suanhomthu;
private TextView suangaythu;
private EditText suaghichuthu;
private ImageButton btnsavethu, btnsuangaythu;
private String[] arrspinnerthu = {"Tiền Lương", "Đòi Nợ", "Bán Đồ", "Đi Vay", "Khác"};
//sua chi
private EditText suatienchi;
private Spinner suanhomchi;
private TextView suangaychi;
private EditText suaghichuchi;
private ImageButton btnsavechi, btnsuangaychi;
private String[] arrspinnerchi = {"Ăn Uống", "Quần Áo", "Cho vay", "Sinh Hoạt", "Đi Lại", "Trả Nợ", "Khác"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nav);
dbthu = new dbThu(this);
dbchi = new dbChi(this);
listthu = (ListView) findViewById(R.id.listView_danhsachkhoanthu);
listchi = (ListView) findViewById(R.id.listView_danhsachkhoanchi);
danhSachThu();
danhSachChi();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Firebase.setAndroidContext(this);
Firebase root = new Firebase("https://expenseproject.firebaseio.com/");
userDb = new UserDatabase(this);
laiXuatDb = new dbLaiXuat(this);
usersRef = root.child(getUid());
loadTab();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//su kien khi click vào listview thu
listthu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(final AdapterView<?> parent, final View view, final int position, long id) {
AlertDialog.Builder b = new AlertDialog.Builder(NavigationDrawer.this);
b.setTitle("Lựa Chọn Của Bạn");
b.setMessage("Bạn Muốn Gì?");
b.setPositiveButton("Sửa", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final Dialog dialogthu = new Dialog(context);
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.suakhoanthu, parent, false);
dialogthu.setContentView(v);
dialogthu.setTitle("Sửa Khoản Thu");
dialogthu.show();
// link to layout
suatienthu = (EditText) dialogthu.findViewById(R.id.editTextsuathu_tienkhoanthu);
suanhomthu = (Spinner) dialogthu.findViewById(R.id.spinnersuathu_nhomkhoanthu);
suangaythu = (TextView) dialogthu.findViewById(R.id.textViewsuathu_ngaykhoanthu);
suaghichuthu = (EditText) dialogthu.findViewById(R.id.editTextsuathu_ghichukhoanthu);
dexuatthu();
// pick time
btnsuangaythu = (ImageButton) dialogthu.findViewById(R.id.imageButtonsuathu_chonngaykhoanthu);
btnsuangaythu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDatePickerDialog(null);
}
});
// set value to dialog
suatienthu.setText(sapxepthu.get(position).getTien());
suangaythu.setText(sapxepthu.get(position).getNgaythang());
suaghichuthu.setText(sapxepthu.get(position).getGhichu());
adapterthu = new ArrayAdapter<>(NavigationDrawer.this, android.R.layout.simple_spinner_dropdown_item, arrspinnerthu);
suanhomthu.setAdapter(adapterthu);
suanhomthu.setSelection(checkPosition(sapxepthu.get(position).getNhom(), arrspinnerthu));
// save button
btnsavethu = (ImageButton) dialogthu.findViewById(R.id.imageButtonsuathu_luukhoanthu);
btnsavethu.setOnClickListener(new View.OnClickListener() {
@SuppressWarnings("static-access")
@Override
public void onClick(View v) {
mDbthu = dbthu.getWritableDatabase();
ContentValues cv = new ContentValues();
String cost = suatienthu.getText().toString();
String date = suangaythu.getText().toString();
String type = suanhomthu.getSelectedItem().toString();
String note = suaghichuthu.getText().toString();
cv.put(dbThu.COL_TIEN, cost);
cv.put(dbThu.COL_DATE, date);
cv.put(dbThu.COL_NHOM, type);
cv.put(dbThu.COL_GHICHU, note);
mDbthu.update(dbThu.TABLE_NAME, cv, "_id " + "=" + sapxepthu.get(position).getId(), null);
danhSachThu();
dialogthu.cancel();
}
});
}
});
b.setNegativeButton("Xóa", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mDbthu = dbthu.getWritableDatabase();
String id = sapxepthu.get(position).getId();
mDbthu.delete(dbThu.TABLE_NAME, "_id=?", new String[]{id});
danhSachThu();
}
});
b.create().show();
}
});
//su kien khi click vào listview chi
listchi.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(final AdapterView<?> parent, View view,
final int position, long id) {
AlertDialog.Builder b = new AlertDialog.Builder(NavigationDrawer.this);
b.setTitle("Lựa Chọn Của Bạn");
b.setMessage("Bạn Muốn Gì?");
b.setPositiveButton("Sửa", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final Dialog dialogchi = new Dialog(context);
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.suakhoanchi, parent, false);
dialogchi.setContentView(v);
dialogchi.setTitle("Sửa Khoản Chi");
dialogchi.show();
// link to layout
suatienchi = (EditText) dialogchi.findViewById(R.id.editTextsuachi_tienkhoanthu);
suanhomchi = (Spinner) dialogchi.findViewById(R.id.spinnersuachi_nhomkhoanthu);
suangaychi = (TextView) dialogchi.findViewById(R.id.textViewsuachi_ngaykhoanthu);
suaghichuchi = (EditText) dialogchi.findViewById(R.id.editTextsuachi_ghichukhoanthu);
dexuatchi();
// pick time
btnsuangaychi = (ImageButton) dialogchi.findViewById(R.id.imageButtonsuachi_chonngaykhoanthu);
btnsuangaychi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDatePickerDialogchi(null);
}
});
// set value to dialog
suatienchi.setText(sapxepchi.get(position).getTien());
suangaychi.setText(sapxepchi.get(position).getNgaythang());
suaghichuchi.setText(sapxepchi.get(position).getGhichu());
adapterchi = new ArrayAdapter<>(NavigationDrawer.this, android.R.layout.simple_spinner_dropdown_item, arrspinnerchi);
suanhomchi.setAdapter(adapterchi);
suanhomchi.setSelection(checkPosition(sapxepchi.get(position).getNhom(), arrspinnerchi));
// save button
btnsavechi = (ImageButton) dialogchi.findViewById(R.id.imageButtonsuachi_luukhoanthu);
btnsavechi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDbchi = dbchi.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(dbChi.COL_TIEN, suatienchi.getText().toString());
cv.put(dbChi.COL_DATE, suangaychi.getText().toString());
cv.put(dbChi.COL_NHOM, suanhomchi.getSelectedItem().toString());
cv.put(dbChi.COL_GHICHU, suaghichuchi.getText().toString());
mDbchi.update(dbChi.TABLE_NAME, cv, "_id " + "=" + sapxepchi.get(position).getId(), null);
dialogchi.cancel();
danhSachChi();
}
});
}
});
b.setNegativeButton("Xóa", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mDbchi = dbchi.getWritableDatabase();
String id = sapxepchi.get(position).getId();
mDbchi.delete(dbChi.TABLE_NAME, "_id=?", new String[]{id});
danhSachChi();
}
});
b.create().show();
}
});
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_diagram) {
arrayThu();
arrayChi();
if (arrthu.size() == 0 || arrchi.size() == 0) {
Toast toast = Toast.makeText(NavigationDrawer.this, "Danh sách rỗng", Toast.LENGTH_SHORT);
toast.show();
} else {
Intent chuyen = new Intent(NavigationDrawer.this, BaoCaoThuChi.class);
startActivity(chuyen);
}
} else if (id == R.id.nav_exchange) {
Intent chuyen = new Intent(NavigationDrawer.this, NgoaiTe.class);
startActivity(chuyen);
} else if (id == R.id.nav_deposit) {
Intent chuyen = new Intent(NavigationDrawer.this, LaiXuat.class);
startActivity(chuyen);
} else if (id == R.id.nav_sync) {
if (checkConn()) {
syncData();
Toast.makeText(NavigationDrawer.this, "Sync success", Toast.LENGTH_SHORT).show();
} else {
AlertDialog.Builder a = new AlertDialog.Builder(NavigationDrawer.this);
a.setTitle("Kiểm tra kết nối");
a.setMessage("Không có kết nối Internet");
a.setNegativeButton("OK", null);
a.show();
}
} else if (id == R.id.nav_user_info) {
} else if (id == R.id.nav_logout) {
AlertDialog.Builder exitDialog = new AlertDialog.Builder(NavigationDrawer.this);
exitDialog.setTitle("Quản lý chi tiêu");
exitDialog.setMessage("Bạn có thực sự muốn đăng xuất chương trình không?");
exitDialog.setNegativeButton("Có", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mSQLite = userDb.getWritableDatabase();
mDbthu = dbthu.getWritableDatabase();
mDbchi = dbchi.getWritableDatabase();
mDbLaiXuat = laiXuatDb.getWritableDatabase();
mSQLite.execSQL("delete from " + UserDatabase.TABLE_NAME);
mDbthu.execSQL("delete from " + dbThu.TABLE_NAME);
mDbchi.execSQL("delete from " + dbChi.TABLE_NAME);
mDbLaiXuat.execSQL("delete from " + dbLaiXuat.TABLE_NAME);
mSQLite.close();
mDbthu.close();
mDbchi.close();
mDbLaiXuat.close();
Intent intent = new Intent(NavigationDrawer.this, LoginActivity.class);
startActivity(intent);
finish();
}
});
exitDialog.setPositiveButton("Để sau", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
exitDialog.setCancelable(false);
exitDialog.show();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void danhSachThu() {
mDbthu = dbthu.getReadableDatabase();
String query = "select * from " + dbThu.TABLE_NAME;
Cursor mCursorthu = mDbthu.rawQuery(query, null);
ArrayList<TienThuChi> arrthu = new ArrayList<>();
DoiNgay doingaythu = new DoiNgay();
TienThuChi objectthu;
if (mCursorthu.moveToFirst()) {
do {
objectthu = new TienThuChi();
objectthu.setId(mCursorthu.getString(0));
objectthu.setTien(mCursorthu.getString(1));
objectthu.setNhom(mCursorthu.getString(2));
objectthu.setNgaythang(doingaythu.doiDate(mCursorthu.getString(3)));
objectthu.setGhichu(mCursorthu.getString(4));
arrthu.add(objectthu);
} while (mCursorthu.moveToNext());
}
mCursorthu.close();
mDbthu.close();
sapxepthu = new ArrayList<>();
sapxepthu = doingaythu.sapXep(arrthu);
for (int i = 0; i < sapxepthu.size(); i++) {
String strsapxep = doingaythu.ngay(sapxepthu.get(i).getNgaythang());
sapxepthu.get(i).setNgaythang(strsapxep);
}
DanhSachThu myadapterthu = new DanhSachThu(NavigationDrawer.this, R.layout.t_customlayout_thu, sapxepthu);
listthu.setAdapter(myadapterthu);
}
public void danhSachChi() {
mDbchi = dbchi.getReadableDatabase();
String querychi = "select * from " + dbChi.TABLE_NAME;
Cursor mCursorchi = mDbchi.rawQuery(querychi, null);
ArrayList<TienThuChi> arrchi = new ArrayList<>();
DoiNgay doingaychi = new DoiNgay();
TienThuChi objectchi;
if (mCursorchi.moveToFirst()) {
do {
objectchi = new TienThuChi();
objectchi.setId(mCursorchi.getString(0));
objectchi.setTien(mCursorchi.getString(1));
objectchi.setNhom(mCursorchi.getString(2));
objectchi.setNgaythang(doingaychi.doiDate(mCursorchi.getString(3)));
objectchi.setGhichu(mCursorchi.getString(4));
arrchi.add(objectchi);
} while (mCursorchi.moveToNext());
}
mCursorchi.close();
mDbchi.close();
sapxepchi = new ArrayList<>();
sapxepchi = doingaychi.sapXep(arrchi);
for (int i = 0; i < sapxepchi.size(); i++) {
String strsapxep = doingaychi.ngay(sapxepchi.get(i).getNgaythang());
sapxepchi.get(i).setNgaythang(strsapxep);
}
DanhSachChi myadapterchi = new DanhSachChi(NavigationDrawer.this, R.layout.t_customlayout_chi, sapxepchi);
listchi.setAdapter(myadapterchi);
}
@Override
protected void onResume() {
super.onResume();
danhSachThu();
danhSachChi();
}
public void loadTab() {
final TabHost tab = (TabHost) findViewById(android.R.id.tabhost);
//goi cau lenh setup
tab.setup();
TabHost.TabSpec spec;
//tao tab 1
spec = tab.newTabSpec("t1");
spec.setContent(R.id.tab1);
spec.setIndicator("DANH SÁCH THU");
tab.addTab(spec);
//tao tab2
spec = tab.newTabSpec("t2");
spec.setContent(R.id.tab2);
spec.setIndicator("DANH SÁCH CHI");
tab.addTab(spec);
tab.setCurrentTab(0);
}
}
あなたは新しい活動が実行されることを意味するか、またはあなたがGUIように、新しいアクティビティを開く必要があります"リフレッシュ"? – chntgomez
このアクティビティが開いているとき、listviewはデータベースからすべてのデータを読み込み、listviewに表示します。 –
私はこの問題を解決しました。それは非同期なのでfirebaseからデータを取得するからです。私のアプリが起動すると、データはまだ完全には取得されていません。 –