如何使表的每一行从数据库中获取每一行

时间:2014-11-18 22:49:46

标签: android sqlite

我正在尝试从sqlite数据库中获取数据到tablelayout中,希望数据库的每一行都在表的行中...我对Android世界有点新意,所以我只是得到了每一列数据库进入表列..但是我无法使db中的每一行进入表的行。  希望有人可以提供帮助 所以这是xml文件:

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 
android:layout_width="match_parent"
 android:layout_height="match_parent">
 <TableLayout 
     android:id="@+id/tabla_cuerpo"
      android:layout_height="wrap_content"
       android:layout_width="match_parent"
        android:background="#000000">
        <TableRow 
            android:id="@+id/tableRow1"
             android:layout_width="match_parent"
              android:layout_height="wrap_content">
              <TextView 
                  android:textColor="#000" 
                  android:textStyle="bold" 
                  android:gravity="center_horizontal" 
                  android:background="#FFFFFF" 
                  android:layout_margin="1dip" 
                  android:id="@+id/textview" 
                  android:layout_weight="0.3" 
                  android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium" 
                    android:text="جدول المواد"/>
        </TableRow>



             <TableRow >
             <TextView 
                  android:background="#FFFFFF"
                   android:layout_margin="1dip" 
                   android:id="@+id/tv1"
                    android:layout_weight="0.3" 
                    android:layout_width="wrap_content"
                     android:layout_height="wrap_content" 
                     android:textAppearance="?android:attr/textAppearanceMedium"


                      />
              <TextView 
                  android:background="#FFFFFF"
                   android:layout_margin="1dip" 
                   android:id="@+id/textv"
                    android:layout_weight="0.3" 
                    android:layout_width="wrap_content"
                     android:layout_height="wrap_content" 
                     android:textAppearance="?android:attr/textAppearanceMedium"


                      />

             <TextView 
                  android:background="#FFFFFF"
                   android:layout_margin="1dip" 
                   android:id="@+id/tv2"
                    android:layout_weight="0.3" 
                    android:layout_width="wrap_content"
                     android:layout_height="wrap_content" 
                     android:textAppearance="?android:attr/textAppearanceMedium"
                      />
             <TextView 
                  android:background="#FFFFFF"
                   android:layout_margin="1dip" 
                   android:id="@+id/tv3"
                    android:layout_weight="0.3" 
                    android:layout_width="wrap_content"
                     android:layout_height="wrap_content" 
                     android:textAppearance="?android:attr/textAppearanceMedium"
                      />
               <TextView 
                  android:background="#FFFFFF"
                   android:layout_margin="1dip" 
                   android:id="@+id/tv4"
                    android:layout_weight="0.3" 
                    android:layout_width="wrap_content"
                     android:layout_height="wrap_content" 
                     android:textAppearance="?android:attr/textAppearanceMedium"
                      />
             <TextView 
                  android:background="#FFFFFF"
                   android:layout_margin="1dip" 
                   android:id="@+id/tv5"
                    android:layout_weight="0.3" 
                    android:layout_width="wrap_content"
                     android:layout_height="wrap_content" 
                     android:textAppearance="?android:attr/textAppearanceMedium"
                      />
              </TableRow>

这是尝试获取数据的java文件

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.subjectschd);

    TextView tv1 =(TextView) findViewById(R.id.tv1);
    TextView tv2 =(TextView) findViewById(R.id.tv2);
    TextView tv3 =(TextView) findViewById(R.id.tv3);
    TextView tv4 =(TextView) findViewById(R.id.tv4);
    TextView tv5 =(TextView) findViewById(R.id.tv5);
    Getdata  info = new Getdata (this);
    info.openDataBase();
    String data=info.gettermsched();

    String data2=info.getermsched2();
    String data3=info.getermsched3();
    String data4=info.getermsched4();
    String data5=info.getermsched5();

    tv1.setText(data);
    tv2.setText(data2);
    tv3.setText(data3);
    tv4.setText(data4);
    tv5.setText(data5);

    info.close();

这就是我获取列的方式..我不知道如何获取行也是如此。 我真的很感激任何帮助。

1 个答案:

答案 0 :(得分:0)

您需要对数据库执行查询,我建议您创建SQLiteOpenHelper的子类,然后创建此方法

public List<MyData> getAllData(){
     List<MyData> allData = new ArrayList<MyData>();
     Cursor cursor = getReadableDatabase().rawQuery("SELECT * FROM MY_TABLE);

     //Move the cursor on all your rows using cursor.moveToNext() method

     return allData;
}

您还需要知道如何从光标中检索数据,查找有关这些内容的信息。