代码之家  ›  专栏  ›  技术社区  ›  portsample

从SQLite中提取片段中的DataTable。。。加载所有行后无法显示表

  •  1
  • portsample  · 技术社区  · 12 年前

    我正在研究一个片段中的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 ;       
        }
    }
    
    1 回复  |  直到 12 年前
        1
  •  0
  •   portsample    12 年前

    这是有效的,

    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();
            }
        }
    }
    
    推荐文章