0

I'm trying to get data from sqlite database into tablelayout want each each row of the database be in the row of the table ... I'm kind of new to the Android world so I just did get each column of the database into table column .. but I couldn't make each row in the db into the row of the table. Hope anyone can help. So this is the xml file:

<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>

and this is the java file that tries to get the data

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

This is how I got the columns .. I don't have any idea of how I can get the rows too. I'll appreciate any help really.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
marwa
  • 13
  • 4
  • [Use a SimpleCursorAdapter](http://stackoverflow.com/questions/12077955/android-using-simplecursoradapter-to-get-data-from-database-to-listview). – CL. Nov 19 '14 at 08:06

1 Answers1

0

You need to execute a query on your database, I suggest you to make a subclass of SQLiteOpenHelper and then create this method

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

You also need to know how to retrieve data from a Cursor, look for info about that's all.

rickyalbert
  • 2,552
  • 4
  • 21
  • 31
  • actually i did that but i got all the rows in one row in the table ... and i know about the cursor thing .. my problem is i want the each row to be locate in different row in the table – marwa Nov 19 '14 at 00:11
  • You have to create a TableRow view for each data you have, and then call addView(tableRow) on the TableLayout. I think that only the TableLayout can be defined in xml, then you have to create programmatically your own tablerow – rickyalbert Nov 19 '14 at 00:14