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();
}
}
}