Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java TableLayout中表行的索引_Java_Android_Android Layout - Fatal编程技术网

Java TableLayout中表行的索引

Java TableLayout中表行的索引,java,android,android-layout,Java,Android,Android Layout,我有一个TableLayout和一些未知数量的TableRows,它们是根据数据库中的内容生成的 每一行都有一个OnClick侦听器,但是,一旦单击发生,我就无法(有意义地)分辨它来自哪一行。是否有一种方法可以获取与TableLayout相关的tablerow索引,或者我应该尝试其他方法 一些代码: private void buildBudgetTable() { NumberFormat currencyFormatter = DecimalFormat.getCurrencyIns

我有一个TableLayout和一些未知数量的TableRows,它们是根据数据库中的内容生成的

每一行都有一个OnClick侦听器,但是,一旦单击发生,我就无法(有意义地)分辨它来自哪一行。是否有一种方法可以获取与TableLayout相关的tablerow索引,或者我应该尝试其他方法

一些代码:

private void buildBudgetTable() {
    NumberFormat currencyFormatter = DecimalFormat.getCurrencyInstance();

    TableLayout budgetTable = (TableLayout) findViewById(R.id.budgetTable);
    budgetTable.removeAllViews();

    try {
        this.budgetItems = Budget.GetEntries(this);
    } catch (SQLException e) {
        Toast.makeText(getApplicationContext(), "Could not load budget", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
        finish();
    }

    for(BudgetEntryItem entry : budgetItems){
        TableRow tr = new TableRow(this);
        tr.setPadding(1, 2, 1, 2);

        TextView txtLeft = new TextView(this);
        txtLeft.setText(entry.Memo);
        txtLeft.setGravity(Gravity.LEFT);
        txtLeft.setPadding(3,3,3,3);

        TextView txtRight = new TextView(this);
        txtRight.setText(currencyFormatter.format(entry.Amount));
        txtRight.setGravity(Gravity.RIGHT);
        txtRight.setPadding(3,3,3,3);

        if(entry.Amount > 0){
            txtRight.setBackgroundColor(Color.rgb(0x33, 0xEE, 0x33));
            txtLeft.setBackgroundColor(Color.rgb(0x33, 0xEE, 0x33));
        }else{
            txtRight.setBackgroundColor(Color.rgb(0xEE, 0x33, 0x33));
            txtLeft.setBackgroundColor(Color.rgb(0xEE, 0x33, 0x33));
        }

        tr.addView(txtLeft);
        tr.addView(txtRight);
        tr.setOnClickListener(this);

        budgetTable.addView(tr);
    }
}

使用TableRow的标记:

BudgetEntryItem entry = null;
for(int i = 0; i < budgetItems; i++){
    entry = budgetItems[i]; // or budgetItems.get(i), etc
    TableRow tr = new TableRow(this);

    // Set the index as the tag
    tr.setTag(i); // or entry.setTag(entry.index()), etc

    // ...
}
BudgetEntryItem条目=null;
对于(int i=0;i
现在要获取索引,只需在onClick中将其转换回int,它为您提供了原始视图

旁白
有些人不赞成以这种方式使用标记,因为它将数据层与UI层耦合在一起。另一种方法是使用字典或字典等间接访问项目,而不将UI元素绑定到数据对象。

试试这个

row = new TableRow(this);
    row.setId(i);

    row.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast toast = Toast.makeText(getApplicationContext(), "Tabele row clicked "+ Integer.toString(v.getId()) , Toast.LENGTH_LONG);
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
            toast.show();

        }
    });

同样的问题,这是解决方案:

row.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Current Row Index
                int rowIndex = tableLayout.indexOfChild(v);
                // Do what you need to do.
            }
        });
在我的例子中,我将侦听器放在TableRow中的TextView上

textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // The row the view is in
                TableRow row = (TableRow) v.getParent();
                // It's index
                int index = tableLayout.indexOfChild(row);
            }
        });

变量混淆了,条目不是表行。我刚刚进入了条目中作为标签。您应该阅读,并考虑将标签设置到索引。这将减少对数据对象的错误引用,而您可能希望在以后对这些数据对象进行垃圾收集,总体而言,这是更好的编程实践。这是迄今为止提出的最可靠的解决方案。极好的解决方案。不需要复杂的对象调用。使用船上的方法。@Ullauri,它的工作方式就像魅力和快乐一样向上投票。:)谢谢,这正是我想要的