为来自excel的线性布局的每个项目添加动态复选框

时间:2016-10-22 01:47:32

标签: java android xml

我开始导入我的excel信息以在android中显示。现在我想为从excel传递的每一列添加复选框,以便保存我选择的列并将它们放入其他活动中。有人能帮助我吗?

https://postimg.org/image/9ysb2vj8t/ 这是一张图片,展示了我的应用程序的最新动态

这是我的java文件

public void Order(View v){
            try{
                String xx="";
                AssetManager am = getAssets();
                InputStream is = am.open("torab.xls");
                Workbook wb = Workbook.getWorkbook(is);
                Sheet s = wb.getSheet(0);
                int row = s.getRows();
                int column = s.getColumns();

                for(int i = 0; i<row; i++){

                    for(int j=0; j<column; j++){
                        Cell z = s.getCell(j,i);
                        xx= xx + z.getContents();

                    }

                  xx = xx +"\n";
                    RelativeLayout linear=(RelativeLayout) findViewById(R.id.linear);
                    RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams(
                            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

                    for(int p=0;p<column;p++){

                        CheckBox checkBox = new CheckBox(this);
                        checkBox.setLayoutParams(lparams);

                        linear.addView(checkBox);

                    }

                }

                Display(xx);

            }
            catch(Exception e){


            }
    }


    public void Display(String value){
        TextView t = (TextView) findViewById(R.id.textView);
        t.setText(value);

    }

    public void comparar(View v){

        Intent i = new Intent(getApplicationContext(),Bluetooth.class);
        startActivity(i);
    }

这是我的xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linear"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.altran.vitor.MainActivity">


    <Button
        android:text="Gerar Dados"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:enabled="true"
        android:longClickable="true"
        android:onClick="Order"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="34dp"
        android:layout_marginStart="34dp"
        android:layout_marginBottom="64dp" />

    <Button
        android:text="Comparar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="comparar"
        android:id="@+id/button2"
        android:layout_alignBaseline="@+id/button"
        android:layout_alignBottom="@+id/button"
        android:layout_toRightOf="@+id/button"
        android:layout_toEndOf="@+id/button"
        android:layout_marginLeft="39dp"
        android:layout_marginStart="39dp" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true">


        <TextView
            android:text="TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="18dp"
            android:layout_marginStart="18dp"
            android:id="@+id/textView" />
    </ScrollView>

</RelativeLayout>

如果你帮我解决这个问题,我将永远感激。

0 个答案:

没有答案