3

I am having trouble in displaying the values in text view. It displays only the last value in text view. I want to iterate each and every value in new line.

Here is my code.

PreviewCatNameTxt = new TextView[obdatin.catCount];
        PreviewNameTxt = new TextView[10];
        PreviewQtyTxt = new TextView[10];
        PreviewAmtTxt = new TextView[10];
        System.out.println("setview3");
        for(int i=0;i<obdatin.catCount;i++){ 
            System.out.println("num oof prca"+obdatin.catList[i].getNumCategory());
            if(obdatin.catList[i].isTotQtyNonZero(0)){
                PreviewCatNameTxt[i] = (TextView) findViewById(R.id.textViewh);

                PreviewCatNameTxt[i].setText(obdatin.catList[i].getCategoryName(0));
                System.out.println("prca"+obdatin.catList[i].getCategoryName(0));
            }
            int cnt =obdatin.catList[i].proList.getNumProduct();
        System.out.println("setview3");

        for(int j = 0; j<cnt; j++) {

            if(obdatin.catList[i].proList.isQtyNonZero(j)){

                PreviewNameTxt[j] = (TextView)findViewById(R.id.textView1);
                PreviewNameTxt[j].setText(obdatin.catList[i].proList.getProductName(j));

                PreviewQtyTxt[j] = (TextView)findViewById(R.id.textView2);

                PreviewQtyTxt[j].setText(obdatin.catList[i].proList.getProductQty(j));

                PreviewAmtTxt[j] = (TextView)findViewById(R.id.textView3);
                String amt = obdatin.catList[i].proList.getProductAmt(j);
                PreviewAmtTxt[j].setText(amt);
                System.out.println("prcaN"+obdatin.catList[i].proList.getProductName(j));
                System.out.println("prcaQ"+obdatin.catList[i].proList.getProductQty(j));
                System.out.println("prcaA"+obdatin.catList[i].proList.getProductAmt(j));
             }

        }
Kitkat
  • 77
  • 16
Abdul Naseer
  • 43
  • 1
  • 7

4 Answers4

3

Edited Code as per your requirement

PreviewCatNameTxt = (TextView) findViewById(R.id.textViewh);


for(int i=0;i<obdatin.catCount;i++){ 
            System.out.println("num oof prca"+obdatin.catList[i].getNumCategory());
            if(obdatin.catList[i].isTotQtyNonZero(0)){


                PreviewCatNameTxt.append(" "+obdatin.catList[i].getCategoryName(0));
                System.out.println("prca"+obdatin.catList[i].getCategoryName(0));
            }
            int cnt =obdatin.catList[i].proList.getNumProduct();
        System.out.println("setview3");

        for(int j = 0; j<cnt; j++) {

            if(obdatin.catList[i].proList.isQtyNonZero(j)){


                   PreviewCatNameTxt.append("\n"+obdatin.catList[i].proList.getProductName(j));

                PreviewCatNameTxt.append("\n"obdatin.catList[i].proList.getProductQty(j));

                String amt = obdatin.catList[i].proList.getProductAmt(j);
                PreviewCatNameTxt.append("\n"+amt+"\n\n");
                System.out.println("prcaN"+obdatin.catList[i].proList.getProductName(j));
                System.out.println("prcaQ"+obdatin.catList[i].proList.getProductQty(j));
                System.out.println("prcaA"+obdatin.catList[i].proList.getProductAmt(j));
             }

Please remove TextView array and all those things. Please reply your feedback.

Satyaki Mukherjee
  • 2,857
  • 1
  • 22
  • 26
  • here textview is an array how can i declare outside of the loop – Abdul Naseer Jan 08 '14 at 07:16
  • Ok so declare it inside but avoid setText and do append. I will edit my answer as per your need – Satyaki Mukherjee Jan 08 '14 at 07:18
  • but if you declare inside then it will every time create new textview so, previous id data will be missed ? can you try using my code? is my code workable? – Satyaki Mukherjee Jan 08 '14 at 07:20
  • i want to show the output as in firstline heading next line n number of rows again heading and next line n number of rows actually i used table layout by help of inflater to achive this but i got failed in that one..so i used to choose this way can you suggest me some idea – Abdul Naseer Jan 08 '14 at 07:22
  • ok then just append and replace settext your problem may be solved. – Satyaki Mukherjee Jan 08 '14 at 07:26
  • Actually your code was working but seet the before i loop.from that loop i am fetching the heading then i have come to j loop print items agin i want to print heading then items...now the problem is printing all headings first then it printng all items – Abdul Naseer Jan 08 '14 at 07:29
  • oops i think u have done a little mistakes please declare all TextView outer of any loop and then test it. One thing avoid settext . but you took different TextView so it will always show one textview content then another and then ........ so your problem will remain lie . – Satyaki Mukherjee Jan 08 '14 at 07:38
  • please do one thing take a single textview . if you ready to take single textview then I will edit my answer. – Satyaki Mukherjee Jan 08 '14 at 07:39
  • Thanks for your help bro!! finally i got it – Abdul Naseer Jan 08 '14 at 07:40
  • 1
    great bro..i used tablelayout – Abdul Naseer Jan 08 '14 at 07:51
1

I am not entirely sure what you are trying to do.Why do you use System.out.println?

If I got it right you want a larger text to be put in TextView separated by newlines after each loop of the for. One way to do it is to have a String which you fill with every iteration, also adding a newline after each iteration and after the for setting the text.

So, you could have something like this:

String catList="";
for(int i=0;i<obdatin.catCount;i++){
        if(obdatin.catList[i].isTotQtyNonZero(0)){
        catList = catList + obdatin.catList[i].getCategoryName(0) + "\n";
        }
int cnt =obdatin.catList[i].proList.getNumProduct();

PreviewCatNameTxt[i] = (TextView) findViewById(R.id.textViewh);
PreviewCatNameTxt[i].setText(catList);

Also, if you want to log something you should od to use LogCat. You can read about Reading and Writing in LogCat here and here. Good luck!

Community
  • 1
  • 1
Turbut Alin
  • 2,568
  • 1
  • 21
  • 30
  • i want to show the output as in firstline heading next line n number of rows again heading and next line n number of rows actually i used table layout to achive this but i got failed in that one..so i used to choose this way can you suggest me some idea – Abdul Naseer Jan 08 '14 at 07:17
1
PreviewNameTxt[j] = (TextView)findViewById(R.id.textView1);
PreviewQtyTxt[j] = (TextView)findViewById(R.id.textView2);
PreviewAmtTxt[j] = (TextView)findViewById(R.id.textView3);
String cat="",obtaincat="",amt="";

for(int j = 0; j<cnt; j++) {

            if(obdatin.catList[i].proList.isQtyNonZero(j)){

                cat = cat + obdatin.catList[i].getCategoryName(j) + "\n";
                obtaincat=btaincat+obdatin.catList[i].proList.getProductQty(j)+"\n";
               amt =amt+ obdatin.catList[i].proList.getProductAmt(j)+"\n";
             }
}
PreviewNameTxt[j].setText(cat);
PreviewQtyTxt[j].setText(obtaincat);
PreviewAmtTxt[j].setText(amt);
learner
  • 3,092
  • 2
  • 21
  • 33
-1

Change to Logcat :

Log.w("","prcaN"+obdatin.catList[i].proList.getProductName(j));
Log.w("","prcaQ"+obdatin.catList[i].proList.getProductQty(j));
Log.w("","prcaA"+obdatin.catList[i].proList.getProductAmt(j));
Nguyen Thanh An
  • 269
  • 1
  • 4
  • 12