我试图显示我从数组函数中得到的结果,该结果在textview中有多个结果
例如,如果结果为1,3,5,7,9,则每个结果将显示在每个文本视图中。在这种情况下,将有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中显示
答案 0 :(得分:2)
示例代码在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]);
}
只是示例更改您的数组和文本视图。
答案 1 :(得分:1)
您需要动态创建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);
}