Let’s have an example like below:
package xliiv.sandbox;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class Double_tablesActivity extends Activity {
static final String TAG = "MAIN";
LinearLayout lay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout lay = new LinearLayout(this);
this.lay = lay;
for(int i=0; i<2; i++) {
boolean heading = (i == 0) ? true: false;
add_table(heading, "" + i);
}
this.lay.setOrientation(1);
setContentView(lay);
}
public void add_table(boolean heading, String text) {
TableLayout t = new TableLayout(this);
TableRow tr = new TableRow(this);
TextView tv = new TextView(this);
tv.setText(text);
if (heading == true) {
tv.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
TextView tv = (TextView) v;
Log.i(TAG, "onclick: " + tv.getText());
//TODO: sort 2nd table denpends on which col of first table was clicked
//??
}
});
}
tr.addView(tv);
t.addView(tr);
lay.addView(t);
}
}
I’d like to sort 2nd table depends on which col was clicked in the 1st table?
Suppose there is a method table.sort(col)..
I found getRootView(), so i could use it and go down to second table but there must be better way, than manually searching..
UPDATE
After midoalageb’s answer here is working version of upper code:
package xliiv.sandbox;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class Double_tablesActivity extends Activity {
static final String TAG = "MAIN";
LinearLayout lay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout lay = new LinearLayout(this);
this.lay = lay;
for(int i=0; i<2; i++) {
boolean heading = (i == 0) ? true: false;
add_table(heading, "" + i);
flipper.setTag("tag" + i);
}
this.lay.setOrientation(1);
setContentView(lay);
}
public void add_table(boolean heading, String text) {
TableLayout t = new TableLayout(this);
TableRow tr = new TableRow(this);
TextView tv = new TextView(this);
tv.setText(text);
if (heading == true) {
tv.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
View root = (View) getRootView();
View view_by_tag = (View) root.findViewWithTag("tag2");
Log.i(TAG, "found table: " + flipper);
}
});
}
tr.addView(tv);
t.addView(tr);
lay.addView(t);
}
}
Since you fill the table from a database, a better implementation is to replace your 2nd table by a
ListViewand use aSimpleCursorAdapterto fill it.Each time you press a key in the first table, requery the
Cursorof theSimpleCursorAdapterusing the relevantORDER BYSQL query.Update: If you do not want to change your layout, you can use
View.SetTagto give a unique Tag for your column when you fill it with data, then in theOnClickListenerof the first Table, useView.FindViewWithTagto find the column of the second table and then use your sort method to sort this table.