Question

I'm trying to show the result which I got from my array function which has multiple result into the textview

for example, if the result is 1, 3, 5, 7, 9, each result will be shown in each textview. There will be 10 textviews and only 5 textviews will be used in this case.

is anyone know how to do it?

 //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++;
            }
        }

I'm trying to shows the value of range which has remainder 1. for example, the range is 10, there will be 4 page which can be flipped. in the page 1, the value is 1, 3, 5, 7, and 9. these value will be showed in the textview

Was it helpful?

Solution 2

You need to create your TextViews dynamically. This is the optimum way.

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);
}

But since you tell that your TextViews are fixed, you need to do something which is not really "appropriate" for programming but here you go:

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);
}

OTHER TIPS

Sample code In 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>

And in java code :

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]);
    }

It's just sample change your array and textviews.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top