0

I have a table with 3 columns, like this

A|B|C
1|4|5
2|9|0
3|6|2
and so on... I am creating this table using the data I am fetching from room database

Now I want to create a string that contains this data completely formatted like the table I have shown above. So that I can share the data as string through an intent. How can I accomplish this? I tried looking at other questions but they were using System.out.Format and i have no idea of how to use it store all that data in a string in an efficient way because my tables can have thousand of rows. I also tried searching on google with different queries on how to share the table data through intent in android.

I want to create a table like this. https://stackoverflow.com/a/18672745 but I don't know how to save that output in a string so that I can share that using android intent. this seemed to be the most related query to the problem to search on google. https://www.google.com/search?q=store+a+properly+formatted+table+in+a+string+java+android&client=opera&ei=21MCYYzCCsqW4-EPyaO5-A4&oq=store+a+properly+formatted+table+in+a+string+java+android&gs_lcp=Cgdnd3Mtd2l6EAMyBwghEAoQoAEyBwghEAoQoAE6BwgAEEcQsAM6BQghEKABOgQIIRAVSgQIQRgAUOGHB1illAdgnZYHaAFwAngAgAGRAogBvQ6SAQUwLjEuN5gBAKABAcgBCMABAQ&sclient=gws-wiz&ved=0ahUKEwjMm6yg3IfyAhVKyzgGHclRDu8Q4dUDCA4&uact=5
I am using a StringBuilder and iterating over each item in list using for loop the problem is that the format does not look good.

it looks like this Number|column1|column2
1|0|0
2|0.602|2.674
3|0.5|3.174

builder.append("Number|"+"Column1"+"|Column2");
                builder.append(System.getProperty("line.separator"));
                for(int i=0;i<list.size();i++) {
                    if(i==0){
                        builder.append(i+"|" + "0"+"|0");
                        builder.append(System.getProperty("line.separator"));
                    } else {
...

                    }
                }
and then builder.toString();
james
  • 1
  • 1
  • show us what you have tried so far. – Scary Wombat Jul 29 '21 at 07:42
  • @ScaryWombat I already have my table that I have created using recycler view. Now I want to share that data using intent to other apps. I am also editing the question to add more info on what I have tried to create that string. – james Jul 29 '21 at 07:47
  • @ScaryWombat do you need more info ? if you do then please let me know what kind of info . If my approach is wrong then let me know how would you interpret this problem to come up with possible set of solutions? – james Jul 29 '21 at 07:56
  • James break this down into answerable questions - what do you want to know first? Maybe how to convert a List to a String delimetered with a pipe? If so show this code and indicate where you are facing problems. – Scary Wombat Jul 29 '21 at 07:58

1 Answers1

0

I have an idea you can make a query to get all tables record "select * FROM " somthing like that

@Query("SELECT * FROM test_table ") List getTableRecord();

now iterate throw the List and do whatever you want with the data you can add each row to a string separated by "," and each column separated by "::" then when you want to print it as a table just use the split method in java for each column and row