質問

ラジオグループにラジオバイトンを動的に追加しようとしていますが、ボタンは表示されません。ただし、テキストはそうです。私は初心者なので、私が間違ったことをすべて指摘してください。

APKV7を使用して(2.1だと思います)。

package test.test2;

import android.app.Activity;
import android.os.Bundle;
import android.widget.*;

public class TipsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);


    //create objects
    ScrollView scrollv = new ScrollView(this);
    LinearLayout linlay = new LinearLayout(this);
    linlay.setOrientation(LinearLayout.VERTICAL);
    scrollv.addView(linlay);

    int currentQuestion = 0, questions = 3;
    int [] answers = new int[4];
    answers[0] = 3;
    answers[1] = 3;
    answers[2] = 4;
    answers[3] = 6;
    while (questions > currentQuestion) {           

        RadioGroup rg = new RadioGroup(this);

        TextView tv = new TextView(this);
        tv.setText("Question no. " + currentQuestion);



        int currentAnswer = 0;

        while (currentAnswer > answers[currentQuestion]) {

            RadioButton rb = new RadioButton(this);
            //rb.setText("Answer no. " + currentAnswer);
            rg.addView(rb);
            currentAnswer++;
        }

        linlay.addView(rg);
        linlay.addView(tv);
        currentQuestion++;  
    }
    setContentView(scrollv);
}
}

(NoteNoughContext NoteNoughContext)

役に立ちましたか?

解決

私はあなたがこのようにあなたのwhileループをするべきだと思います:

while (currentAnswer < answers[currentQuestion]) {

        RadioButton rb = new RadioButton(this);
        //rb.setText("Answer no. " + currentAnswer);
        rg.addView(rb);
        currentAnswer++;
    }

問題は、Currentanswerがどの答えよりも小さいことです...

他のヒント

このコードを置き換えます:

int currentAnswer = 0;

while (currentAnswer > answers[currentQuestion])

これに:

int currentAnswer = 0;

while (currentAnswer < answers[currentQuestion])
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top