How to get selected values in table row in android

我的梦境 提交于 2019-12-11 04:52:55

问题


I Had the table layout with 11 table rows and check boxes each table row i want to get selected check box row value and display in a toast message on Long clicking on selected row i had done this blow but it wont works please help me with this

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;


    }

This is my 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:


TextView tv = (TextView)(((TableRow)table_row)).getChildAt(i); you problem

Tre to remove "for(int i=1;i<11;i++){" with "for(int i=0;i<10;i++){"



来源:https://stackoverflow.com/questions/31085490/how-to-get-selected-values-from-a-table-row-in-a-table-layout-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!