【问题标题】:read data content in TableRow读取 TableRow 中的数据内容
【发布时间】:2012-08-17 17:01:19
【问题描述】:

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

这是我的代码

公共无效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();

【问题讨论】:

  • 欢迎来到 Stackoverflow!如果您的特定答案有帮助,请投票。如果某个答案是正确的,请接受它。

标签: android tablerow


【解决方案1】:

我假设您正在使用某种适配器类将数据加载到您的表中。创建视图时,只需将OnClickListener 分配给复选框,将TextWatcher 分配给EditTexts。使用这些类的回调来根据用户输入做任何你想做的事情。我是否正确理解了您的问题?

【讨论】:

  • 非常感谢,通过一个游标的TableRow,所以我不明白是我必须读取用户输入的数据才能更新我在数据库中的表。不喜欢走 TableRow 并删除 EditText 控件数据和 CheckBox
  • 在 OnClickListener 和 TextWatcher 的回调中,您应该使用更新的用户信息调用您的数据库。
  • 但这里有一个调查应该蛋黄是很多信息,最后我存储了所有数据。更明确地说,我有一个 3-TAB TABHOST,每个都有一个 TableLayout,其中包含“N”型 TableRow EditText 和 CheckBox 控件我需要读取所有行和列并从控件获取数据
  • 对不起,我好像听不懂你在问什么。
  • 我的问题是!如何获取用户在包含“N”行的 TableRow 中包含的控件 (EditText) 中输入的数据。
【解决方案2】:

当您创建新的表格行时,您可以将每个文件保存在一个多向量中,例如字符串。然后你只需要在父向量中获取每个向量的大小。我修改了你的代码的例子:

             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());

结果.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";
        }
      }

如果复选框被选中,您可以通过 ID (findViewById) 获取它。 希望对你有帮助,再见;-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-23
    相关资源
    最近更新 更多