如何搜索在android的表格布局中动态创建的行?我有一个表格布局,其中包含名称和代码作为列,我如何搜索这些行的特定名称/代码?
我正试图以这种方式添加标签:
TextView empFirstName = null;
int i,j;
for (i=0; i<employees.size();i++){
final Employee employee = (Employee) employees.get(i);
int count = 0;
TableRow empData = new TableRow(this);
empData.setId(300+count);
empData.setClickable(true);
/*empData.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT ));*/
//tr.setClickable(true);
final TextView empCode = new TextView(this);
empCode.setId(300+count);
empCode.setText(employee.getCode());
empCode.setTextSize(16);
empCode.setPadding(2,2, 2, 2);
empCode.setLayoutParams(new TableRow.LayoutParams( 0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 2 ));
empData.addView(empCode);
final TextView empName = new TextView(this);
empName.setId(300+count);
empName.setText(employee.getCallName());
empName.setTextSize(16);
empName.setPadding(2, 2, 2, 2);
empName.setLayoutParams(new TableRow.LayoutParams( 0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 4 ));
empData.addView(empName);
empFirstName = new TextView(this);
empFirstName.setId(007);
empFirstName.setText(employee.getFirstName());
empFirstName.setTag(i);
}
我正试图以这种方式检索名字:
for(j=0;j<=i;j++){
TextView names = (TextView) empFirstName.getTag(j);
System.out.println("Name is "+names.getText());
}
但它正在抛出空指针异常!!
答案 0 :(得分:0)
您可以在创建标记时为每个视图设置标记,并在以后需要时比较标记。
答案 1 :(得分:0)
您想要搜索行的行或内容吗?如果是行,则为行设置标记。类似的东西:
row.setTag(SomeTag);
在res / values下定义一个id文件。将其添加到:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="sample_key" />
</resources>
在java文件中。它是这样的:
String sampleKey = "This is a sample key";
textView.setTag(R.id.sample_key, sampleKey);
String returnedString = (String) textView.getTag(R.id.sample_key);