如何使setOnClickListener能够更新的价值 [英] How to make setOnClickListener capable of updating value

查看:276
本文介绍了如何使setOnClickListener能够更新的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android的新的,我需要一些帮助。

我有四个textviews,其中之一是可点击:

  clickableTextView.setOnClickListener(新View.OnClickListener(){
    @覆盖
    公共无效的onClick(视图v){

clickableTextView textview1 的价值,但在 setOnClickListener clickableTextView 需要 textview2 的价值最前一页点击后,第二次点击需要<$的值之后C $ C> textview3 等..

表示:第一次是可点击 textview1 ,第二次是可以点击 textview2 ,第三次是 textview3 等..

但即使为什么 clickableTextView 走的是精确值,点击是每次第一个TextView的, setOnClickListener 未更新 clickableTextView 的新值。有人可以帮助我,我该怎么办呢?

这里是code:

  correctAnswerHolder.setOnClickListener(新View.OnClickListener()
        {
            @覆盖
            公共无效的onClick(视图v)
            {
                Log.e(correctAnswerHolder,correctAnswerHolder.getText()的toString());                如果(ⅰ&下;则为list.size())
                {
                    我++;
                    Log.e(我+ +,Integer.toString(i)段);                    字符串的问题= list.get(I).getQuestion();
                    TextView的questionText =(的TextView)findViewById(R.id.questionText);
                    questionText.setText(问题);                    串correctAnswer = list.get(ⅰ).getCorrectAnswer();
                    TextView的correctAnswerText =(的TextView)findViewById(R.id.correctAnswerText);
                    Log.e(正确答案,correctAnswer);
                    串wrongAnswer_1 = list.get(ⅰ).getWrongAnswer1();
                    TextView的wrongAnswerText_1 =(的TextView)findViewById(R.id.wrongAnswerText_1);
                    串wrongAnswer_2 = list.get(ⅰ).getWrongAnswer2();
                    TextView的wrongAnswerText_2 =(的TextView)findViewById(R.id.wrongAnswerText_2);
                    串wrongAnswer_3 = list.get(ⅰ).getWrongAnswer3();
                    TextView的wrongAnswerText_3 =(的TextView)findViewById(R.id.wrongAnswerText_3);                    乌里imgUri = list.get(I).getImageURI();
                    ImageView的ImageView的=(ImageView的)findViewById(R.id.imageView);
                    imageView.setImageURI(imgUri);                    最终的String [] =答案{correctAnswer,wrongAnswer_1,wrongAnswer_2,wrongAnswer_3};                    ArrayList的&LT;串GT; answersList =新的ArrayList&LT;串GT;(Arrays.asList(答案));
                    长种子= System.nanoTime();
                    Collections.shuffle(answersList,新的随机(种子));                    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));                    如果(。correctAnswerText.getText()的toString()等于(correctAnswer))correctAnswerHolder = correctAnswerText;
                    否则,如果(wrongAnswerText_1.getText()的toString()等于(correctAnswer)。)correctAnswerHolder = wrongAnswerText_1;
                    否则,如果(wrongAnswerText_2.getText()的toString()等于(correctAnswer)。)correctAnswerHolder = wrongAnswerText_2;
                    否则,如果(wrongAnswerText_3.getText()的toString()等于(correctAnswer)。)correctAnswerHolder = wrongAnswerText_3;                    Log.e(新correctAnswerHolder,correctAnswerHolder.getText()的toString());
                }
            }
        });
    }


解决方案

这一切code的:

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

应该是在活动的onCreate()函数:

 公共类MyActivity扩展活动
{
    私人TextView的questionText;
    ...    @覆盖
    公共无效的onCreate(捆绑包)
    {
        super.onCreate(包);
        questionText =(的TextView)findViewById(R.id.myactivity_questiontext);
        ...

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

 公共类MyFragment扩展片段
{
    私人TextView的questionText;    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,东西)
    {
        查看查看= inflater.inflate(R.layout.myfragment,集装箱,FALSE);
        questionText =(TextView中)view.findViewById(R.id.myfragment_questiontext);
        ...
        返回视图。
    ...

之后,它或许应该工作,如果你已经做的一切权利。

I'm new in android and I need some help.

I have four textviews, and one of them is clickable:

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

clickableTextView has the value of textview1, but inside setOnClickListener the clickableTextView takes the value of textview2 after the fisrt click, after the second click takes the value of textview3 and so on..

Means that: the first time is clickable textview1, the second time is clickable textview2, the third time is textview3 and so on..

but even why clickableTextView is taking the exact values, clickable is everytime the first textview, setOnClickListener is not updating the new value of clickableTextView. Can someone help me how can i do this?

HERE IS THE CODE:

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

解决方案

All of this code:

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

Should be in the Activity's onCreate() function:

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

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

Or in the onCreateView() function of the Fragment, if you're using them:

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;
    ...

Afterwards, it should probably work if you're already doing everything right.

这篇关于如何使setOnClickListener能够更新的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆