我正在研究一个片段中的DataTable,它从SQLite数据库中提取数据,并将其显示在一个可上下左右浏览的表中。在FOR循环迭代之后,我很难获得要显示的最终表。我认为问题出在最后一行。插入最后一行后,显示表格的适当方式是什么?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 ;
}
}