2

So below is my code that i use to display sqlite data in a single textview. So far so good. But now i need the the data to be in a Tabular form. I need the "billprint" String, because i am using it for printing the details in a printing device.

So, is there any way that the details can be printed/displayed in a tabular form within a single string file.?

private void displaydb() {
    // TODO Auto-generated method stub
    billprint = "";
    TextView tv = (TextView)findViewById(R.id.textView1);
    SQLiteDatabase myDB = null;

    try {
        myDB = getActivity().openOrCreateDatabase("shopcart1", android.content.Context.MODE_PRIVATE, null);

        String selectQuery = "SELECT  * FROM tbl_cartitems";
        Cursor c = myDB.rawQuery(selectQuery, null);

        int Column1 = c.getColumnIndex("productID");
        int Column2 = c.getColumnIndex("productName");
        int Column3 = c.getColumnIndex("productQuantity");
        int Column4 = c.getColumnIndex("productPrice");

        c.moveToFirst();
        if (c != null) {
            do {
                String pID = c.getString(Column1);
                String pName = c.getString(Column2);
                String pQua = c.getString(Column3);
                String pPri = c.getString(Column4);


                billprint = billprint +""+
                        pID+"   "+pName+"       "+""+pQua+"  "+pPri+"\n";
            } while (c.moveToNext());
        }

        tv.setText(pppid);
        tv.setTextSize(18F);
        tv.setTextColor(Color.RED);
    }

    catch (Exception e) {
        Toast.makeText(getActivity(), "Database Empty", Toast.LENGTH_LONG).show();
    } finally {
        if (myDB != null)
            myDB.close();
    }
}

So far the output is in this form:

prod-1 vanilla ice cream 1 15
prod-2 chocolate ice cream 5 50
prod-3 milk ice cream 10 150

But the output that i want should be in tabular form like:

prod-1 vanilla ice cream    1   15
prod-2 chocolate ice cream  5   50
prod-3 milk ice cream       10  150

layout (activity_testing.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.naji.productorder.Testing" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium" />

Any help would be appreciated. Thanks in advance.

2
  • If your ViewGroup is Linearlayout,then set the weight to all your textview, or create your Textview dynamically and add proper LayoutParams. Commented Apr 8, 2016 at 5:08
  • Sorry i cant do that, i just need the string file to be able to display data in tabular form, since i will be using this string to print in a device by retrieving data from database dynamically. Commented Apr 8, 2016 at 5:29

4 Answers 4

1

Don't use this kind of coding standard.

  • you should separate Ui component and Db operation by splitting up in separate class.
  • Use AsyncTask for Db operation,and create DTO class to hold data to be stored after fetching from sqlite
  • separate your business logic in service class. Don't do all operation in single class.
Sign up to request clarification or add additional context in comments.

4 Comments

You have to format it by using \n,\r and spaces("").
Actually i don't use this way for any database operation. I have seperate DBHelper class for database operation. i just gave the method to let people understand my situation. I just need a correct output for the string file. And \n or \t doesnt help, since the product name is variable hence, it goes out of sync.
You have to split the row into n number.(n=no of columns).For each column ,you should fix its maximum length.For each product name,its size is less than the fixed length,add spaces.(no of spaces added=fixed length-length of the product).By this way you can do this.I did it the same
Can you please elaborate a bit. May be an example. I have been stuck in this for 2 days. Thanks..
0

Can you use a WebView instead of TextView to display. Because if you can. This code can ease your task.

WebView webView;
    webView = (WebView) findViewById(R.id.webView);

    String billprint = "<table>";
    if (c != null) {
        do {

            String pID = c.getString(Column1);
            String pName = c.getString(Column2);
            String pQua = c.getString(Column3);
            String pPri = c.getString(Column4);

            billprint += "<tr>"
                + "<td>prod-"+pID+"</td>"
                + "<td>"+pName+"</td>"
                + "<td>"+pQua+"</td>"
                + "<td>"+pPri+"</td>"
                + "</tr>";
        } while (c.moveToNext());
    }
    billprint += "</table>";

    webView.loadDataWithBaseURL(null, billprint, "text/html", "utf-8", null);

Hope this helps!

2 Comments

Through WebView i can display it in tabular form. But i need a string file. I will be using that string for printing the output in a device printer.
Ok. I got your problem. Just to let you know that you can print webview's content also in a device printer. Even you can create a pdf from it.
0

If you use a monospaced font you can use java formatter to allocate N (16 in this case) characters per item.

Doesn't work for proportional fonts since they vary in width.

    StringBuilder sb = new StringBuilder();
    Formatter formatter = new Formatter(sb);

    String s1 = "foo";
    String s2 = "bar";
    String s3 = "foobar";
    String s4 = "hubba bubba";

    formatter.format("%16s %16s %16s %16s\n", s1, s2, s3, s4);
    formatter.format("%16s %16s %16s %16s\n", s4, s3, s2, s1);

    System.out.print(formatter);

Comments

0

you can try this..

int i=0;
String billprint="";
String billprint1="";

List<String[]> billprint2= new ArrayList<>();

String fff[];


String pID1[] = {"data1","data2","data3"};
String pName1[] =  {"wwwwwww","eeee","qqqqqq"};
String pQua1[] =  {"1","2","3"};
String pPri1[] =  {"12","13","14"};
do {
    String pID = pID1[i];
    String pName = pName1[i];
    String pQua = pQua1[i];
    String pPri = pPri1[i];


    billprint = billprint +""+
            pID+"   "+pName+"       "+""+pQua+"  "+pPri+"\n";
    ;
    String[] b1 = new String[] {pID,pName,pQua,pPri};
    Log.e("DATA",""+b1);
    billprint2.add(b1);

    i++;
} while (i<3);


String data2[];
int maxLemgth=0;
for(String sde[]: billprint2){
    data2 = sde;
    for(int y =0 ;y<data2.length;y++){
        Log.e("DATA",""+data2[y]);

        if(maxLemgth<data2[y].length())
            maxLemgth = data2[y].length();
    }
}

String HoldData="";
int counter =1;
// find data length and add space
for(String sde[]: billprint2){
    data2 = sde;

    for(int y =0 ;y<data2.length;y++){
        Log.e("DATA",""+data2[y]);

            int checklength = data2[y].length();
            if(maxLemgth>=checklength){
                String addSpace = addSpace(data2[y],(maxLemgth-checklength)+1);
                HoldData = HoldData+addSpace;
            }
            else{
                HoldData = HoldData+data2[y];

        }


    }

    HoldData=HoldData+"\n";

    counter++;
}

Log.e("DATA",""+HoldData);

and

public String addSpace(String Data, int spacecount){

        if(spacecount>0) {
            for (int d = 0; d < spacecount; d++) {
                Data = Data + " ";
            }
        }

        return Data;
    }

output:

E/DATA: 

data1   wwwwwww 1       12      

data2   eeee    2       13      

data3   qqqqqq  3       14

I hope this will help you.. thanks

4 Comments

Thanks this method works for Sytem.out.print and in log. But when i try to implement this in a textview or even in my printer, in both cases, it again goes out of order. Anyway thanks for your effort.
I have added the layout.
OK... Change width ...wrap_content to fill_parent and and same as linear layout... Match to fill
And take weight=1 into textview

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.