android - 如何从其他 View 的事件访问 View ( View 在布局层次结构中的不同分支中)?

标签 android sorting onclick android-activity

让我们举个例子:

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);

    }
}

我想根据在第一个表中单击的列对第二个表进行排序? 假设有一个方法 table.sort(col).. 我找到了 getRootView(),所以我可以使用它并转到第二个表,但必须有比手动搜索更好的方法..

更新

在 midoalageb 的回答之后,这里是上层代码的工作版本:

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);

    }
}

最佳答案

由于您从数据库中填充表格,更好的实现方式是用 ListView 替换第二个表格并使用 SimpleCursorAdapter 来填充它。

SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to)

每次在第一个表中按下一个键时,使用相关的 ORDER BY SQL 查询重新查询 SimpleCursorAdapterCursor

更新:如果您不想更改布局,可以使用 View.SetTag 在填充数据时为您的列提供一个唯一的标签,然后在第一个表的 OnClickListener 中,使用 View.FindViewWithTag 找到第二个表的列,然后使用您的排序方法对该表进行排序。

关于android - 如何从其他 View 的事件访问 View ( View 在布局层次结构中的不同分支中)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11281598/

相关文章:

android - Proguard 损坏可绘制文件

android - 是否可以在我自己的应用程序中使用私有(private)三星框架?

java - 从 Activity 到抽屉导航的 Intent 问题

Linux整行排序无法正确排序

asp.net ListView 使用 DataBind 排序

javascript - 简单的if else onclick then呢?

JavaScript 每次点击时增加++ 不透明度

java - ViewPager 和 fragment : Why do I always see the same thing?

arrays - 使用嵌套元组对数组进行排序

android - 在撰写中 onClick 后禁用查看