How to scroll a table in android

I have a table view that I want to scroll through because the data is not displayed.

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="0,1,2,3,4" android:id="@+id/maintable" > </TableLayout> 

this is my tablelayout, and if I pack it into <ScrollView> </Scrollview> , the application crashes if I open the action. How to do it?

+4
source share
2 answers

You really have to put the code you tried that crashed your application and the reason for your failure. There is no valuable information in your original post.

Uh ... did you try something simple? An example .xml that I use in several places:

 <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TableLayout android:id="@+id/score_table" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TableRow android:id="@+id/header"/> </TableLayout> </ScrollView> 

Works great for me. You do not need to enable TableRow unless you are using it, obviously.

+11
source

Create your lines dynamically Here I put a small example,

main.xml

 <Button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add row"></Button> <ScrollView android:id="@+id/ScrollView01" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TableLayout android:id="@+id/TableLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="0"> <TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/TextView01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="textfield 1-1"></TextView> <CheckBox android:id="@+id/CheckBox01" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> </TableRow> </TableLayout> </ScrollView> 

Activity

 public class tablelayout extends Activity implements OnClickListener { /** Called when the activity is first created. */ //initialize a button and a counter Button btn; int counter = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setup the layout setContentView(R.layout.main); // add a click-listener on the button btn = (Button) findViewById(R.id.Button01); btn.setOnClickListener(this); } // run when the button is clicked public void onClick(View view) { // get a reference for the TableLayout TableLayout table = (TableLayout) findViewById(R.id.TableLayout01); // create a new TableRow TableRow row = new TableRow(this); // count the counter up by one counter++; // create a new TextView TextView t = new TextView(this); // set the text to "text xx" t.setText("text " + counter); // create a CheckBox CheckBox c = new CheckBox(this); // add the TextView and the CheckBox to the new TableRow row.addView(t); row.addView(c); // add the TableRow to the TableLayout table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); } } 

You can create your own table row when needed in code. I assumed that this is a button click.

Hope this helps.

And here is another example .

+2
source

Source: https://habr.com/ru/post/1344769/


All Articles