How to identify the view identifier for a number of table cells in a linear layout

I am working with a grid of a table built using a custom list with a fixed row of headers.
When a user clicks on one of the fixed header cells, the entire data table is sorted in that particular column.
I developed how to change the text of a clicked header cell by adding an up or down arrow to an existing header / column header (see Code below).
However, I also need to change the text of all the other column header cells in the data table to remove any existing up or down arrows from these other header cells.

The problem I ran into is this: how to find the view ids of these other column header cells at runtime.

Please note that these view identifiers cannot be found by searching for a specific TextView identifier from the xml layout, since all table header cells are created using a common TextView identifier called text1 (see the linear layout code used for the table heading below):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/item_table1_header" android:gravity="center" android:orientation="vertical" > <TextView android:id="@android:id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 

The following is the getView code used to create the table cells, which also includes the built-in OnClickListener. Note that the header line of the fixed table is numbered -1.

I will need to add code to change the text of existing header cells in a section labeled β€œTODO” as soon as I can identify their existing TextView identifiers:

 @Override public View getView(final int row, final int column, View converView, ViewGroup parent) { if (converView == null) { converView = inflater.inflate(getLayoutResource(row, column), parent, false); } setText(converView, getCellString(row, column)); converView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (row == -1) { // clicked on header row cell for(int col=0; col<=getColumnCount(); col++){ if (col == column+1) { if (getColOrder(col) == 0 || getColOrder(col) == 2) { setText(v, getCellString(row, column) + " ↑"); // reset selected column header to ASC order setColOrder(col, 1); } else { setText(v, getCellString(row, column) + " ↓"); // reset selected column header to DESC order setColOrder(col, 2); } } else { setColOrder(col, 0); // reset all other column headers to 0 (not ordered) // TODO: // here we need to change the text of all other header cells, using their view ids } } } } }); return converView; } 
+2
source share
2 answers

I found a solution to the aforementioned problem of assigning, storing and identifying view identifiers in order to update the header cells of the table header. Here is the updated working code with the solution:

 @Override public View getView(final int row, final int column, View converView, ViewGroup parent) { if (converView == null) { converView = inflater.inflate(getLayoutResource(row, column), parent, false); if (row == -1){ converView.setId(column+2); // assign new id to the cell view setHeaderId(column); // store that view id in the header array } } setText(converView, getCellString(row, column)); converView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (row == -1) { // clicked on header row cell for(int col=0; col<=getColumnCount(); col++){ if (col == column+1) { // check if col = clicked column if (getColOrder(col) == 0 || getColOrder(col) == 2) { // 0 = not ordered, 1 = +" ↑" ASC, 2 = +" ↓" DESC setText(v, getCellString(row, column) + " ↑"); // reset selected column header to ASC order setColOrder(col, 1); } else { setText(v, getCellString(row, column) + " ↓"); // reset selected column header to DESC order setColOrder(col, 2); } } else { int viewId = getHeaderId(col); // extract view id from the header array if (viewId > 0) { // to prevent updating header cells with unassigned view Id's ViewGroup parent = (ViewGroup)v.getParent(); // parent = TableFixHeaders View tv = parent.findViewById(viewId); setText(tv, getCellString(row, col-1)); // update the header cell text with the original text string (from header text array) setColOrder(col, 0); // reset all other column headers to 0 (= not ordered) } } } } }); return converView; } 
0
source

I would make your layout my own subclass of View. Keep track of its initial state so that you can reset later. Then add the public reset () function to your new class, which replaces the text with any original value. Finally, now you do not need to access the internal properties of your view from your adapter. Instead, you can simply call reset () on the headers, and they will take care of their internal state.

+1
source

All Articles