0

I have a string, comma separated, from mysql database witch I split into an array. My problem is that when I try to put them in a tablerow programatically it display each split value in a new row. I want to display it in same row with 4 columns. This is my code:

String currentString = Order_list;
            String[] separated = currentString.split(",");

            TableLayout secondTbl = findViewById(R.id.secondTable);

            for(int i=0;i<separated.length;i++){
                TableRow row= new TableRow(getApplicationContext());
                TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
                row.setLayoutParams(lp);
                row.setBackgroundResource(R.drawable.row_drawable);
                txtOrdprod = new TextView(getApplicationContext());
                row.addView(txtOrdprod);
                TableRow.LayoutParams params = (TableRow.LayoutParams) txtOrdprod.getLayoutParams();
                params.leftMargin = 18;
                params.rightMargin = 10;
                params.topMargin = 10;
                txtOrdprod.setLayoutParams(params);
                txtOrdprod.setTextSize(TypedValue.COMPLEX_UNIT_SP,22);
                txtOrdprod.setTextColor(Color.BLACK);
                txtOrdprod.setTypeface(null, Typeface.BOLD);
                txtOrdprod.setText(separated[i]);
                secondTbl.addView(row,i);
            }
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189

1 Answers1

0

You have to create a row, and put your columns (seperated strings) into that row. For example:

String orders = "2, Product, 100 Tablets, 50 Eur, 3, Product, 100 Tablets, 75 Eur";

int k = 0;
int startPoint = 0;
List<String> orderList = new ArrayList<>();
for(int i = 0; i < orders.length(); i++)
{
    if(orders.charAt(i) == ',')
    {
        k++;
        if(k == 4)
        {
            String ab = orders.substring(startPoint, i);
            orderList.add(ab);
            startPoint = i + 1;
            k = 0;
        }
    }
}

TableLayout secondTbl = findViewById(R.id.secondTable);

int rowIndex = 0;
for(String order : orderList)
{
    String[] separated = order.split(",");

    TableRow row = new TableRow(getApplicationContext());
    TableRow.LayoutParams lp = new 
    TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 
    TableRow.LayoutParams.WRAP_CONTENT);
    row.setLayoutParams(lp);
    row.setBackgroundResource(R.drawable.row_drawable);
    secondTbl.addView(row, rowIndex);

    for(int i = 0; i < separated.length; i++)
    {
        txtOrdprod = new TextView(getApplicationContext());
        row.addView(txtOrdprod);
        TableRow.LayoutParams params = (TableRow.LayoutParams) 
        txtOrdprod.getLayoutParams();
        params.leftMargin = 18;
        params.rightMargin = 10;
        params.topMargin = 10;
        txtOrdprod.setLayoutParams(params);
        txtOrdprod.setTextSize(TypedValue.COMPLEX_UNIT_SP,22);
        txtOrdprod.setTextColor(Color.BLACK);
        txtOrdprod.setTypeface(null, Typeface.BOLD);
        txtOrdprod.setText(separated[i]);
    }

    rowIndex++;
}

But keep in mind that the orders string needs to be formatted like the on in the example, otherwise it will not work. It would be better if you have a POJO for an order and work with this one.

kAliert
  • 768
  • 9
  • 21
  • Ok this is working. BUT.. at the rowIndex does not work. Because i have multiple rows that i want to be generated. – Iulian Nistor Aug 30 '18 at 06:09
  • Do you have a list of strings you want to add to the table? Than you have to create a new for-loop above the code (see my edited example) – kAliert Aug 30 '18 at 06:16
  • Still not working. My string is different for each order. Exemple: "2,Product,100Tablets,50 Eur". And i have same string with multiple products. Exemple: "2,Product,100 Tablets,50 Eur, 3,Product,100 Tablets, 75 Eur". So i want after each currency to be in a new row – Iulian Nistor Aug 30 '18 at 06:31
  • Than you have to format your string first, to match your desired result. After that you can add them to the table. – kAliert Aug 30 '18 at 06:38
  • I do not know how to format. I guess with for-loop make new string after currency, but i don not now how :( – Iulian Nistor Aug 30 '18 at 06:47
  • I modified the answer (https://stackoverflow.com/questions/28358388/how-to-split-string-at-every-nth-occurrence-of-character-in-java) to match your problem: `String test = "1,2,3,4,1,2,3,4,1,2,3,4"; int k = 0; int startPoint = 0; ArrayList arrayList = new ArrayList<>(); for(int i = 0; i < test.length(); i++) { if(test.charAt(i) == ',') { k++; if(k == 4) { String ab = test.substring(startPoint, i); System.out.println(ab); arrayList.add(ab); startPoint = i + 1; k = 0; } } }` – kAliert Aug 30 '18 at 07:25
  • Works great. Thanx – Iulian Nistor Aug 30 '18 at 08:02