我有一个扩展LinearLayout的类。该类中有一个TextView。我需要从另一个活动更改TextView的文本。我已经尝试将TextView对象声明为公共静态。但它没有用。请帮我做。
这是我的代码:
package com.briscommunications.bris.ui;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.briscommunications.bris.R;
import com.briscommunications.bris.GlobalVariables;
import com.briscommunications.bris.LinphonePreferences;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Timer;
import java.util.TimerTask;
/**
* @author Guillaume Beraudo
*
*/
public class Numpad extends LinearLayout implements AddressAware {
private boolean mPlayDtmf;
public void setPlayDtmf(boolean sendDtmf) {
this.mPlayDtmf = sendDtmf;
}
public static Handler handler = new Handler();
Timer timer = new Timer();
public static TextView txtBalance;
public static Context objNumpadContext;
public static Activity objNumpadAct;
public Numpad(Context context, boolean playDtmf) {
super(context);
mPlayDtmf = playDtmf;
LayoutInflater.from(context).inflate(R.layout.numpad, this);
setLongClickable(true);
onFinishInflate();
}
public static void ShowBalance(){
try {
LinphonePreferences mPrefs = LinphonePreferences.instance();
int defaultAccountID = mPrefs.getDefaultAccountIndex();
GlobalVariables.UserName = mPrefs.getAccountUsername(defaultAccountID);
}
catch (Exception e){
GlobalVariables.UserName ="";
}
new getBalance_actMain().execute();
}
public Numpad(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Numpad);
mPlayDtmf = 1 == a.getInt(R.styleable.Numpad_play_dtmf, 1);
a.recycle();
LayoutInflater.from(context).inflate(R.layout.numpad, this);
setLongClickable(true);
txtBalance = (TextView) findViewById(R.id.txtBalance);
objNumpadContext = getContext();
objNumpadAct = (Activity) objNumpadContext;
ShowBalance();
/*
TimerTask doAsynchronousTask = new TimerTask() {
@Override
public void run() {
//Perform background work here
handler.post(new Runnable() {
public void run() {
}
});
}
};
timer.schedule(doAsynchronousTask, 10, 5000);*/
}
@Override
protected final void onFinishInflate() {
for (Digit v : retrieveChildren(this, Digit.class)) {
v.setPlayDtmf(mPlayDtmf);
}
super.onFinishInflate();
}
public void setAddressWidget(AddressText address) {
for (AddressAware v : retrieveChildren(this, AddressAware.class)) {
v.setAddressWidget(address);
}
}
private final <T> Collection<T> retrieveChildren(ViewGroup viewGroup, Class<T> clazz) {
final Collection<T> views = new ArrayList<T>();
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View v = viewGroup.getChildAt(i);
if (v instanceof ViewGroup) {
views.addAll(retrieveChildren((ViewGroup) v, clazz));
} else {
if (clazz.isInstance(v))
views.add(clazz.cast(v));
}
}
return views;
}
public static class getBalance_actMain extends AsyncTask {
String usrBal="Fetching Balance";
@Override
protected Object doInBackground(Object[] params) {
try {
HttpClient client=new DefaultHttpClient();
HttpGet request=new HttpGet("some_url");
HttpResponse response=client.execute(request);
HttpEntity entity=response.getEntity();
usrBal = EntityUtils.toString(entity);
entity.consumeContent();
} catch (ClientProtocolException e) {
usrBal = "FALSE";
} catch (IOException e) {
usrBal = "FALSE";
}
return null;
}
@Override
protected void onPostExecute(Object o) {
if (usrBal.equals("FALSE")){
txtBalance.setText("--.--");
return;
}
//txtBalance.setText(usrBal);
com.briscommunications.bris.ui.Numpad.txtBalance.setText(usrBal);
}
}
}
我从其他活动调用静态ShowBalance()函数。
提前致谢。
答案 0 :(得分:0)
据我所知,由于主线程(UI线程)限制,不可能直接将一个活动中的文本从另一个活动中更改。
尝试将稍后需要的值存储在公共变量中,然后在另一个Activity中,在onResume()或onOpen()中使用公共变量中存储的值更新editText的值。