从Android中动态创建的表行读取值时出错

时间:2012-10-22 06:15:24

标签: android tablerow

所以我有几个动态创建的tablerows.Each TableRow有几个Textviews。我有一个按钮,点击后应该获得表中每个Tablerow中的第二和第三个textview的所有值。我试图使用下面的代码

来做到这一点
          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);


            }   
            }

但我得到以下错误(下面的Logcat)。需要帮助

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)

解决: 在获得@zacck的帮助后,我终于解决了这个问题:

                 for(int i = 0; i < mTable.getChildCount(); i++){
                    TableRow tr = (TableRow) mTable.getChildAt(i);

                    LinearLayout l1 = (LinearLayout)tr.getChildAt(1);
                    LinearLayout l2 = (LinearLayout)tr.getChildAt(2);



                    TextView code = (TextView)l1.getChildAt(0);
                    sCode = code.getText().toString();
                    Log.v("Inserted---->", sCode);
                    codeA[i]=sCode;


                    TextView quantity = (TextView)l2.getChildAt(0);
                    sQuantity = quantity.getText().toString();
                    Log.v("Inserted--->", sQuantity);
                    quantityA[i]=sQuantity;

            }

1 个答案:

答案 0 :(得分:0)

之前你必须用setTag(position)标记行的各个视图,否则你可以使用任何唯一值作为参数。然后,您可以通过引用getTag()获取视图内容。