Extract data from tablelayout to array and pass to server.(Android)

728 Views Asked by At

In my application I want to add data to dynamically created table in table layout temporarily and pass multiple rows of data to php mysql thru volley. I can't figure out how to pass the values of dynamic table data to array.. i can do it in website using jQuery..by this code.

var myTableArray = [];
$("table#myTable tr").each(function() { 
var arrayOfThisRow = [];
var tableData = $(this).find('td');
if (tableData.length > 0) {
    tableData.each(function() { arrayOfThisRow.push($(this).text()); });
    myTableArray.push(arrayOfThisRow);}

How to do it in android?

My Activity:

public class ArrayActivity extends AppCompatActivity {

EditText t1, t2,t3, t4;
Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_array);

    t1 = (EditText) findViewById(R.id.id_01);
    t2 = (EditText) findViewById(R.id.id_02);
    t3 = (EditText) findViewById(R.id.id_03);
    t4 = (EditText) findViewById(R.id.id_04);
    btn = (Button) findViewById(R.id.id_btn);
    final TextView textView = (TextView) findViewById(R.id.tv_101);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String s1, s2, s3, s4;
            s1 = t1.getText().toString();
            s2 = t2.getText().toString();
            s3 = t3.getText().toString();
            s4 = t4.getText().toString();

            TableLayout tableLayout = (TableLayout) findViewById(R.id.up_table);
            TableRow row = new TableRow(ArrayActivity.this);
            TextView t1v = new TextView(ArrayActivity.this);
            t1v.setText(s1);
            t1v.setGravity(Gravity.CENTER);
            t1v.setBackgroundResource(R.drawable.row_border);
            row.addView(t1v);
            TextView t2v = new TextView(ArrayActivity.this);
            t2v.setText(s2);
            t2v.setGravity(Gravity.CENTER);
            t2v.setBackgroundResource(R.drawable.row_border);
            row.addView(t2v);
            TextView t3v = new TextView(ArrayActivity.this);
            t3v.setText(s3);
            t3v.setGravity(Gravity.CENTER);
            t3v.setBackgroundResource(R.drawable.row_border);
            row.addView(t3v);
            TextView t4v = new TextView(ArrayActivity.this);
            t4v.setText(s4);
            t4v.setGravity(Gravity.CENTER);
            t4v.setBackgroundResource(R.drawable.row_border);
            row.addView(t4v);
            tableLayout.addView(row);                 


        }
    });
}
}

PHP Code

<?php
$con = mysqli_connect('localhost', '******', '******', '******');
if ($con->connect_error) {
 die("Connection failed: " . $con->connect_error);
}

$myTableArray = $_REQUEST['myTableArray'];

 if(is_array($myTableArray)){

$aValues = array();
foreach ($myTableArray as $aRow) {
$aValues[] = "'" . implode("','", $aRow) . "'";
 }
$sValues = "(" . implode("), (", $aValues) . ")";

$sql = "INSERT INTO mess (name, goods, quantity, price, action) values $sValues";
}

if ($con->query($sql) == TRUE) {
echo "Dear User, Data Added Successfully!";
} else {
echo "Error: " . $sql . "<br>" . $con->error;
}

$con->close();

?>
1

There are 1 best solutions below

0
On

Lets break this into 2 parts, parsing and dynamically creating the table:

1) For the parsing you can use Java Pattern or String.find() to format your data into a meaningful data structure (probably a 2 dimensional String array) - there are many examples and since I am uncertain what the input is I can't help you with the specifics.

2) As for dynamically creating the table, lets assume that we are using a 2 dimensional String array:

private void updateTable(@NonNull TableLayout table, @NonNull ArrayList<ArrayList<String>> data) {

    // Just to be sure table is clean
    table.removeAllViews();

    for (ArrayList<String> row: data) {

        TableRow tableRow = new TableRow(table.getContext());
        tableRow.setLayoutParams(new TableLayout.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));

        for (String text: row) {
            TextView textView = new TextView(table.getContext());
            textView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
            textView.setText(text);
            tableRow.addView(textView);
        }
        table.addView(tableRow);
    }
}