【问题标题】:Assigning a string to a text value in an async task called from AppWidgetProvider在从 AppWidgetProvider 调用的异步任务中将字符串分配给文本值
【发布时间】:2014-01-14 13:55:18
【问题描述】:

我正在尝试通过异步任务在小部件中设置 textview 值。我创建了一个简单的小部件,其布局如下所示:

widget_test.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/widget_bg_normal"
    >


    <TextView
        android:id="@+id/widget_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="testing 123"
        android:layout_gravity="center_horizontal|center"
        android:layout_marginTop="5dip"
        android:padding="10dip"
        android:textColor="@android:color/black"
        >
    </TextView>

</LinearLayout>

我的 AppWidgetProvider 代码是:

package com.example.beerportfoliopro;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;

/**
 * Created by Mike on 12/26/13.
 */
public class HelloWidget extends AppWidgetProvider {

    @Override
    public void onEnabled(Context context){

        //build url for async task
        String url = "hiddenURL";

        //call async task
        new GetRandomBeer(context).execute(url);



    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){

        //todo: call update code, which should be the same as onEnabled


    }


}

上面的代码应该控制启用应用程序时发生的情况(也就是当用户将应用程序添加到他们的主屏幕时)以及应用程序在根据我的 hello_widget-provider.xml 中设置的时间进行自我更新时应该执行的操作。 :

<?xml version="1.0" encoding="utf-8"?>

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="146dip"
    android:minHeight="72dip"
    android:updatePeriodMillis="1000"
    android:initialLayout="@layout/widget_test"
    >

</appwidget-provider>

出于测试目的,我没有让我的异步任务解析任何 json,但我只是试图通过异步任务更改我的小部件上的文本值。这是我的异步任务的代码:

package com.example.beerportfoliopro;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.beerportfolio.beerportfoliopro.R;

public class GetRandomBeer extends AsyncTask
        <String, Void, String> {

    Context c;

    public GetRandomBeer(Context context)
    {
        c = context;

    }

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        return readJSONFeed(arg0[0]);
    }



    protected void onPostExecute(String result){
        Log.d("taste","Inside get taste");
        //decode json here
        try{

            //todo: get all beer data



            //todo: set text views with data
            TextView breweryTitle = (TextView) ((Activity) c).findViewById(R.widget_tv);




        }
        catch(Exception e){

        }


    }

    public String readJSONFeed(String URL) {
        StringBuilder stringBuilder = new StringBuilder();
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(URL);
        try {
            HttpResponse response = httpClient.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                InputStream inputStream = entity.getContent();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(inputStream));
                String line;
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line);
                }
                inputStream.close();
            } else {
                Log.d("JSON", "Failed to download file");
            }
        } catch (Exception e) {
            Log.d("readJSONFeed", e.getLocalizedMessage());
        }
        return stringBuilder.toString();
    }

}

我现在的问题是我的 widget_tv 现在显示错误:

cannot resolve symbol widget_tv

【问题讨论】:

    标签: android android-asynctask android-widget android-xml


    【解决方案1】:

    您的 TextView ID 应该是:R.id.widget_tv 而不是 R.widget_tv

    【讨论】:

    • 愚蠢的错误...谢谢。一旦允许我在几分钟内接受你的回答,我就会接受。现在我试图弄清楚为什么当它被添加到主屏幕时它不会改变文本视图的值。但如果您有兴趣,我会单独提出一个问题。
    • @Mike 是的,请这样做,我会尝试调查一下。
    • 这是我的下一个问题,谢谢:stackoverflow.com/questions/20792944/…
    【解决方案2】:

    在执行异步任务之前,您是否在传递给异步任务的活动类中调用了 setContentView(view)?如果不是,你应该使用布局充气器来充气 textview

    ((Activity) c).getLayoutInflater().inflate(R.id.widget_tv, null); 
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-15
    • 2021-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-20
    相关资源
    最近更新 更多