简体   繁体   English

如何从另一个Activity更改LinerLayout扩展类中TextView的文本?

[英]How to change text of a TextView in a LinerLayout extended class from another Activity?

I have a class that extends LinearLayout. 我有一个扩展LinearLayout的类。 There is a TextView in that class. 该类中有一个TextView。 I need to change the text of that TextView from another activity. 我需要从另一个活动更改该TextView的文本。 I have tried by declaring the TextView object as public static. 我已经尝试通过将TextView对象声明为公共静态对象。 But it is not working. 但这是行不通的。 Please help me to do it. 请帮我做。

This is my Code: 这是我的代码:

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);

        }
    }

}

I am calling static ShowBalance() function from the other activity. 我正在从其他活动中调用静态ShowBalance()函数。

Thanks in advance. 提前致谢。

As far as I know it's not possible to change the text in one activity from another directly, because of the main thread (UI thread) restrictions. 据我所知,由于主线程(UI线程)的限制,不可能在一个活动中直接从另一个活动中更改文本。

Try to store the value you need later in a public variabile and then in the other Activity, in onResume() or onOpen() update the value of the editText with the stored value in the public variable. 尝试稍后将所需的值存储在公共变量中,然后在另一个Activity中,在onResume()或onOpen()中使用存储在公共变量中的值来更新editText的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM