javaandroidstringtextview

Android: variable from String List returning always 0


I'm trying to set a text in a TextView from a List of Strings. I can see the value in the LogCat, but when I put it in TextView I get 0. What could be the problem?

EDIT My List is not empty, I can see values in the LogCat!!

List<String> listResult = new ArrayList<String>();
// here i'm putting some data 
if (listResult != null)
{ 
    for (int i=0;i<listResult.size();i++)
    {
        Log.i("position",listResult.get(i)+"")//i can see the componnent of the list
        textView1.setText(position+1+"");
        textView3.setText(listResult.get(i)+"");//I'm getting 0
    }
    Log.i("*******","***************");
    Log.i("position",listResult+"")//i can see the whole list
}

This is the LogCat i'm getting :

05-20 11:39:07.970: I/position(21761): 0
05-20 11:39:07.970: I/position(21761): 2
05-20 11:39:07.970: I/position(21761): 0
05-20 11:39:07.970: I/position(21761): 0
05-20 11:39:07.970: I/position(21761): 0
05-20 11:39:07.970: I/position(21761): 0
05-20 11:39:07.970: I/position(21761): 6
05-20 11:39:07.971: I/position(21761): 0
05-20 11:39:07.971: I/position(21761): 0
05-20 11:39:07.971: I/position(21761): 2
05-20 11:39:07.971: I/position(21761): 0
05-20 11:39:07.971: I/position(21761): 4
05-20 11:39:07.971: I/position(21761): 0
05-20 11:39:07.971: I/position(21761): 0
05-20 11:39:07.971: I/*******(21761): ***************
05-20 11:39:07.972: I/position(21761): [0, 2, 0, 0, 0, 0, 6, 0, 0, 2, 0, 4, 0, 0]

Solution

  • The reason for getting 0 in TextView is that when you call setText() it sets a text replacing the one previously set, if any. So, in your case you just get the result of the last iteration, which is 0. In order to show the entire list in TextView you should add a new text to the one previously added at each iteration using append() method. The for loop should look as follows:

    for (int i=0; i < listResult.size(); i++) {
        Log.i("position", listResult.get(i) + "")
        textView1.append(String.valueOf(position+1));
        textView3.append(listResult.get(i).toString() + " ");
    }