每当数据库中的数据更新时,如何更新网格布局数据?每当数据库中的数据更新时,我想从服务中广播一条消息并在此 Acitivty 上接收。但是,如果我调用updateSummary()
广播接收器的接收,它会将新子代添加到现有的 gridLyout 子代中。如何替换现有的子代并定期刷新网格布局?
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
updateSummary();
}
public void updateSummary()
{
gridLayout=(GridLayout)findViewById(R.id.newgrid);
DBHelper dbHelper = new DBHelper(this);
SQLiteDatabase mySummaryDB = dbHelper.getWritableDatabase();
if (mySummaryDB != null) {
String mycols=DBConstants.ID+","+DBConstants.NUMBER+","+DBConstants.DATE+","+DBConstants.TIME;
Cursor c0 = mySummaryDB.rawQuery("select distinct("+DBConstants.NUMBER+") from "+DBConstants.MASTER_DATA_TABLE+"", null);
if(c0!=null)
{
if(c0.moveToFirst())
{
do
{
String number = c0.getString(c0.getColumnIndex(DBConstants.NUMBER));
Cursor c1 = mySummaryDB.rawQuery("select "+mycols+" from "+DBConstants.MASTER_DATA_TABLE+" where "+DBConstants.NUMBER+"='"+NUMBER+"' order by "+DBConstants.MYDATETIME+" desc limit 1", null);
Log.i("summary",Integer.toString(c1.getCount()));
if(c1!=null)
{
if(c1.moveToFirst())
{
int increment=0;
do
{
String id = c1.getString(c1.getColumnIndex(DBConstants.ID));
String date = c1.getString(c1.getColumnIndex(DBConstants.DATE));
String time = c1.getString(c1.getColumnIndex(DBConstants.TIME));
Cursor c2 = mySummaryDB.query(DBConstants.MYDATA_TABLE,new String[]{DBConstants.NAME}, DBConstants.NUMBER+"=?", new String[]{number}, null, null, null);
String name="";
if(c2!=null)
{
if(c2.moveToFirst())
{
name = c2.getString(c2.getColumnIndex(DBConstants.NAME));
Log.i("summary", "name->"+name);
}
}
c2.close();
TextView nameTV = new TextView(this);
nameTV.setText(name);
gridLayout.addView(nameTV);
TextView dateTV = new TextView(this);
dateTV.setText(date);
gridLayout.addView(dateTV);
TextView timeTV = new TextView(this);
timeTV.setText(time);
gridLayout.addView(timeTV);
}
increment=increment+1;
}
while(c1.moveToNext());
}
}
}
while(c0.moveToNext());
}
}
dbHelper.close();
}