androidandroid-tablelayoutandroid-tablet-layout

Updating to a table in Android results in duplication of shown records


Working in Android, I have a table on a tab that I am pulling from an internal database. After entering data on another tab, I would like for the table to be updated to show the new records. Currently I am calling the following,

public void createDataTable(View v) {
    DBAdapter msdb= new DBAdapter(getActivity().getApplicationContext(),MainActivity.szDbName, null);
    db=msdb.getWritableDatabase();

    Cursor c=db.rawQuery("SELECT * FROM surveyDB", null);
    Integer iRowid = c.getColumnIndex("_id");
    Integer iSpecies = c.getColumnIndex("species");
    Integer iCount = c.getColumnIndex("surveycount");
    
    if(c.getCount()>0){
        c.moveToFirst();
        do{
            //row=new TableRow(this.getActivity());
            final TableRow row=new TableRow(this.getActivity());
            row.setClickable(true);
            //Setting up the first column parameters
            tvRowid=new TextView(getActivity());
            tvRowid.setText(c.getString(iRowid));
            row.addView(tvRowid);

            //Setting up the second column parameters
            tvSpecies=new TextView(getActivity());
            tvSpecies.setText(c.getString(iSpecies));
            tvSpecies.setTextAppearance(getActivity(), R.style.myStyle);
            tvSpecies.setGravity(Gravity.RIGHT);
            row.addView(tvSpecies); //adding column to row

            //Setting up third column parameters
            tvSurveyCount=new TextView(getActivity());
            tvSurveyCount.setText(c.getString(iCount));
            tvSurveyCount.setTextAppearance(getActivity(), R.style.myStyle);
            tvSurveyCount.setPadding(4, 1, 4, 1);
            tvSurveyCount.setGravity(Gravity.RIGHT);
            row.addView(tvSurveyCount);

        tableLayout.addView(row,new TableLayout.LayoutParams(
                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
   }while(c.moveToNext());
        c.close();
        db.close();
    }else{
        Toast.makeText(getActivity().getApplicationContext(), "The table is empty. Please add data.", Toast.LENGTH_LONG).show();
    }
}

However, this results in duplication of records as shown below. Creating three records: seven dogs, five cats, and 15 parakeets shows the following table,

Rowid Species Count
   1    dog     7   
   1    dog     7
   2    cat     5
   1    dog     7
   2    cat     5
   3    parakeet 15

even though the records in the database are correct (no duplications). Basically the table was recreated three times with the database contents appended to existing records on the tab.

Question: How do I just call a refresh from the database where the table on the tab gets updated from the records in the database to reflect added records, as well as updates to previous records? Thanks in advance.


Solution

  • If you keep adding rows to the TableLayout each time you call createDataTable without clearing the rows you added in a previous call to createDataTable then you will get this affect.

    The solution is to call tableLayout.removeAllViews() at the beginning of createDataTable