Clearing TableLayout inside LinearLayout removes TextView Header - android

I have a LinearLayout with a TextView and TableLayout embedded in it. here is the format of my xml file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/txtHeader"
android:layout_width="match_parent"
android:text="Header" />
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<!-- Column 1 -->
<TextView
android:id="#+id/row1Item1"
android:text="Column1" />
<!-- Column 2 -->
<TextView
android:id="#+id/row1Item2"
android:text="Column2" />
</TableRow>
</TableLayout>
</LinearLayout>
I am adding rows dynamically to the table. Everytime, I get an update, I need to clear the table and write new information (number of rows are variables). So, I am trying to do clear the table as a first step whenever I have to update.
ll = (LinearLayout)findViewById(R.id.wlayout);
tb = (TableLayout)findViewById(R.id.wtable);
private void on Update(){
tb.removeAllViewsInLayout();
Create table dynamically..
}
But it is removing my text header also. I have also tried ll.removeView(tb) where ll is the LinearLayout. Can any one please help me. I am new to Android. Thank you.

You have not given id to TableLayout tag.Please give id and then try to clear it.
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="+#id/wtable"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<!-- Column 1 -->
<TextView
android:id="#+id/row1Item1"
android:text="Column1" />
<!-- Column 2 -->
<TextView
android:id="#+id/row1Item2"
android:text="Column2" />
</TableRow>
</TableLayout>
And then try this..
tb = (TableLayout)findViewById(R.id.wtable);
tb.removeAllViewsInLayout();

Related

Populating a table with an array of data

I want to set up a table which has a fixed amount of columns, and an X amount of rows with The first row listing the contents of each column. For example, one column will be 'Name' and a second column will be 'Age', then there will be an X amount of rows which store the data. Is there any way that I can set up arrays for this data elsewhere, and automatically create/fill the rows of the table with this data. I've done this previously with a more simple example using a Custom Adapter but I'm not quite sure how to go about this with a table involved. I'm quite stuck and any help would be greatly appreciated.
I think inside the table layout create the tablerow as below
<TableLayout
---
>
<TableRow
--
>
<Textview
for name
/>
<Textview
...for age
/>
</TableRow>
<TableRow
--
>
<Listview
for name
/>
<Listview
...for age
/>
</TableRow>
</TableLayout>
and populate the listview with a simple arrayadapter with your fixed data.
this's simple if you have textviews only in your listview you can use
ArrayAdapter ad=new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item1,namearray); list1.setAdapter(ad);
ArrayAdapter ad=new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item1,agearray); list2.setAdapter(ad);
A ListView basically acts as a row within any table of data. You should create a POJO with all the attributes you want to display in a row. You can create create a custom xml layout for the data, which would probably by a horizontal LinearLayout that corresponds to your columns.
POJO
public class MyData {
public String Name;
public int Age;
// More data here
}
ListView item layout (layout_list_item.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/Name"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="0.7" />
<TextView
android:id="#+id/Age"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="0.3" />
</LinearLayout>
Main Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="0.7"
android:text="Name" />
<TextView
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="0.3"
android:text="Age" />
</LinearLayout>
<ListView
android:id="+#id/MyDataListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
You then need a custom adapter which sets the fields in the list view with the values from your POJO. There are plenty of tutorials on the internet for this.

Display ResultSet in Table View

I really need help
I have a ResultSet as a result of select query from MySQL DB. how can I display it in table layout?? I have setup a layout for tableview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<EditText
android:id="#+id/myFilter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/some_hint" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:padding="10dp"
android:text="#string/some_text" android:textSize="20sp" />
<TableLayout
android:id="#+id/producttablelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dividerPadding="#dimen/activity_horizontal_margin"
android:scrollbars="horizontal|vertical"
android:showDividers="none|beginning|middle|end" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top|bottom|left|right"
android:paddingTop="#dimen/activity_horizontal_margin" >
<TextView
android:id="#+id/ttt1"
android:text="Column 1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/ttt2"
android:text="Column 12"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
</TableLayout>
</LinearLayout>
I want to be able to add ResultSet data into it. Something like:
super.onCreate(savedInstanceState);
String s = null;
setContentView(R.layout.activity_list_all_products);
// Show the Up button in the action bar.
setupActionBar();
context333=this;
TableLayout myTable = (TableLayout)findViewById(R.id.producttablelayout);
try
{
connect2 = DriverManager.getConnection(LogonActivity.url, LogonActivity.user, LogonActivity.password);
statement2 = connect2.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
preparedStatement2 = connect2.prepareStatement("select article_code,article_desc from products limit 4");
resultSet2=preparedStatement2.executeQuery();
resultSet2.beforeFirst();
while (resultSet2.next()) {
how to add next record here to the table layout??
}
Please help
You can create a new Views (it seems you are populating your table rows with TextViews) and populate them with the results from your query as you wish. Then you can create a new TableRow with a constructor (see the documentation).
Add the views to to table row instnace using:
tableRowInstance.addView(viewInstance);
and then add the tableRow to the tableLayout the same way:
myTable.addView(tableRowInstance)
TableLayouts and TableRows are both subclasses of View. You should do some research on ViewGroup, View, and how they are structured. This link provides nice visualizations.

Programmatically replace cell of TableLayout (with complex layout)

In my app I have a table with somewhat complex layout: it is a 3x3 table, with the top and left being static headers, and the other four cells each containing a table again with my information.
This internal table has two options, and I programmatically select which of the two to use, and add them to the table. So far so good.
Now when updating the information in the table, a second table is added to the cell, instead of the original one being replaced. So after a few updates, my main table becomes huge, and the cells in it really high with a number of internal tables. Of course that's not the idea.
private int setTable(int minAPI, int maxAPI, TableLayout tableId) {
// Get the levels that belong to the API.
String minLevel = getLevel(minAPI);
String maxLevel = getLevel(maxAPI);
// Get an inflater to inflate the table layouts.
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Get the layout of the API/Level table.
// Which one to use for the levels depends on whether the levels are
// the same or not.
TextView textLevelFrom = null;
TextView textLevelTo = null;
TableLayout tableLayout = null;
if (minLevel.equals(maxLevel)) {
Log.v(TAG, "Setting up api_table1.");
tableLayout = (TableLayout) inflater.inflate(R.layout.api_table1, tableId);
textLevelFrom = (TextView) tableLayout.findViewById(R.id.textLevel);
textLevelFrom.setText(minLevel);
textLevelFrom.setTextColor(getColour(minAPI));
}
else {
Log.v(TAG, "Setting up api_table2.");
tableLayout = (TableLayout) inflater.inflate(R.layout.api_table2, tableId);
textLevelFrom = (TextView) tableLayout.findViewById(R.id.textLevelFrom);
textLevelFrom.setText(minLevel);
textLevelFrom.setTextColor(getColour(minAPI));
textLevelTo = (TextView) tableLayout.findViewById(R.id.textLevelTo);
textLevelTo.setText(maxLevel);
textLevelTo.setTextColor(getColour(maxAPI));
}
Log.v(TAG, "Populating the table.");
TextView textAPIFrom = (TextView) tableLayout.findViewById(R.id.textAPIFrom);
textAPIFrom.setText(""+minAPI);
textAPIFrom.setTextColor(getColour(minAPI));
TextView textAPITo = (TextView) tableLayout.findViewById(R.id.textAPITo);
textAPITo.setText(""+maxAPI);
textAPITo.setTextColor(getColour(maxAPI));
textLevelFrom.setText(minLevel);
textLevelFrom.setTextColor(getColour(minAPI));
return tableLayout.getId();
}
The TableLayout TableId is previously found by a call like
tableCurrentGeneral = (TableLayout) findViewById(R.id.CurrentGeneral);
one for each of the four cells. The main table is defined in main.xml; the cells of these table are an empty TableLayout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.squirrel.hkairpollution.MySupportMapFragment"
/>
<!-- class="com.google.android.gms.maps.SupportMapFragment" -->
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tableAPI"
android:background="#color/translucent_white"
android:layout_alignBottom="#id/map">
<!-- First row: headers. -->
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/general"
android:textStyle="bold"
android:textColor="#color/black"
android:gravity="center"
android:paddingRight="8dip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/roadside"
android:textStyle="bold"
android:textColor="#color/black"
android:gravity="center" />
</TableRow>
<!-- Second row: current API -->
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/current"
android:textStyle="bold"
android:textColor="#color/black"
android:paddingRight="5dip"
android:gravity="center" />
<!-- Current API, general stations -->
<TableLayout
android:id="#+id/CurrentGeneral"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingRight="8dip" />
<TableLayout
android:id="#+id/CurrentRoadside"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</TableRow>
<!-- Third row: forecast API -->
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/forecast"
android:textStyle="bold"
android:textColor="#color/black"
android:paddingRight="5dip"
android:gravity="center" />
<!-- Forecast API, general stations -->
<TableLayout
android:id="#+id/ForecastGeneral"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingRight="8dip" />
<TableLayout
android:id="#+id/ForecastRoadside"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
<ProgressBar
android:id="#+id/progressBar1"
android:visibility="invisible"
android:layout_width="48dip"
android:layout_height="48dip"
android:layout_alignTop="#id/map"
android:layout_alignLeft="#id/map"/>
<ImageView
android:id="#+id/refresh"
android:layout_width="48dip"
android:layout_height="48dip"
android:contentDescription="#string/refresh"
android:layout_alignTop="#id/map"
android:layout_alignLeft="#id/map"
android:src="#drawable/ic_menu_refresh" />
</RelativeLayout>
The line
tableLayout = (TableLayout) inflater.inflate(R.layout.api_table1, tableId);
correctly links the api_table1 resp. api_table2 layouts to the desired cells, but all the time this layout is added, it's not being replaced.
I unsuccessfully tried handing back the id of the just added view, to remove it later:
tableAPI.removeView(findViewById(tableCurrentRoadsideId));
How can this be done?
Finally solved it through a slightly different route.
I wrongly believed that when you attach a view to a cell again, it is a second view that is attached to it. Instead, my table was appending more rows to itself.
And also so much for no documentation on the actual workings of TableLayout.removeView()
The solution: at the start of setTable a line that removes all the views from that table. That clears up this table, after which I can re-attach my layout, and all is as it should be.
private void setTable(int minAPI, int maxAPI, TableLayout tableId) {
tableId.removeAllViews();
...

ScrollView doesn't display whole TableLayout

I want to display a TableLayout within a ScrollView. The Table has 9 TableRows but the ScrollView starts displaying parts of the 4th row and I can't sroll up to see the rows above.
The XML-Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/uyi_choosepi_relativelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:keepScreenOn="true" >
<MyGallery
android:id="#+id/uyi_gallery_originals"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<TableLayout
android:id="#+id/uyi_choosepi_buttontable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:stretchColumns="0,1" >
<TableRow >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hilfe"
android:onClick="showHelp"
android:drawableLeft="#android:drawable/ic_menu_help" />
<Button
android:id="#+id/uyi_choosepi_savepw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fertig"
android:clickable="false"
android:enabled="false"
android:onClick="savePassword"
android:drawableLeft="#android:drawable/ic_menu_save"/>
</TableRow>
</TableLayout>
<ScrollView
android:id="#+id/uyi_choosepi_scrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/uyi_gallery_originals"
android:layout_above="#id/uyi_choosepi_buttontable">
<TableLayout
android:id="#+id/uyi_choosepi_tablelayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:layout_gravity="center_vertical"
android:shrinkColumns="0,1,2" />
</ScrollView>
</RelativeLayout>
The TableRows in the uyi_choosepi_tablelayout where added programmatically as follows:
table = (TableLayout) findViewById(R.id.uyi_choosepi_tablelayout);
for (int i = 0; i < UYIMainActivity.PASS_LENGTH; i++) {
TableRow row = new TableRow(table.getContext());
originalViews[i] = new ImageView(row.getContext());
originalViews[i].setAdjustViewBounds(true);
originalViews[i].setScaleType(ScaleType.FIT_XY);
originalViews[i].setPadding(3, 3, 3, 3);
originalViews[i].setImageResource(R.drawable.oempty);
distortedViews[i] = new ImageView(row.getContext());
distortedViews[i].setAdjustViewBounds(true);
distortedViews[i].setScaleType(ScaleType.FIT_XY);
distortedViews[i].setPadding(3, 3, 3, 3);
distortedViews[i].setImageResource(R.drawable.wempty);
ImageView arrow = new ImageView(row.getContext());
arrow.setAdjustViewBounds(true);
arrow.setScaleType(ScaleType.FIT_XY);
arrow.setMaxWidth(80);
arrow.setPadding(3, 3, 3, 3);
arrow.setImageResource(R.drawable.arrow_active);
row.addView(originalViews[i]);
row.addView(arrow);
row.addView(distortedViews[i]);
table.addView(row);
}
I hope you can give me a hint!
Try using a GridView instead that may get you further towards your goal. I saw [this blog post][1] recently, and I would have approached it more along these lines.
Edit: I meant to paste this tutorial sorry, http://android-coding.blogspot.com/2011/09/custom-gridview-ii-with-imageview-and.html
I solved my problem by surrounding the TableLayout with a LinearLayout
<ScrollView
android:id="#+id/uyi_choosepi_scrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/uyi_gallery_originals"
android:layout_above="#id/uyi_choosepi_buttontable">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout
android:id="#+id/uyi_choosepi_tablelayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:shrinkColumns="0,1,2" />
</LinearLayout>
</ScrollView>

how to make columns in table layout spread evenly making maximum use of space available

I was just trying out with table layout to display some data....
The data is a 3 column data and i want that the columns should utilize the whole width available. But it seems that the layout XML code which i had used is just wrapping up the columns according to the content.
Layout XML code
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow
android:layout_width="fill_parent">
<TextView
android:padding="3dip"
android:gravity="left"
android:text="Name"
/>
<TextView
android:padding="3dip"
android:gravity="left"
android:text="Address"
/>
<TextView
android:padding="3dip"
android:gravity="left"
android:text="Age"
/>
</TableRow>
</TableLayout>
You can try adding android:stretchColumns="0,1,2" to your <TableLayout> element.
android:stretchColomns="0,1" is a good option. Also you can try using android:layout_span = "2" This will stretch the columns together depending on your value(Here it will span two columns together)

Categories

Resources