2014-03-24 36 views
0

因此,我有这个类显示数据库内部和应用程序,我想知道是否有可能将一个滚动条放入应用程序中,以便在数据库变得太大而无法使用屏幕时可以简单地向下滚动显示表格中的所有元素?Android数据库滚动条

以下是此类的代码

package com.example.app;

import android.app.Activity; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.graphics.Color; 
import android.graphics.Typeface; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 

import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.Toast; 




public class CurrentBets extends Activity { 
    private String fname,lname,email; 
    private Button button; 
    SQLiteDatabase db; 
    TableRow tableRow; 
    TextView textView,textView1,textView2,textView3,textView4,textView5; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_current_bets); 
     db=openOrCreateDatabase("MyDB1",MODE_PRIVATE, null); 
     db.execSQL("CREATE TABLE IF NOT EXISTS Student(fname VARCHAR,lname VARCHAR,email VARCHAR);"); 
     button = (Button) findViewById(R.id.button1); 

     button.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       Toast.makeText(getApplicationContext(), 
         "Bet Was Added", Toast.LENGTH_LONG).show(); 

      } 
     }); 
    } 
    public void data(View view) 
    { 
     EditText edittext1=(EditText)findViewById(R.id.firstname); 
     EditText edittext2=(EditText)findViewById(R.id.lastname); 
     EditText edittext3=(EditText)findViewById(R.id.email); 
     fname=edittext1.getText().toString(); 
     lname=edittext2.getText().toString(); 
     email=edittext3.getText().toString(); 
     db.execSQL("INSERT INTO Student VALUES('"+fname+"','"+lname+"','"+email+"');"); 


    } 
    public void showdata(View view) 
    { 
     Cursor c=db.rawQuery("SELECT * from Student", null); 
     int count= c.getCount(); 
     c.moveToFirst(); 
     TableLayout tableLayout = new TableLayout(getApplicationContext()); 
     tableLayout.setVerticalScrollBarEnabled(true); 
     TableRow tableRow; 
     TextView textView,textView1,textView2,textView3,textView4,textView5; 
     tableRow = new TableRow(getApplicationContext()); 
     textView=new TextView(getApplicationContext()); 
     textView.setText("Stake"); 
     textView.setTextColor(Color.RED); 
     textView.setTypeface(null, Typeface.BOLD); 
     textView.setPadding(20, 20, 20, 20); 
     tableRow.addView(textView); 
     textView4=new TextView(getApplicationContext()); 
     textView4.setText("Odds"); 
     textView4.setTextColor(Color.RED); 
     textView4.setTypeface(null, Typeface.BOLD); 
     textView4.setPadding(20, 20, 20, 20); 
     tableRow.addView(textView4); 
     textView5=new TextView(getApplicationContext()); 
     textView5.setText("Returns"); 
     textView5.setTextColor(Color.RED); 
     textView5.setTypeface(null, Typeface.BOLD); 
     textView5.setPadding(20, 20, 20, 20); 
     tableRow.addView(textView5); 
     tableLayout.addView(tableRow); 
     for (Integer j = 0; j < count; j++) 
     { 
      tableRow = new TableRow(getApplicationContext()); 
      textView1 = new TextView(getApplicationContext()); 
      textView1.setText(c.getString(c.getColumnIndex("fname"))); 
      textView2 = new TextView(getApplicationContext()); 
      textView2.setText(c.getString(c.getColumnIndex("lname"))); 
      textView3 = new TextView(getApplicationContext()); 
      textView3.setText(c.getString(c.getColumnIndex("email"))); 
      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() ; 
     } 
     setContentView(tableLayout); 
     db.close(); 
    } 
    public void close(View view) 
    { 
     System.exit(0); 
    } 

} 

回答

0

我会建议使用ListView或GridView并将项目加载到适配器以获得更好的性能。 ListView或GridView将负责滚动。如果你想使用TableView,将它包装在ScrollView中,它应该可以解决你的问题。