android.widget.TableRow tidak dapat ditransmisikan ke android.widget.TableLayout

Saya menulis aplikasi Android pertama saya, CRUD sederhana. Tapi setelah saya mengubah GridView ke TableLayout saya mendapatkan kesalahan berikut:

android.widget.TableRow cannot be cast to android.widget.TableLayout

Inilah aktivitas saya:

public class AllActivity extends Activity {

    private SQLiteDatabase sampleDB;
    private TableLayout tableList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.all);





        sampleDB = this.openOrCreateDatabase("my_app",
                SQLiteDatabase.CREATE_IF_NECESSARY, null);


        String query = "SELECT * FROM BOOKS;";
        Cursor cursor = sampleDB.rawQuery(query, null);


        tableList = (TableLayout)findViewById(R.id.tableList);

        if(cursor.moveToFirst())
        {
            do
            {
                TableRow row = new TableRow(this);

                TextView id = new TextView(this);
                id.setText(""+cursor.getLong(0));

                TextView title = new TextView(this);
                title.setText(cursor.getString(1));

                TextView author = new TextView(this);
                author.setText(cursor.getString(2));


                row.addView(id);
                row.addView(title);
                row.addView(author);

                tableList.addView(row);



            }while(cursor.moveToNext());

        }

    }


}

Pandangan ku:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <TableRow
        android:id="@+id/tableList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </TableRow>

</LinearLayout>

Ada tips apa yang saya lakukan salah?
Saya melakukan semuanya seperti di Print out Konten ArrayList di Android TableLayout tetapi tidak dapat berfungsi.


person user1409508    schedule 23.01.2014    source sumber


Jawaban (1)


android.widget.TableRow cannot be cast to android.widget.TableLayout

Kamu punya

 <TableRow
    android:id="@+id/tableList" // table row
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</TableRow>

Tapi saat menginisialisasi Anda punya

 tableList = (TableLayout)findViewById(R.id.tableList);
 // casting tablerow to tablelayout

Anda mungkin ingin memeriksanya

http://www.mkyong.com/android/android-tablelayout-example/

Anda juga dapat menemukan contoh tata letak tabel di sampel SDK @

android-sdk/samples/android-17/ApiDemos/res/layout/table_layout_1.xml

person Raghunandan    schedule 23.01.2014
comment
Tentu saja! Sial... kesalahan bodoh. Saya yakin saya menyeret TableLayout dari editor visual. Terimakasih atas bantuannya. Saya akan menerima jawaban dalam 8 menit - person user1409508; 23.01.2014