我正在使用eclipse IDE开发一个android应用程序,其中一部分通过远程网关读取数据,然后信息以表格的形式呈现——使用TableLayout和TableRow,在Java中动态地。我希望能够显示数据,然后选择一个元素,并在此基础上执行一些其他操作。
数据的提取以及屏幕和列表的生成工作正常。我已将其设置为可点击,并设置了适当的 onClickListener。我能够在 onClickListener 中进入 onClick 方法(通过使用 v.getId() 输出证明)-(在 Eclipse IDE 的 LogCat 上看到)。但是,当尝试在 onClick 方法中 v.getParent 时,Eclipse 模拟器会失败。VM 关闭后显示的错误消息显示:
java.lang.ClassCastException:无法将 android.widget.TableLayout 强制转换为 android.widget.TableRow。
我花了几天时间试图找出问题,但显然不明白我有什么问题,所以正在寻求帮助。无论这是朝着正确方向轻推,还是专门告诉我出了什么问题,任何帮助都将不胜感激。
最终,我想提取 TableRow (节点)中的一个字段 - 这是一个唯一的整数地址,并将其用于进一步处理。
Java代码是:
public void readNodeKeys(int[] value) {
setContentView(R.layout.testrz_nodes);
TabHost node_host = (TabHost) findViewById(R.id.tabhost);
node_host.setup();
TabSpec nodeTab = node_host.newTabSpec("node_tabs");
nodeTab.setIndicator(getResources().getString(R.string.node_information),
getResources().getDrawable(android.R.drawable.star_on));
nodeTab.setContent(R.id.node_ScrollView2);
node_host.addTab(nodeTab);
node_host.setCurrentTabByTag("node_tabs");
TableLayout list_table = (TableLayout) findViewById(R.id.node_TableLayout2);
initializeHeaderRow(list_table);
try {
processScores(list_table, value);
} catch (Exception e) {
Log.e(DEBUG_TAG, "Failed to load list status data", e);
}
}
public void initializeHeaderRow(TableLayout statusTable) {
TableRow headerRow = new TableRow(this);
int textColor = getResources().getColor(R.color.testrz_logo_color);
float textSize = getResources().getDimension(R.dimen.testrz_nodelist_text_size_6);
addTextToRowWithValues(headerRow, getResources().getString(R.string.testrz_nodelist_node), textColor, textSize);
addTextToRowWithValues(headerRow, getResources().getString(R.string.testrz_nodelist_data_index), textColor, textSize);
addTextToRowWithValues(headerRow, getResources().getString(R.string.testrz_nodelist_zone_A), textColor, textSize);
addTextToRowWithValues(headerRow, getResources().getString(R.string.testrz_nodelist_zone_B), textColor, textSize);
addTextToRowWithValues(headerRow, getResources().getString(R.string.testrz_nodelist_zone_C), textColor, textSize);
addTextToRowWithValues(headerRow, getResources().getString(R.string.testrz_nodelist_module_type), textColor, textSize);
statusTable.addView(headerRow);
}
public void addTextToRowWithValues(final TableRow tableRow, String text, int textColor, float textSize) {
TextView textView = new TextView(this);
textView.setTextSize(textSize);
textView.setTextColor(textColor);
textView.setText(text);
tableRow.setClickable(true);
tableRow.setOnClickListener(tableRowOnClickListener);
tableRow.addView(textView);
}
public OnClickListener tableRowOnClickListener = new OnClickListener () {
public void onClick(View v) {
System.out.println("Row Clicked : " + v.getId());
TableRow tr = (TableRow)v.getParent();
System.out.println("We've got to the TableRow tr");
TextView tv = (TextView) tr.getChildAt(index);
System.out.println("We've got to the TableView tv");
String result = tv.getText().toString();
System.out.println("On Click string is : " + result);
}
};
public void processScores(final TableLayout statusTable, int[] value) throws
IOException {
int counter = 0;
System.out.println("mtestrz_Node_Count : " + Integer.toHexString(mtestrz_Node_Count));
for (counter = 0; counter < mtestrz_Node_Count; counter++) {
testrz_Node_Key_Values RNKV = new testrz_Node_Key_Values();
offset = 0x10;
while (value[(counter * offset)+18] != 0xff) {
int eventType = -1;
boolean bFoundScores = false;
// Find list
nodelist[mtestrz_Node_Count][0] = value[(counter * offset)+18];
nodelist[mtestrz_Node_Count][1] = value[(counter * offset)+19];
nodelist[mtestrz_Node_Count][2] = value[(counter * offset)+20];
nodelist[mtestrz_Node_Count][3] = value[(counter * offset)+21];
nodelist[mtestrz_Node_Count][4] = value[(counter * offset)+22];
nodelist[mtestrz_Node_Count][5] = value[(counter * offset)+23];
nodelist[mtestrz_Node_Count][6] = value[(counter * offset)+24];
nodelist[mtestrz_Node_Count][7] = value[(counter * offset)+25];
nodelist[mtestrz_Node_Count][8] = value[(counter * offset)+26];
nodelist[mtestrz_Node_Count][9] = value[(counter * offset)+27];
nodelist[mtestrz_Node_Count][10] = value[(counter * offset)+28];
nodelist[mtestrz_Node_Count][11] = value[(counter * offset)+29];
nodelist[mtestrz_Node_Count][12] = value[(counter * offset)+30];
nodelist[mtestrz_Node_Count][13] = value[(counter * offset)+31];
nodelist[mtestrz_Node_Count][14] = value[(counter * offset)+32];
nodelist[mtestrz_Node_Count][15] = value[(counter * offset)+33];
RNKV.Node_add = nodelist[mtestrz_Node_Count][1] * 0x100 + nodelist[mtestrz_Node_Count][0];
RNKV.Code_Control = nodelist[mtestrz_Node_Count][3] * 0x100 + nodelist[mtestrz_Node_Count][2];
RNKV.Data_index = nodelist[mtestrz_Node_Count][5] * 0x100 + nodelist[mtestrz_Node_Count][4];
RNKV.Zone_A = nodelist[mtestrz_Node_Count][7] * 0x100 + nodelist[mtestrz_Node_Count][6];
RNKV.Zone_B = nodelist[mtestrz_Node_Count][9] * 0x100 + nodelist[mtestrz_Node_Count][8];
RNKV.Zone_C = nodelist[mtestrz_Node_Count][11] * 0x100 + nodelist[mtestrz_Node_Count][10];
RNKV.Module_type = nodelist[mtestrz_Node_Count][12];
Module_Type module_str;
module_str = (module_type_str[RNKV.Module_type]);
String module_string;
module_string = module_str.toString();
bFoundScores = true;
String node = Integer.toHexString(RNKV.Node_add);
String data_index = Integer.toHexString(RNKV.Data_index);
String Zone_A = Integer.toHexString(RNKV.Zone_A);
String Zone_B = Integer.toHexString(RNKV.Zone_B);
String Zone_C = Integer.toHexString(RNKV.Zone_C);
String module_type = module_string;
insertStatusRow(statusTable, node, data_index, Zone_A, Zone_B, Zone_C, module_type);
// Handle no scores available
if (bFoundScores == false) {
final TableRow newRow = new TableRow(this);
TextView noResults = new TextView(this);
noResults.setText(getResources().getString(R.string.testrz_no_data));
newRow.addView(noResults);
statusTable.addView(newRow);
}
counter = counter + 1;
}
}
}
public void insertStatusRow(final TableLayout statusTable, String node, String data_index, String Zone_A, String Zone_B, String Zone_C, String module_type) {
final TableRow newRow = new TableRow(this);
int textColor = getResources().getColor(R.color.testrz_title_color);
float textSize = getResources().getDimension(R.dimen.testrz_nodelist_text_size_6);
addTextToRowWithValues(newRow, node, textColor, textSize);
addTextToRowWithValues(newRow, data_index, textColor, textSize);
addTextToRowWithValues(newRow, Zone_A, textColor, textSize);
addTextToRowWithValues(newRow, Zone_B, textColor, textSize);
addTextToRowWithValues(newRow, Zone_C, textColor, textSize);
addTextToRowWithValues(newRow, module_type, textColor, textSize);
statusTable.addView(newRow);
}
XML代码是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/rayzig_nodes_RelativeLayout02"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/rayzig_nodes_Textview02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/nodes" />
<DigitalClock
android:id="@+id/rayzig_nodes_digitalClock2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_below="@+id/rayzig_nodes_Textview02"
android:layout_marginTop="17dp"
android:text="@string/clock" />
</RelativeLayout>
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="@+id/node_ScrollView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:id="@+id/node_TableLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*" >
</TableLayout>
</ScrollView>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>