【问题标题】:Android array result shows in multiple textviewAndroid数组结果显示在多个文本视图中
【发布时间】:2014-04-22 06:48:14
【问题描述】:

我正在尝试显示我从数组函数中获得的结果,该数组函数在文本视图中有多个结果

例如,如果结果是 1、3、5、7、9,则每个结果将显示在每个 textview 中。将有 10 个文本视图,在这种情况下将仅使用 5 个文本视图。

有人知道怎么做吗?

 //this function is used to determine how many page can be flipped. I used viewFlipper in this case. rangeValue is depending on the user input
 public int binaryRangePage(int rangeValue){
    int flipperPage = 1;
    while(rangeValue >= 2){
        rangeValue = rangeValue/2;
        flipperPage++;
    }
    return flipperPage;
}


    totalPage = binaryRangePage(rangeMode);
            //initialization range for 2 dimensional table
    binaryTable = new int[rangeMode+1][totalPage+1];
    int n;
    int i;
    int k;          
    //looping the range
    for(i=1; i<=rangeMode; i++){
        n = i;
        flipperPage = 1;
        //looping for each page in viewFlipper
        while(n>0){
            remainder = n%2;
            binaryTable[i][flipperPage] = remainder;
            n = (int) Math.floor(n/2);
            System.out.println("remainder["+i+"]["+flipperPage+"]: "+remainder);            
            flipperPage++;

            }
        //if the current page is less than total page which should has value, the remainder page will be filled with 0
        if(flipperPage<=totalPage){
            //looping untuk remainder page dgn 0
            for(k=flipperPage; k<=totalPage;k++){
                binaryTable[i][flipperPage] = 0;
                System.out.println("remainder["+i+"]["+flipperPage+"]: 0");
                flipperPage++;
            }
        }

我试图显示余数为 1 的范围的值。例如,范围为 10,将有 4 页可以翻转。在第 1 页中,值为 1、3、5、7 和 9。这些值将显示在 textview 中

【问题讨论】:

  • 如果可能的话,结果会随机显示在textview中
  • 到目前为止你尝试了什么?
  • 从代码创建TextView,查看此链接stackoverflow.com/questions/10482099/…
  • @NambiNarayanan 我尝试获取这些数组列表。现在我想在我的文本视图中显示这些列表。文本视图已修复。所以在我的页面中,会有 10 个文本视图。如果有 4 个列表,则只使用 4 个 textview,剩余的 textview 将为空白。
  • @Mobi 我的文本视图在我的页面中是固定的,只有那些数组列表取决于用户输入

标签: android arrays textview


【解决方案1】:

xml中的示例代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" 
    android:orientation="vertical">

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />
    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />
    <TextView
        android:id="@+id/textview2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

</LinearLayout>

在java代码中:

private static String[] array = {"a", "b", "c"};
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView text = (TextView) findViewById(R.id.textview);
        TextView text1 = (TextView) findViewById(R.id.textview1);
        TextView text2 = (TextView) findViewById(R.id.textview2);

        text.setText(array[0]);
        text1.setText(array[1]);
        text2.setText(array[2]);
    }

这只是更改数组和文本视图的示例。

【讨论】:

    【解决方案2】:

    您需要动态创建 TextView。这是最佳方式。

    LinearLayout layout  = (LinearLayout) findViewById(R.id.linearLayout1);
    
    for (int i = 0; i <= array.length ; i++){
          TextView tv = new TextView(getApplicationContext());
          tv.setText(array[i]);
          layout.addView(tv);
    }
    

    但是既然你告诉你你的 TextViews 是固定的,你需要做一些对编程来说并不“合适”的事情,但是你去吧:

    if (array[0]!=null){
        textView1.setText(array[0]);
    }
    else{
        textView1.setVisibility(View.GONE);
    }
    
    if (array[1]!=null){
        textView2.setText(array[1]);
    }
    else{
        textView2.setVisibility(View.GONE);
    }
    .
    .
    .
    if (array[9]!=null){
        textView10.setText(array[9]);
    }
    else{
        textView10.setVisibility(View.GONE);
    }
    

    【讨论】:

    • 如果我的文本视图是固定的,就像有 10 个文本视图一样,是否有可能?
    猜你喜欢
    • 1970-01-01
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    相关资源
    最近更新 更多