简体   繁体   中英

Making table row scrollable in Java in Android Stud

I tried creating a scroll view in my relative layout to make my table layout scrollable but i keep getting this error:

 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. 

This is my Table Layout that display's fields in a database:

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

This is my xml file:

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

How do I fix this error?

Any help is appreciated! Thank you so much!

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

The First setContentView is called with the parenet relative layout which contains the table layout that has your rows.Therefore you are trying to setContentView to a view which is contained in the parent that you have already setContentView to ie.

rl contains sv contains tableLayout . but you are setting the activity view first to rl then to tableLayout .

Instead you should set the content view to rl .Then fill your tableLayout like you did and refresh the layout.ie call requestLayout() on the parent view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM