将arraylist值分配给textview

时间:2014-04-22 10:12:33

标签: android arrays arraylist

我正在尝试将我的arraylist中的值设置为textview

    ArrayList<Integer> arrayListPage1, arrayListPage2, arrayListPage3, arrayListPage4, arrayListPage5;

    arrayListPage1 = new ArrayList<Integer>(rangeMode);
    arrayListPage2 = new ArrayList<Integer>(rangeMode);
    arrayListPage3 = new ArrayList<Integer>(rangeMode);
    arrayListPage4 = new ArrayList<Integer>(rangeMode);
    arrayListPage5 = new ArrayList<Integer>(rangeMode);
    totalCardInPage = new int[totalPage+1];
    for(int j=1;j<=totalPage;j++){  
        int x=0;
        for(int i=1;i<=rangeMode;i++){
            if(binaryTable[i][j]==1){                   
                //maka i ada di page j
                cardInPage[j][x]=i;
                //array buat card di page 1
                if (j==1){
                    arrayListPage1.add(i);
                }else if(j==2){
                    arrayListPage2.add(i);
                }else if(j==3){
                    arrayListPage3.add(i);
                }else if(j==4){
                    arrayListPage4.add(i);
                }else if(j==5){
                    arrayListPage5.add(i);
                }
                x++;                    
            }
        }
        System.out.println("page-"+j+" jumlah kartu:"+x);
        totalCardInPage[j]=x;
    }
    System.out.println("List page 1 "+arrayListPage1);
    System.out.println("List page 2 "+arrayListPage2);
    System.out.println("List page 3 "+arrayListPage3);
    System.out.println("List page 4 "+arrayListPage4);
    System.out.println("List page 5 "+arrayListPage5);
    TextView text1 = (TextView) findViewById(R.id.text1);
    if (arrayListPage1[0]!=null){

        text1.setText(arrayListPage1[0]);
    }
    else{
        text1.setVisibility(View.GONE);
    }

    if (arrayListPage1[1]!=null){
        text1.setText(arrayListPage1[1]);
    }
    else{
        text1.setVisibility(View.GONE);
    }

但是当我尝试将arraylist值设置为textview时出现错误。错误说因为我正在使用arraylist所以我无法将值分配给textview

任何人都知道如何解决这个问题?

感谢

1 个答案:

答案 0 :(得分:3)

之所以发生这种情况,是因为您将Integer值设置为TextView,而TextView则从资源中查找String。将Integer值转换为String。

例如:

text1.setText(arrayListPage1.get(0).toString());

或简单:

text1.setText(arrayListPage1.get(0) + "");