【问题标题】:Making table row scrollable in Java in Android Stud在 Android Stud 中使用 Java 使表格行可滚动
【发布时间】:2015-04-01 08:14:07
【问题描述】:

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

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

这是我的 xml 文件:

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

如何解决此错误?

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

【问题讨论】:

  • 为什么要设置两次内容视图?而您的表格视图是滚动视图中的子视图(这是相对布局的子视图)。当您第二次设置内容视图时也是如此。这也是在活动上完成的。这是一种错误的编码方式
  • 所以我删除了第一个 setcontentview?
  • 是的,您需要刷新整体视图。要做到这一点,一旦你添加了你调用的所有行requestLayout()
  • 你能写代码吗?所以我能更好地理解它吗?我是处理 Android Studio 的新手。
  • 它不是 android studio 问题 :).. 好的将添加代码。

标签: java android tablelayout


【解决方案1】:

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

    }

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

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

相反,您应该将内容视图设置为 rl。然后像您一样填充您的 tableLayout 并刷新布局。即在父视图上调用 requestLayout()

【讨论】:

  • 没有 requestLayout() 函数。代码变为红色。
  • 在你的父视图上调用它,rl.requestLayout()
  • 我忘了说r.layout.students_masterlist是xml文件相对布局r.id.layout。
  • 是的,这很好。也请阅读此内容以了解 ListView 实现。ListView 将是一种更优化、更容易实现的方式,甚至可以实现点击侦听器。 vogella.com/tutorials/AndroidListView/article.html
  • 它有效,谢谢!并且不知道您共享的链接。再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-26
  • 1970-01-01
  • 1970-01-01
  • 2014-06-12
  • 1970-01-01
  • 2019-06-14
相关资源
最近更新 更多