sma6871
sma6871

Reputation: 3298

Update Textview from internet with AsyncTask

I use AsynTask for get data from net and show in Textview, this is my code

public class PatientAssistant extends Activity {

    private TextView txtTemp,txtHuminity;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.patient_assistant);
        AppPreferences pref=new AppPreferences(this);

        TextView textCity=(TextView)findViewById(R.id.txtCity);

        txtTemp=(TextView)findViewById(R.id.txtTemp);
        txtHuminity=(TextView)findViewById(R.id.txtHumidity);

        textCity.setText(pref.getCity());

        String city=(String) textCity.getText();

        CallWeatherAsync cwa=new CallWeatherAsync(this,txtTemp,txtHuminity);

        cwa.execute(city);


        Button btnSend=(Button)findViewById(R.id.btnSend);
        btnSend.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //TODO send report

            }
        });
    }

    private class CallWeatherAsync extends AsyncTask<String, Void, WeatherInfo> {

        private PatientAssistant _activity;
        private TextView txtT,txtH;
        public CallWeatherAsync(PatientAssistant activity,TextView txtTmp,TextView txthum)
        {
            _activity=activity;
            txtT=txtTmp;
            txthum=txtH;
        }
        @Override
        protected WeatherInfo doInBackground(String... params) {

            YahooWeatherUtils yahooWeatherUtils = YahooWeatherUtils.getInstance();

            WeatherInfo weatherInfo = yahooWeatherUtils.queryYahooWeather(_activity.getApplicationContext(), params[0]);
            return weatherInfo;
        }
        @Override
        protected void onPostExecute(WeatherInfo result) {

            txtT.setText(result.getCurrentTempC());
            txtH.setText(result.getAtmosphereHumidity());
        }

    }

}

but this throws Resources$NotFoundException!!!

12-02 00:39:06.383: E/AndroidRuntime(4035): android.content.res.Resources$NotFoundException: String resource ID #0xc
12-02 00:39:06.383: E/AndroidRuntime(4035):     at android.content.res.Resources.getText(Resources.java:247)
12-02 00:39:06.383: E/AndroidRuntime(4035):     at android.widget.TextView.setText(TextView.java:3473)
12-02 00:39:06.383: E/AndroidRuntime(4035):     at com.assitant.patient.PatientAssistant$CallWeatherAsync.onPostExecute(PatientAssistant.java:116)
12-02 00:39:06.383: E/AndroidRuntime(4035):     at com.assitant.patient.PatientAssistant$CallWeatherAsync.onPostExecute

How I can do this?

Upvotes: 0

Views: 637

Answers (3)

funkmaster
funkmaster

Reputation: 9

Cheers ! , your problem should be with txthum=txtH; it should be txtH =txthum;

Upvotes: 1

thomas.cloud
thomas.cloud

Reputation: 963

You do not to set the TextView in the private class. Since you set the 2 TextViews as global variables for the PatientAssistant class you should just use them directly in the private AsyncClass.

Upvotes: 0

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

Blind guess is that:

txtT.setText(result.getCurrentTempC());
txtH.setText(result.getAtmosphereHumidity());

returns int not string. And if it is int then it is considered resource Id. This shall work:

txtT.setText( "" + result.getCurrentTempC());
txtH.setText( "" + result.getAtmosphereHumidity());

Upvotes: 2

Related Questions