如何使setOnClickListener能够更新值

时间:2014-06-23 08:01:52

标签: android textview onclicklistener

我是android的新手,我需要一些帮助。

我有四个textview,其中一个是可点击的:

clickableTextView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

clickableTextView的值为textview1,但在setOnClickListener内,clickableTextView在fisrt点击后获取textview2的值,在第二次点击后textview3的价值等等。

表示:第一次点击textview1,第二次点击textview2,第三次点击textview3,依此类推......

但即使为什么clickableTextView采用了确切的值,每次第一个文本视图时都可以点击,setOnClickListener没有更新clickableTextView的新值。有人可以帮助我,我该怎么做?

这是代码:

correctAnswerHolder.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Log.e("correctAnswerHolder", correctAnswerHolder.getText().toString());

                if ( i < list.size() )
                {
                    i++;
                    Log.e("i++", Integer.toString(i));

                    String question = list.get(i).getQuestion();
                    TextView questionText = (TextView) findViewById(R.id.questionText);
                    questionText.setText(question);

                    String correctAnswer = list.get(i).getCorrectAnswer();
                    TextView correctAnswerText = (TextView) findViewById(R.id.correctAnswerText);
                    Log.e("The Correct Answer", correctAnswer);
                    String wrongAnswer_1 = list.get(i).getWrongAnswer1();
                    TextView wrongAnswerText_1 = (TextView) findViewById(R.id.wrongAnswerText_1);
                    String wrongAnswer_2 = list.get(i).getWrongAnswer2();
                    TextView wrongAnswerText_2 = (TextView) findViewById(R.id.wrongAnswerText_2);
                    String wrongAnswer_3 = list.get(i).getWrongAnswer3();
                    TextView wrongAnswerText_3 = (TextView) findViewById(R.id.wrongAnswerText_3);

                    Uri imgUri = list.get(i).getImageURI();
                    ImageView imageView = (ImageView)findViewById(R.id.imageView);
                    imageView.setImageURI(imgUri);

                    final String [] answers = {correctAnswer, wrongAnswer_1, wrongAnswer_2, wrongAnswer_3};

                    ArrayList<String> answersList = new ArrayList<String>(Arrays.asList(answers));
                    long seed = System.nanoTime();
                    Collections.shuffle(answersList, new Random(seed));

                    Log.e("AnswersListSort ", answersList.toString());

                    correctAnswerText.setText(answersList.get(0));
                    wrongAnswerText_1.setText(answersList.get(1));
                    wrongAnswerText_2.setText(answersList.get(2));
                    wrongAnswerText_3.setText(answersList.get(3));

                    if (correctAnswerText.getText().toString().equals(correctAnswer)) correctAnswerHolder = correctAnswerText;
                    else if (wrongAnswerText_1.getText().toString().equals(correctAnswer)) correctAnswerHolder = wrongAnswerText_1;
                    else if (wrongAnswerText_2.getText().toString().equals(correctAnswer)) correctAnswerHolder = wrongAnswerText_2;
                    else if (wrongAnswerText_3.getText().toString().equals(correctAnswer)) correctAnswerHolder = wrongAnswerText_3;

                    Log.e("New correctAnswerHolder", correctAnswerHolder.getText().toString());
                }
            }
        });
    }

2 个答案:

答案 0 :(得分:0)

您可以将所有TextView设置为类属性。然后,您可以在Activity或片段内的任何位置使用TextView。

修改

在发布示例之前。什么是correctAnswerHolder?尝试更改代码:

if (correctAnswerText.getText().toString().equals(correctAnswer)) correctAnswerHolder.setText(correctAnswerText);
                    else if (wrongAnswerText_1.getText().toString().equals(correctAnswer)) correctAnswerHolder.setText(wrongAnswerText_1);
                    else if (wrongAnswerText_2.getText().toString().equals(correctAnswer)) correctAnswerHolder.setText(wrongAnswerText_2);
                    else if (wrongAnswerText_3.getText().toString().equals(correctAnswer)) correctAnswerHolder.setText(wrongAnswerText_3);

答案 1 :(得分:0)

所有这些代码:

                TextView questionText = (TextView) findViewById(R.id.questionText);
                TextView correctAnswerText = (TextView) findViewById(R.id.correctAnswerText);
                TextView wrongAnswerText_1 = (TextView) findViewById(R.id.wrongAnswerText_1);
                TextView wrongAnswerText_2 = (TextView) findViewById(R.id.wrongAnswerText_2);
                TextView wrongAnswerText_3 = (TextView) findViewById(R.id.wrongAnswerText_3);

应该在Activity的onCreate()函数中:

public class MyActivity extends Activity
{
    private TextView questionText;
    ...

    @Override
    public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        questionText = (TextView)findViewById(R.id.myactivity_questiontext);
        ...

或者在片段的onCreateView()函数中,如果您正在使用它们:

public class MyFragment extends Fragment
{
    private TextView questionText;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, something)
    {
        View view = inflater.inflate(R.layout.myfragment, container, false);
        questionText = (TextView) view.findViewById(R.id.myfragment_questiontext);
        ...
        return view;
    ...

之后,如果你已经做好了一切,它应该可行。