在 Android Stud 的 Java 中使 table 行可滚动
Making table row scrollable in Java in Android Stud
我尝试在我的相对布局中创建滚动视图以使我的 table 布局可滚动,但我不断收到此错误:
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.
这是我的 Table 在数据库中显示字段的布局:
@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>
如何解决这个错误?
感谢任何帮助!非常感谢!
@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 是使用包含 table 布局的 parenet 相对布局调用的 rows.Therefore 您正在尝试将 ContentView 设置为包含在您已经拥有的父视图中的视图setContentView 为 ie.
rl
包含 sv
包含 tableLayout
。
但是您首先将 activity 视图设置为 rl
,然后再设置为 tableLayout
。
您应该将内容视图设置为 rl
。然后像您一样填充您的 table 布局,并在父视图上刷新 layout.ie 调用 requestLayout()
。
我尝试在我的相对布局中创建滚动视图以使我的 table 布局可滚动,但我不断收到此错误:
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.
这是我的 Table 在数据库中显示字段的布局:
@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>
如何解决这个错误?
感谢任何帮助!非常感谢!
@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 是使用包含 table 布局的 parenet 相对布局调用的 rows.Therefore 您正在尝试将 ContentView 设置为包含在您已经拥有的父视图中的视图setContentView 为 ie.
rl
包含 sv
包含 tableLayout
。
但是您首先将 activity 视图设置为 rl
,然后再设置为 tableLayout
。
您应该将内容视图设置为 rl
。然后像您一样填充您的 table 布局,并在父视图上刷新 layout.ie 调用 requestLayout()
。