0

所以我有几个动态创建的tablerows。每个TableRow 都有几个Textviews。我有一个按钮,单击该按钮应该获取表中每个Tablerow 中第二个和第三个textviews 的所有值。我尝试使用下面的代码来做到这一点

          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)

已解决:从@zackk 获得帮助后,我最终解决了这个问题:

                 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;

            }
4

1 回答 1

0

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

于 2012-10-22T06:22:10.767 回答