1

我有包含复选框和 2 个文本视图的行

我正在使用此代码重复自定义 xml 布局,它工作正常

但我想知道我的意思是行中每个组件的每个 id(复选框的 id 和编辑 1 和 id 编辑 2)

所以我可以和他们一起工作。

我还需要 for 循环来为 ids 创建这个实例。

为了让我的 Q 清楚,我想要如下:

for ( int i = 0 ; i < 60 ; i++ ){

CheckBox "auto name here " = new (CheckBox) findViewById(R.id."the id of the current item in rep.xml layout or row ");

"auto name here".setChecked(true);

等等……

我在 for 循环中创建行的代码:

for (int i = 0; i<60 ; i++ ){

LayoutInflater  inflater =    (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = (View) inflater.inflate(R.layout.rep, null);

tl.addView(view); // tl is table-layout instance 
}
4

2 回答 2

1

如果您需要添加 60 行,则不应使用循环和 TableLayout。

相反,您应该使用带有 Adapter 的 AdapterView 来保存您的数据并为您增加行数。这样做的众多好处之一是能够声明当每行中的复选框被独立选中时会发生什么。另一个是您将获得更好的性能,因为您不会一次将所有 60 行都放在内存中,因为 convertView 回收。

转到此处: http: //developer.android.com/guide/topics/ui/binding.html以获得对 AdapterViews 的高级介绍。

研究 ListView 和 GridView 的示例。一旦你把它们弄下来,调整一些示例代码来增加你自己的行,而不是给定的示例代码。

于 2013-03-04T21:00:49.907 回答
0

已编辑 已编辑

package com.example.help;

import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    CheckBox[] cb=new CheckBox[60];

    for (int i = 0; i<60 ; i++ ){

    LayoutInflater  inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = (View) inflater.inflate(R.layout.rep, null);

    tl.addView(view); // tl is table-layout instance 
    cb[i]=(CheckBox)view.fidViewById(R.id.CheckBoxid);
    cb[i].setChecked(true);



    }
}
于 2013-03-04T21:00:52.870 回答