Question

I am working on a DataTable in a Fragment that pulls data from a SQLite database and will display it in a table that is browsable up and down and side to side. I am having a difficult time getting the final table to show after the FOR loop iterates. I think that the problem lies in the last line. What is the appropriate way to show the table after the last row is inserted? TIA...

public class DataTable extends Fragment {
    SQLiteDatabase db;  
    String rowid, species, area, sampler;
    TableRow tableRow;
    TextView textView, textView1, textView2, textView3, textView4, textView5;

.
.
.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {         
        //View view = inflater.inflate(R.layout.datatable, container, false);
        View view = inflater.inflate(R.layout.datatable, container, false);

        //GridView gridView = (GridView) view.findViewById(R.id.grid);  //this is fine  
        TableLayout tablelayout = (TableLayout)this.getActivity().findViewById(R.id.table);

        DBAdapter msdb= new DBAdapter(getActivity().getApplicationContext(),"adfg", null);     
        db=msdb.getWritableDatabase();
        showdata(view);
        return view;    
    }

public void showdata(View view){
    Cursor c = db.rawQuery("SELECT * FROM surveyDB",null);
    int count= c.getCount();
    c.moveToFirst();

    TableLayout tableLayout = new TableLayout(getActivity().getApplicationContext
    tableLayout.setVerticalScrollBarEnabled(true);
    TableRow tableRow;
    TextView textView,textView1,textView2,textView3,textView4,textView5;

    tableRow = new TableRow(getActivity().getApplicationContext());

    textView=new TextView(getActivity().getApplicationContext());
    textView.setText("Rowid");
    textView.setTextColor(Color.RED);
    textView.setTypeface(null, Typeface.BOLD);
    textView.setPadding(20, 20, 20, 20);
    tableRow.addView(textView);
.
.
.
    tableLayout.addView(tableRow)  
            for (Integer j = 0; j < count; j++)
            {
                tableRow = new TableRow(getActivity().getApplicationContext());

                textView1 = new TextView(getActivity().getApplicationContext());
                textView1.setText(c.getString(c.getColumnIndex("_id")));
                //Toast.makeText(getActivity().getApplicationContext(), "SAVE Successful."+textView1+".", Toast.LENGTH_LONG).show();    
                textView2 = new TextView(getActivity().getApplicationContext());
                textView2.setText(c.getString(c.getColumnIndex("species")));

                textView3 = new TextView(getActivity().getApplicationContext());
                textView3.setText(c.getString(c.getColumnIndex("area")));

                //still need sampler...

                textView1.setPadding(20, 20, 20, 20);
                textView2.setPadding(20, 20, 20, 20);
                textView3.setPadding(20, 20, 20, 20);

                tableRow.addView(textView1);
                tableRow.addView(textView2);
                tableRow.addView(textView3);

                tableLayout.addView(tableRow);
                c.moveToNext() ;
            }
         c.close();      
         db.close();
      // setContentView(tableLayout);
         return ;       
    }
}
Was it helpful?

Solution

This works,

public class DataTable extends Fragment {   
    SQLiteDatabase database, db;

    TableLayout tableLayout;
    TableRow row;
    TextView firstCol;
    TextView secondCol;
    TextView thirdCol;
    TextView fourthCol;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {         
        View view = inflater.inflate(R.layout.datatable, container, false);
        tableLayout = (TableLayout) view.findViewById(R.id.table);

        DBAdapter msdb= new DBAdapter(getActivity().getApplicationContext(),"adfg", null);     
        db=msdb.getWritableDatabase();
        displayDB();
        return view;    
    }
    private void displayDB() {
        Cursor c=db.rawQuery("SELECT * FROM surveyDB", null);
        Integer index0=c.getColumnIndex("_id");
        Integer index1 = c.getColumnIndex("species");   
        Integer index2 = c.getColumnIndex("area");
        Integer index3 = c.getColumnIndex("sampler");

        if(c.getCount()>0){
            c.moveToFirst();
            do{
                row=new TableRow(getActivity());
                row.setId(100);
                row.setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT));  
                //Setting up the first column parameters
                firstCol=new TextView(getActivity());
                firstCol.setText(c.getString(index0));
                firstCol.setTextSize(12);
                firstCol.setTextColor(Color.YELLOW);
                firstCol.setLayoutParams(new LayoutParams(
                        LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));
                row.addView(firstCol); 
                //Setting up the second column parameters           
                secondCol=new TextView(getActivity());
                secondCol.setText(c.getString(index1));
                secondCol.setTextColor(Color.YELLOW);
                secondCol.setTextSize(12);
                secondCol.setLayoutParams(new LayoutParams(
                        LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));
                row.addView(secondCol); //adding coloumn to row
                //Setting up the third column parameters
                thirdCol=new TextView(getActivity());
                thirdCol.setText(c.getString(index2));
                thirdCol.setTextColor(Color.YELLOW);
                thirdCol.setTextSize(12);
                thirdCol.setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT));
                row.addView(thirdCol); //adding column to row
                //Setting up fourth column parameters
                fourthCol=new TextView(getActivity());
                fourthCol.setText(c.getString(index3));
                fourthCol.setTextColor(Color.YELLOW);
                fourthCol.setTextSize(12);
                fourthCol.setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT));                
                row.addView(fourthCol); 

                tableLayout.addView(row,new TableLayout.LayoutParams(
                            LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT));                
            }while(c.moveToNext());
            db.close();
        }else{
            Toast.makeText(getActivity().getApplicationContext(), "Event occurred.", Toast.LENGTH_LONG).show();
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top