开发者

Android - Correctly referencing and updating widgets and controls

Could someone explain the best way to achieve the following, I think it's a fairly simple question but I'd like to know the best way, as it can be achieved several different ways.

Let's say I wanted a class which updated a TextView's text to "Test". Of course this isn't what my real problem is, but I'm trying to make this question as simple as possible. Let's also say this will accept any text view.

The class it calls does not inherit Activity.

The ways I see to do this are as follows, please explain why and why not these methods should or shouldn't be used.

Pass the TextView as a reference and upd开发者_运维百科ate the text.

public class Test

public void updateText(TextView tv)
{
   tv.setText("Test");
}

The other option is to pass the Activity, and call findViewById, but the problem here is if the ID does not match that that the Test class expects, the view will return null and won't update the TextView.

public class Test

public void updateText(Activity act)
{
   TextView tv = (TextView) act.findViewById(R.id.i_must_exist);
   tv.setText("Test");
}

Another choice would be to use getters/setters.

public class Test

private TextView mTvToUpdate;

public void setTextView(TextView tv)
{
    mTvToUpdate = tv;
}

public void updateText(Activity act)
{
    mTvToUpdate.setText("Test");
}

I guess the real question is it wrong to pass an objects reference as a parameter, is Activity the preferred way? Why? It is more likely to experience memory leaks, are both solutions OK? Is it down to preference?

Please don't reply with "Why would you want to do this?" as in this example I obviously wouldn't want to do what I am asking, but the question behind it still applies.

Thanks in advance.


I wouldn't go with the last way without a good reason because when you hold a reference to an UI element, you may leak memory more easily. When you hold a reference to a Widget, you hold a reference to the Activity either, so you have to make sure that you don't hold the reference to the Widget if the Activity is destroyed (possibly with a WeakReference). This is described in the article Avoiding Memory Leaks.

Besides that there's no real difference.


The object which "owns" the textfield should be the one who actually calls the setText method. So in most cases, that would be the activity. It seems like in this case, your Test class should only provide a method which will return the text you want to display as a string.

So something like

    public class MyActivity extends Activity
    {
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(...);

            Test t = new Test(...);

            String testText = t.getTestString();

            TextView tv = (TextView)findViewById(R.id.TestTextView);
            tv.setText(testText)
        }
    }

    public class Test
    {
        public String getTestString()
        {
            return "Test"; // Probably would be more dynamic in your case.
        }
    }

This will abstract the data knowledge out to the Test class. All your activity needs to know is that someone else is providing a data string, and it can set the text itself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜