private void initData() {
// TODO Auto-generated method stub
mDatas = new ArrayList<Bean>()
SQLiteOpenHelper dbHelper = new DatabaseHelper(getActivity(), "friends.db", null, 1)
SQLiteDatabase db = dbHelper.getWritableDatabase()
// db.execSQL("delete from festival where festival='阿斯顿'")
Cursor cursor = db.rawQuery("select * from festival", null)
while (cursor.moveToNext()) {
String fes = cursor.getString(1)
String dat = cursor.getString(2)
//获取系统时间并组成字符串
final Calendar ca1 = Calendar.getInstance()
nYear = ca1.get(Calendar.YEAR)
nMonth = ca1.get(Calendar.MONTH)
nDay = ca1.get(Calendar.DAY_OF_MONTH)
Snyear = String.valueOf(nYear)
SnMonth = String.valueOf(nMonth + 1)
Sndays = String.valueOf(nDay)
if (nMonth >= 0 && nMonth <= 9) {
j = Snyear + "0" + SnMonth + Sndays
} else {
j = Snyear + SnMonth + Sndays
}
String i = getDateCount(dat, j)
Bean bean = new Bean(fes, dat, i)
mDatas.add(bean)
}
adapter = new MyAdapter(getActivity(), mDatas, R.layout.timeline_item)
listview.setAdapter(adapter)
}
- 将获取的时间和日期名称以及天数差放置于mDatas中,然后通过Cursor一组组取出来放置于listview中
- 但是,有个问题。我在另一个activity中获取输入的数据insert进SQLite,但是在initData()中不会实时更新,因为它是先读取的listview再通过添加数据操作存入SQLite,所以并不会自动更新listview。
SQLiteOpenHelper dbHelper = new DatabaseHelper(this, "friends.db", null, 1);
SQLiteDatabase db=dbHelper.getWritableDatabase();
final ContentValues values = new ContentValues();
String fes = dateTitle.getText().toString();
values.put("festival",fes);
values.put("time",i);
db.insert("festival",null,values);
- adapter.notifyDataSetChanged();我试了,但是它放在initData()中不会更新,因为我是往数据库中加,不是往mDatas中加,所以这个方法不成立
例如这样放置的话就可以动态添加
-// Button button = (Button) view.findViewById(R.id.button);
-// button.setOnClickListener(new View.OnClickListener() {
-// @Override
-// public void onClick(View v) {
-// Bean bean=new Bean("圣诞节", "2015-08-05", "1513");
-// mDatas.add(bean);
-// adapter.notifyDataSetChanged();
-// }
-// });
- 所以我想啊,那么就重新加载 Activity,因为fragment是在activity中的,所以结束activity生命周期再重新Onstart它,那样重新加载listview不就能起到一个刷新的效果了嘛
- 因此
Intent intent = new Intent(this,bottom.class);
startActivity(intent);
finish();