如何在android中的表行中获取选定的值

时间:2015-06-25 13:00:51

标签: android selection tablelayout

我有表格布局有11个表行和复选框每个表格行我想要选中复选框行值并显示在一个Toast消息上长按所选行我已经完成了这个打击但它不会工作请帮助我这个

public class Manual_AC_Fuse_ckt extends Fragment {

    public int []check_box_count = new int[11];
    public int [] Table_Row_Count = new int[11];
    TableRow table_row;
    TableLayout table;
    CheckBox check_box;
    int i;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View Root_view = inflater.inflate(R.layout.manualmode_ac_fuse_ckt, null);

        Table_Row_Count[0] = R.id.tableRow1_manual;
        Table_Row_Count[1] = R.id.tableRow2_manual;
        Table_Row_Count[2] = R.id.tableRow3_manual;
        Table_Row_Count[3] = R.id.tableRow4_manual;
        Table_Row_Count[4] = R.id.tableRow5_manual;
        Table_Row_Count[5] = R.id.tableRow6_manual;
        Table_Row_Count[6] = R.id.tableRow7_manual;
        Table_Row_Count[7] = R.id.tableRow8_manual;
        Table_Row_Count[8] = R.id.tableRow9_manual;
        Table_Row_Count[9] = R.id.tableRow10_manual;
        Table_Row_Count[10] = R.id.tableRow11_manual;

        table_row = (TableRow)Root_view.findViewById(Table_Row_Count[i]);

        check_box_count[0] = R.id.cb_1;
        check_box_count[1] = R.id.cb_2;
        check_box_count[2] = R.id.cb_3;
        check_box_count[3] = R.id.cb_3_1;
        check_box_count[4] = R.id.cb_4;
        check_box_count[5] = R.id.cb_5;
        check_box_count[6] = R.id.cb_6;
        check_box_count[7] = R.id.cb_7;
        check_box_count[8] = R.id.cb_8;
        check_box_count[9] = R.id.cb_9;
        check_box_count[10] = R.id.cb_10;
        check_box = (CheckBox)Root_view.findViewById(check_box_count[i]);






        registerForContextMenu(table_row);
        table_row.setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                for(int i=1;i<11;i++){
                if(check_box.isChecked()==true){
                    int count = table_row.getChildCount();
                    String[] str = new String[count];
                    for(int j=0;j<count;j++){
                        TextView tv = (TextView)(((TableRow)table_row)).getChildAt(i);
                        str[j] = tv.getText().toString();
                    }
                    Toast.makeText(getActivity(), Arrays.toString(str), Toast.LENGTH_LONG).show();
                }
                }
                return true;
            }
        });
        return Root_view;


    }

这是我的Logcat:

01-02 08:24:15.150: E/AndroidRuntime(7283): FATAL EXCEPTION: main
01-02 08:24:15.150: E/AndroidRuntime(7283): java.lang.NullPointerException
01-02 08:24:15.150: E/AndroidRuntime(7283):     at com.developer.milanandroid.Manual_AC_Fuse_ckt$1.onLongClick(Manual_AC_Fuse_ckt.java:78)
01-02 08:24:15.150: E/AndroidRuntime(7283):     at android.view.View.performLongClick(View.java:4276)
01-02 08:24:15.150: E/AndroidRuntime(7283):     at android.view.View$CheckForLongPress.run(View.java:17700)
01-02 08:24:15.150: E/AndroidRuntime(7283):     at android.os.Handler.handleCallback(Handler.java:730)
01-02 08:24:15.150: E/AndroidRuntime(7283):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-02 08:24:15.150: E/AndroidRuntime(7283):     at android.os.Looper.loop(Looper.java:137)
01-02 08:24:15.150: E/AndroidRuntime(7283):     at android.app.ActivityThread.main(ActivityThread.java:5103)
01-02 08:24:15.150: E/AndroidRuntime(7283):     at java.lang.reflect.Method.invokeNative(Native Method)
01-02 08:24:15.150: E/AndroidRuntime(7283):     at java.lang.reflect.Method.invoke(Method.java:525)
01-02 08:24:15.150: E/AndroidRuntime(7283):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-02 08:24:15.150: E/AndroidRuntime(7283):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-02 08:24:15.150: E/AndroidRuntime(7283):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

TextView tv =(TextView)(((TableRow)table_row))。getChildAt(i);你有问题

Tre删除 “for(int i = 1; i&lt; 11; i ++){”with“for(int i = 0; i&lt; 10; i ++){”