Reputation: 8071
How to update Grid Layout data whenever data updated in Database? I want to broadcast a message from service and receive on this Acitivty whenever data updated in database. But If I call updateSummary()
on Receive of broadcast receiver it add new child to existing gridLyout children.How do I replace existing child and refresh the Grid Layout periodically?
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();
}
Upvotes: 0
Views: 1907
Reputation: 1763
I am not sure about Cursor part of it, but in general this is the approach you can use to update the GridView.
References: 1. Building Layouts with an Adapter 2. Hold View Objects in a View Holder
Upvotes: 0
Reputation: 605
I am not 100% sure if I know what you want to do, but it seems to me as if you want to have three TextViews in your GridLayout which should be updated when the method updateSummary is called. If so, you should define them in the xml layout file of your activity and give them an id. Using the function textView = (TextView) findViewById(R.id.id_of_your_component)
in your activity you can access the TextViews from within your java code via the id and then you can update the texts of the three components with textView.setText("your text")
as it seems that this is what you really want to do. At the moment you create new TextViews everytime the method updateSummary is called and add them to the GridLayout, but you only have to instantiate them one time (best in xml layout) and then update them in your method.
Upvotes: 1