Matt Jameson
Matt Jameson

Reputation: 582

findViewById() not working when using a seperate class

Hi I am new to Android

I would like keep the following code seperatly from my MainActivity file. However, when I try to findViewById() inside the seperate class, I get the error

"cannot be resolved"

Now I know I cannot extend the MainActivity class as it will lead to stack overflow, but could someone tell me how to go about access a textview from this seperate file?

public class Counter {

    private TextView tempTextView;
    //Temporary TextView
    private Button tempBtn;
    public Handler mHandler = new Handler();
    public long startTime;
    public long elapsedTime;
    public final int REFRESH_RATE = 100;
    public String hours,minutes,seconds,milliseconds;
    public long secs,mins,hrs,msecs;
    public boolean stopped = false;

    public Runnable startTimer = new Runnable() {
        public void run() {
            elapsedTime = System.currentTimeMillis() - startTime;
            updateTimer(elapsedTime);
            mHandler.postDelayed(this,REFRESH_RATE);
        }
    };


    private void updateTimer (float time) {
        secs = (long) (time / 1000);
        mins = (long) ((time / 1000) / 60);
        hrs = (long) (((time / 1000) / 60) / 60); /* Convert the seconds to String * and format to ensure it has * a leading zero when required */
        secs = secs % 60;
        seconds = String.valueOf(secs);
        if (secs == 0) {
            seconds = "00";
        }
        if (secs < 10 && secs > 0) {
            seconds = "0" + seconds;
        } /* Convert the minutes to String and format the String */
        mins = mins % 60;
        minutes = String.valueOf(mins);
        if (mins == 0) {
            minutes = "00";
        }
        if (
                mins < 10 && mins > 0
                ) {
            minutes = "0" + minutes;
        } /* Convert the hours to String and format the String */
        hours = String.valueOf(hrs);
        if (hrs == 0) {
            hours = "00";
        }
        if (hrs < 10 && hrs > 0) {
            hours = "0" + hours;
        }
        /* Although we are not using milliseconds on the timer in this example * I included the code in the event that you wanted to include it on your own */
        milliseconds = String.valueOf((long) time);
        if (milliseconds.length() == 2) {
            milliseconds = "0" + milliseconds;
        }
        if (milliseconds.length() <= 1) {
            milliseconds = "00";
        }
        milliseconds = milliseconds.substring(milliseconds.length() - 3, milliseconds.length() - 2);
        /* Setting the timer text to the elapsed time */
       // ((TextView) findViewById(R.id.elapsed_value)).setText(hours + ":" + minutes + ":" + seconds);
        //  ((TextView)findViewById(R.id.timerMs)).setText("." + milliseconds); }

    }
}

Upvotes: 1

Views: 3119

Answers (4)

Lady_ari
Lady_ari

Reputation: 533

You can have a constructor where you set an activity variable.

public Counter (MainActivity activity) {
    mActivity = activity;
}

Then, you can call

mActivity.findViewById(...);

which will find the view in your MainActivity layout that you set in

setContentView(<layout>);

Upvotes: 1

ci_
ci_

Reputation: 8774

You could create a constructor for Counter that takes a reference to your TextView from your Activity.

public Counter(TextView tv) {
    tempTextView = tv;
}

Then call findViewById() on your MainActivity as pass in the result when you're instantiating Counter.

You're already on the UI thread, no need for AsyncTask here...

Upvotes: 1

Bubu
Bubu

Reputation: 1543

You need to inflate a view and call the mehod findViewById() from this view. To inflate a view, you need a Context (you can set it in a custom constructor)

View view; 
LayoutInflater inflater = (LayoutInflater)   yourContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
view = inflater.inflate(R.layout.mylayout, null);

tempTextView = (TextView) view.findViewById(R.id.tv);

Upvotes: 2

user370305
user370305

Reputation: 109257

Because, findViewById() is method from Activity class. That's why you can not use it in any other Java class.

And second you can not update Android application UI from any worker thread.

So better to use AsyncTask and pass the TextView's references which you want to update after certain task.

Or use runOnMainUiThread() if you are going to update Application UI from other worker thread. But Be sure runOnUiThread() only works with Activity Context.

Upvotes: 1

Related Questions