Error reading values from dynamically created table rows in Android
- by jaymo
So I have a couple of dynamically created tablerows.Each TableRow has a couple of Textviews.I have a button that on clicked should get all the values of the second and third textviews in each Tablerow in the Table. I have tried to do this using the code below
mSubmitOrders.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(mTable.getChildCount()>1){
for(int i = 0; i < mTable.getChildCount(); i++){
TableRow tr = (TableRow)mTable.getChildAt(i);
TextView code = (TextView) tr.getChildAt(1);
TextView quantity = (TextView) tr.getChildAt(2);
String Scode = code.getText().toString();
Log.i("TAG", Scode);
String Squantity = quantity.getText().toString();
Log.i("TAG", Squantity);
}
}
But I get the below errors (Logcat below). Assistance required
10-22 09:00:16.345: E/AndroidRuntime(4495): FATAL EXCEPTION: main
10-22 09:00:16.345: E/AndroidRuntime(4495): java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
10-22 09:00:16.345: E/AndroidRuntime(4495): at com.symetry.myitprovider.ui.actual$4.onClick(actual.java:173)
10-22 09:00:16.345: E/AndroidRuntime(4495): at android.view.View.performClick(View.java:3131)
10-22 09:00:16.345: E/AndroidRuntime(4495): at android.view.View$PerformClick.run(View.java:12035)
10-22 09:00:16.345: E/AndroidRuntime(4495): at android.os.Handler.handleCallback(Handler.java:587)
10-22 09:00:16.345: E/AndroidRuntime(4495): at android.os.Handler.dispatchMessage(Handler.java:92)
10-22 09:00:16.345: E/AndroidRuntime(4495): at android.os.Looper.loop(Looper.java:132)
10-22 09:00:16.345: E/AndroidRuntime(4495): at android.app.ActivityThread.main(ActivityThread.java:4123)
10-22 09:00:16.345: E/AndroidRuntime(4495): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 09:00:16.345: E/AndroidRuntime(4495): at java.lang.reflect.Method.invoke(Method.java:491)
10-22 09:00:16.345: E/AndroidRuntime(4495): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
10-22 09:00:16.345: E/AndroidRuntime(4495): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
10-22 09:00:16.345: E/AndroidRuntime(4495): at dalvik.system.NativeStart.main(Native Method)
EDIT:
I have modified my code and now there is no error..but its not working as needed..
The new code is as below..
mSubmitOrders.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(mTable.getChildCount()>1){
for(int i = 0; i < mTable.getChildCount(); i++){
//TODO: Figure out how to do this well
LinearLayout LL = (LinearLayout)mTable.getChildAt(i);
View x = ((View)(LL.getParent()).getParent());
Log.i("TAG", "Past view");
TextView code = (TextView)((ViewGroup) x).getChildAt(2);
String Scode = code.getText().toString();
Log.i("TAG", Scode);
}
But thing is its not getting the texview from TableLayout's Table Row..its getting a different one...the one I have circled in the picture
![Error]:http://semasoftltd.com/error.png