How to Update a Textview of an Activity from Another Class

How to update a TextView of an Activity from another class

You have to pass the Context reference via constructor.

public class ClassB {
Context context;
public ClassB(Context context){
this.context=context;
}

public void Update(){
TextView txtView = (TextView) ((Activity)context).findViewById(R.id.text);
txtView.setText("Hello");
}

Cannot update TextView from another class

You have to pass the Context reference via constructor:

public Bill(Context context){
this.context=context;
//rest of your code...

}

Also find the TextView like this:

TextView billText = (TextView) ((Activity)context).findViewById(R.id.useless);

Accessing TextView from another class

If I were you I'd keep all your UI updates within your main Activity class. You just get your DBWork class to return the text you want displayed to your Activity. So something like:

public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) view.findViewById(R.id.TextView01);
DBWork db = new DBWork("192.168.2.14:8080/DH/DPost";, "test_db");
tv.setText(db.getText());
}
}

Then your db class:

public class DBWork{
...
public String getText(){
//do stuff
return "";
}
}

Please note that any database operations you perform during onCreate should be as quick as possible as they are running in the UI thread. If you are going to perform long running database queries then you should use an AsyncTask or create a new Thread and use a Handler to update the UI.

Android - how to update textview from another class

You can achieve your goal with BroadcastReceiver, here is the sample code:

Phone Side

    public MainActivity extends Activity {

...

public static final String ACTION_TEXT_CHANGED = "changed";

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
sensorList = (TextView) findViewById(R.id.sensorListView);
sensorList.setText("\n" + "On create");
...
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, new IntentFilter(ACTION_TEXT_CHANGED));
}

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String content = intent.getStringExtra("content");
sensorList.setText("");
}
};

}

public class ListenerService extends WearableListenerService {

...

public static final String ACTION_TEXT_CHANGED = "changed";

...

@Override
public void onMessageReceived(MessageEvent messageEvent) {

...
retrieveMessage(message);
...
}

private void retrieveMessage(String message) {
Intent intent = new Intent();
intent.setAction(ACTION_TEXT_CHANGED);
intent.putExtra("content", message);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}

Two options to update a TextView of an Activity from another class via TimerTask

The basic thumb rule in android is that you can update the views only on the main thread, thus you get the exception

 09-30 20:08:29.635: W/System.err(14515): android.view.ViewRoot$CalledFromWrongThreadException:      Only the original thread that created a view hierarchy can touch its views.

Which tells that you are accessing the view elements from non UI thread. So in order to make changes to view from non UI thread, refer to this website and read the thread section. Its all you need..
http://developer.android.com/guide/components/processes-and-threads.html

Updating TextView from another Activity

You can create observable like:

in your Adapter create:

public interface IMyCallback {
void onDataChanged(int gameTime);
}

Global field in Adapter:

private IMyCallback callback

Inside constructor of Adapter

public MyAdapter(... , IMyCallback callback) {
...
this.callback = callback;
}

Now your activity make implements IMyCallback and override method onDataChanged.
And when you create your adapter send as callback YourActivity.this.

finnaly you need to make like:

    add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
callback.onDataChanged(Integer.valueOf(gameItem.getTime()));
}

after this call you will get update in your override method onDataChanged in your activity class. enjoy

------------ Update -----------
in your activity, when you will implements interface you will get method like

     @Override
public void onDataChanged(int gameTime) {
// it will fire when you call your callback.onDataChanged(); in your adapter
// here can change your textview
textview.setText(String.valueOf(gameTime));
}


Related Topics



Leave a reply



Submit