Question

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();
    }
Was it helpful?

Solution

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.

OTHER TIPS

I am not sure about Cursor part of it, but in general this is the approach you can use to update the GridView.

  1. You need to monitor your DB changes (though some background thread).
  2. If the underlying dataset has changed, call notifyDataSetChanged (Ref: BaseAdapter.html#notifyDataSetChanged() or SimpleCursorAdapter.html#notifyDataSetChanged()
  3. Once the above method is called, the views would get refreshed when them become visible.
  4. You don't require to update all the cells in the GridView (but creating new TextView for each cell view).
  5. Implement the getView() method (which is called when ever the cells become visible) to return your custom view of the Cell in the GridView.
  6. Also use ViewHolder pattern so that you don't recreate each cell every time (more like caching). Make sure the data is set dynamically as part of each getView call (we only cache the Views - the UI part)

References: 1. Building Layouts with an Adapter 2. Hold View Objects in a View Holder

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top