0

I have made an variable Tweet inside first onClick event. I need to get that variable for another onClick event. Either variable Tweet or variable RandomIndex. I'm searching internet for two hours now, having problems to find how can I pass that variable. I understand that when OnClick event finishes, it kills the variable.

buttonWhatToDoNow.setOnClickListener(new View.OnClickListener() {

            public int GetRandomNumber(int max) {
                Random rand = new Random();
                int num = rand.nextInt(max);
                return num;
            }

            int RandomIndex;

            public void onClick(View v) {

                int countMax = databaseHelper.getCountNow();
                RandomIndex = GetRandomNumber(countMax) + 1;

                String Boolean = databaseHelper.getBoolean(RandomIndex);

                if (Boolean == "1") {
                    RandomIndex = GetRandomNumber(countMax) + 1;
                    Boolean = databaseHelper.getBoolean(RandomIndex);
                }
                while (Boolean == "1") {
                    RandomIndex = GetRandomNumber(countMax) + 1;
                    Boolean = databaseHelper.getBoolean(RandomIndex);
                }

                String Title = databaseHelper.getTitleNow(RandomIndex);
                String Text1 = databaseHelper.getText1Now(RandomIndex);
                String Text2 = databaseHelper.getText2Now(RandomIndex);
                String Text3 = databaseHelper.getText3Now(RandomIndex);
                String Tweet = databaseHelper.getTweetNow(RandomIndex);

                String Image1 = databaseHelper.getImage1Now(RandomIndex);
                String Image2 = databaseHelper.getImage2Now(RandomIndex);
                String Image3 = databaseHelper.getImage3Now(RandomIndex);

                textViewDoThisTitle.setText(Title);
                textViewDoThisText1.setText(Text1);
                imageViewDoThis1.setImageResource(getResources().getIdentifier(
                        Image1, "drawable", getPackageName()));
                textViewDoThisText2.setText(Text2);
                imageViewDoThis2.setImageResource(getResources().getIdentifier(
                        Image2, "drawable", getPackageName()));
                textViewDoThisText3.setText(Text3);
                imageViewDoThis3.setImageResource(getResources().getIdentifier(
                        Image3, "drawable", getPackageName()));

            }

            public final int asddd = RandomIndex;
        });

        buttonTweetThis.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent sendIntent = new Intent(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, Tweet);
                sendIntent.setType("application/twitter");
                startActivity(Intent.createChooser(sendIntent, null));
            }
        });
4

2 回答 2

0

I dont know why you dont just use a class variable but anyway you can try this:

 setTag(Object tag)
    //Sets the tag associated with a view.


getTag(Object tag)
//gets the tag associated with a view.


thus place anything you want to save for example view.setTag(tweet);

then in the other onclick yo can get the raw object and probably have to cast it to what you want. using view.getTag(); 
于 2013-05-05T22:24:41.573 回答
0

You can use Tweet like as global variable in top of class. This is no good solution if you have complex activity.

You also may put on click listeners in different inner class, and you may throw constructor of this class pass this variable in method without global vars.

于 2013-05-05T22:27:21.693 回答