在Android Stud中使表格行可以在Java中滚动

时间:2015-02-02 11:31:45

标签: java android tablelayout

我尝试在相对布局中创建滚动视图,以使我的表格布局可滚动,但我一直收到此错误:



02-02 19:29:10.116: E/AndroidRuntime(9400): 
java.lang.RuntimeException: Unable to start activity ComponentInfo{test.com.classmanagertest/test.com.classmanagertest.StudentsMasterList}: java.lang.IllegalStateException: 
The specified child already has a parent. You must call removeView() on the child's parent first.




这是我的表格布局,显示数​​据库中的字段:



@Override
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.students_masterlist);
        db=openOrCreateDatabase("ClassManager",MODE_WORLD_WRITEABLE, null);


        Cursor c=db.rawQuery("SELECT StudPic, StudentID, LastName FROM MasterStudents", null);
        int count= c.getCount();
        c.moveToFirst();
        TableLayout tableLayout = new TableLayout(getApplicationContext());
        tableLayout.setVerticalScrollBarEnabled(true);
        TableRow tableRow;
        TextView textView, textView1;
        ImageView StudImageView;

        RelativeLayout rl=(RelativeLayout)findViewById(R.id.layout);
        ScrollView sv = new ScrollView(this);
        sv.addView(tableLayout);
        rl.addView(sv);

        for (Integer j = 0; j < count; j++)
        {
            tableRow = new TableRow(getApplicationContext());

            StudImageView = new ImageView(getApplicationContext());
            StudImageView.setPadding(20, 20, 20, 20);
            StudImage=c.getBlob(c.getColumnIndex("StudPic"));
            Bitmap b1= BitmapFactory.decodeByteArray(StudImage, 0, StudImage.length);
            StudImageView.setImageBitmap(b1);
            tableRow.addView(StudImageView);

            textView1 = new TextView(getApplicationContext());
            textView1.setText(c.getString(c.getColumnIndex("StudentID")));
            textView1.setPadding(20, 20, 20, 20);
            textView1.setTextColor(getResources().getColor(R.color.blueactionbar));
            textView1.setTextSize(TypedValue.COMPLEX_UNIT_DIP,25);
            textView1.setTypeface(null, Typeface.BOLD);
            tableRow.addView(textView1);

            textView = new TextView(getApplicationContext());
            textView.setText(c.getString(c.getColumnIndex("LastName")));
            textView.setPadding(20, 20, 20, 20);
            textView.setTextColor(getResources().getColor(R.color.blueactionbar));
            textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,25);
            textView.setTypeface(null, Typeface.BOLD);
            tableRow.addView(textView);
            tableLayout.addView(tableRow);

            c.moveToNext() ;
        }
        setContentView(tableLayout);
        db.close();
    }
&#13;
&#13;
&#13;

这是我的xml文件:

&#13;
&#13;
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:layout_gravity="center_horizontal"
    android:scrollbars="vertical|horizontal"
    android:id="@+id/layout">



</RelativeLayout>
&#13;
&#13;
&#13;

如何解决此错误?

任何帮助表示赞赏!非常感谢你!

1 个答案:

答案 0 :(得分:1)

&#13;
&#13;
@Override
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.students_masterlist);
        db=openOrCreateDatabase("ClassManager",MODE_WORLD_WRITEABLE, null);


        Cursor c=db.rawQuery("SELECT StudPic, StudentID, LastName FROM MasterStudents", null);
        int count= c.getCount();
        c.moveToFirst();
        TableLayout tableLayout = new TableLayout(getApplicationContext());
        tableLayout.setVerticalScrollBarEnabled(true);
        TableRow tableRow;
        TextView textView, textView1;
        ImageView StudImageView;

        RelativeLayout rl=(RelativeLayout)findViewById(R.id.layout);
        ScrollView sv = new ScrollView(this);
        sv.addView(tableLayout);
        rl.addView(sv);
        for (Integer j = 0; j < count; j++)
        {
            tableRow = new TableRow(getApplicationContext());
            StudImageView = new ImageView(getApplicationContext());
            StudImageView.setPadding(20, 20, 20, 20);
            StudImage=c.getBlob(c.getColumnIndex("StudPic"));
            Bitmap b1= BitmapFactory.decodeByteArray(StudImage, 0, StudImage.length);
            StudImageView.setImageBitmap(b1);
            tableRow.addView(StudImageView);

            textView1 = new TextView(getApplicationContext());
            textView1.setText(c.getString(c.getColumnIndex("StudentID")));
            textView1.setPadding(20, 20, 20, 20);
            textView1.setTextColor(getResources().getColor(R.color.blueactionbar));
            textView1.setTextSize(TypedValue.COMPLEX_UNIT_DIP,25);
            textView1.setTypeface(null, Typeface.BOLD);
            tableRow.addView(textView1);

            textView = new TextView(getApplicationContext());
            textView.setText(c.getString(c.getColumnIndex("LastName")));
            textView.setPadding(20, 20, 20, 20);
            textView.setTextColor(getResources().getColor(R.color.blueactionbar));
            textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,25);
            textView.setTypeface(null, Typeface.BOLD);
            tableRow.addView(textView);
            tableLayout.addView(tableRow);
            c.moveToNext() ;
        }
        db.close();
        rl.requestLayout();

    }
&#13;
&#13;
&#13;

使用parenet相对布局调用第一个setContentView,其中包含具有行的表布局。因此,您尝试将setContentView设置为包含在已将setContentView设置为父级的父视图中的视图。

rl包含sv包含tableLayout。 但您要先将活动视图设置为rl,然后再设置为tableLayout

相反,您应该将内容视图设置为rl。然后像您一样填写tableLayout并刷新父视图上的layout.ie call requestLayout()