读取TableRow中的数据内容

时间:2011-10-22 00:22:09

标签: android tablerow

我有一个动态加载的TableRow。它包含多个EditTexts和CheckBoxes。如何从这些EditTexts和CheckBoxes获取用户输入?

这是我的代码

public void cargarProductos(SQLiteDatabase db){             TableLayout tabla =(TableLayout)findViewById(R.id.Detalle);

        Cursor cProd = db.rawQuery("SELECT * FROM vstEncProductos WHERE IdCliente="+idCliente+" AND Idcategoria="+IdCategoria, null);
        int ValorChk = 0;


        if (cProd.moveToFirst()) {
            do {
                 TableRow row = new TableRow(this);

                 row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
                 row.setId(cProd.getInt(0));

                 ImageButton btnBarCode = new ImageButton(this);
                 btnBarCode.setBackgroundResource(R.drawable.barcodereader48);
                 btnBarCode.setOnClickListener(new OnClickListener() {
                     public void onClick(View v) {
                            Intent intent = new Intent("com.google.zxing.client.android.SCAN");
                            intent.setPackage("com.google.zxing.client.android");
                            startActivityForResult(intent, 0);

                     };

                    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
                        if (requestCode == 0) {
                            if (resultCode == RESULT_OK) {
                                String contents = intent.getStringExtra("SCAN_RESULT");
                            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
                            showDialog(R.string.result_succeeded, "Format: " + format + "\nContents: " + contents);

                            // Handle successful scan
                            } else if (resultCode == RESULT_CANCELED) {
                                // Handle cancel
                            }
                        }
                        }

                 }); 

                 row.addView(btnBarCode, new TableRow.LayoutParams(0));

                 TextView txtProducto = new TextView(this);
                 txtProducto.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
                 txtProducto.setText(cProd.getString(1));
                 txtProducto.setWidth(230);
                 row.addView(txtProducto, new TableRow.LayoutParams(1));

                 EditText txtPrecio = new EditText(this);
                 txtPrecio.setWidth(130);
                 txtPrecio.setInputType(InputType.TYPE_CLASS_NUMBER);
                 row.addView(txtPrecio, new TableRow.LayoutParams(2));





                 TextView txtPrecioAnt = new TextView(this);
                 txtPrecioAnt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
                 txtPrecioAnt.setText(cProd.getString(7));
                 txtPrecioAnt.setWidth(115);
                 txtPrecioAnt.setGravity(Gravity.RIGHT);
                 row.addView(txtPrecioAnt, new TableRow.LayoutParams(3));

                 CheckBox chkPresencia = new CheckBox(this);
                 ValorChk=cProd.getInt(4);
                 chkPresencia.setChecked(ValorChk==-1);
                 chkPresencia.setWidth(50);
                 row.addView(chkPresencia, new TableRow.LayoutParams(4));

                 CheckBox chkPrecioPromo = new CheckBox(this);
                 ValorChk=cProd.getInt(4);
                 chkPrecioPromo.setChecked(ValorChk==-1);
                 chkPrecioPromo.setWidth(50);
                 row.addView(chkPrecioPromo, new TableRow.LayoutParams(4));

                 CheckBox chkAlerta = new CheckBox(this);
                 ValorChk=cProd.getInt(4);
                 chkAlerta.setChecked(ValorChk==-1);
                 chkAlerta.setWidth(50);
                 row.addView(chkAlerta, new TableRow.LayoutParams(4));                 

                 tabla.addView(row, new TableLayout.LayoutParams());



            } while (cProd.moveToNext());  
        }

        cProd.close();

2 个答案:

答案 0 :(得分:0)

我假设您正在使用某种适配器类将数据加载到表中。创建视图时,只需为复选框分配OnClickListener,为EditTexts分配TextWatcher即可。使用这些类中的回调可以使用用户输入执行所需的操作。我能正确理解你的问题吗?

答案 1 :(得分:0)

当您创建新的表格时,您可以将每个文件保存在多个矢量中,例如字符串。那么你只需要在透明向量内获得每个向量的大小。我修改了你的cody的例子:

             Vector<Vector<String>> result = new Vector<Vector<String>>();
              Vector<String> vecAux = new Vector<String>();

             TextView txtProducto = new TextView(this);
             txtProducto.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
             txtProducto.setText(cProd.getString(1));
             txtProducto.setWidth(230);
             row.addView(txtProducto, new TableRow.LayoutParams(1));
             vecAux.add(cProd.getString(1));

             EditText txtPrecio = new EditText(this);
             txtPrecio.setWidth(130);
             txtPrecio.setInputType(InputType.TYPE_CLASS_NUMBER);
             row.addView(txtPrecio, new TableRow.LayoutParams(2));


             TextView txtPrecioAnt = new TextView(this);
             txtPrecioAnt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
             txtPrecioAnt.setText(cProd.getString(7));
             txtPrecioAnt.setWidth(115);
             txtPrecioAnt.setGravity(Gravity.RIGHT);
             row.addView(txtPrecioAnt, new TableRow.LayoutParams(3));
             vecAux.add(cProd.getString(7));

             CheckBox chkPresencia = new CheckBox(this);
             ValorChk=cProd.getInt(4);
             chkPresencia.setChecked(ValorChk==-1);
             chkPresencia.setWidth(50);
             row.addView(chkPresencia, new TableRow.LayoutParams(4));


             CheckBox chkPrecioPromo = new CheckBox(this);
             ValorChk=cProd.getInt(4);
             chkPrecioPromo.setChecked(ValorChk==-1);
             chkPrecioPromo.setWidth(50);
             row.addView(chkPrecioPromo, new TableRow.LayoutParams(4));

             CheckBox chkAlerta = new CheckBox(this);
             ValorChk=cProd.getInt(4);
             chkAlerta.setChecked(ValorChk==-1);
             chkAlerta.setWidth(50);
             row.addView(chkAlerta, new TableRow.LayoutParams(4));                 

             tabla.addView(row, new TableLayout.LayoutParams());

result.add(vecAux);

获得结果,寻找矢量的所有位置:


      String texto = "";

      for (int i = 0; i < result.size(); i++) {
        for (int j = 0; j < result.elementAt(i).size(); j++) {
       texto += result.elementAt(i).elementAt(j) + "\t";
        }
      }

对于get if是否选中复选框,您可以通过ID(findViewById)获取它。 我希望能帮到你,再见; - )