0

我正在使用 asynctask 来执行我的功能并在那里获取值。但是我的文本视图没有得到更新,尽管我的日志语句给了我结果。

我在这里没有使用自定义意图,只是基本的。

这是我的代码片段:

    public class ListViewWidget extends AppWidgetProvider{

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

        super.onUpdate(context, appWidgetManager, appWidgetIds);
        updateViews = new RemoteViews(context.getPackageName(), R.layout.list_layout);             
        thisWidget = new ComponentName(context, ListViewWidget.class);
        FetchTask fetchTask=new FetchTask();
        AppWidgetManager.getInstance(context.getApplicationContext());
        fetchTask.execute();

        }

//FetchTask

    public static class FetchTask extends AsyncTask<URL,Integer,String> implements ServerRequestEnvironment{

    protected String doInBackground(URL... arg0) {
    //logic part and stuff not entered

    //This is the end part that returns me the result, which is not getting printed to text view.

          String name="";
          int i = new Random().nextInt(27);
          storeObject=store.getStores().getItems().get(i).getStore();

          name= storeObject.getName();
          resultStuff(name);
          Log.i("StoreTag","storeval:"+name);   //returns name of the 0th item      

                return name;
    }//end of doInBackground() method

    protected void onPostExecute(String name) {
                // TODO Auto-generated method stub
          Intent intent=new Intent("android.appwidget.action.APPWIDGET_UPDATE");

          PendingIntent pendingIntent=PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
          updateViews.setTextViewText(R.id.text_view,resultStuff(name));
          updateViews.setOnClickPendingIntent(R.id.next, pendingIntent);

          AppWidgetManager manager = AppWidgetManager.getInstance(context.getApplicationContext());

          manager.updateAppWidget(thisWidget, updateViews);
    }//end of PostExecute

    }//End of async task class
    }//end of ListViewWidget class

我在这里缺少什么?请指导..

更新:这样好吗?要在 onReceive 和 onUpdate 中调用的异步任务?

@Override public void onReceive(Context context,Intent intent){

    if(intent.getAction().equals("android.appwidget.action.APPWIDGET_UPDATE")){
        fetchTask.execute();
    }
}

更新 2:

 public static String name="New text"

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

        super.onUpdate(context, appWidgetManager, appWidgetIds);
        updateViews = new RemoteViews(context.getPackageName(), R.layout.list_layout);

//-----------NEW LINE ADDED-------------
            updateViews.setTextViewText(R.id.text_view,name);
        thisWidget = new ComponentName(context, ListViewWidget.class);

        //Now in onPostExecute()
        //AppWidgetManager manager = AppWidgetManager.getInstance(context.getApplicationContext());
        //manager.updateAppWidget(thisWidget, updateViews);

        fetchTask.execute();

    }
protected void onPostExecute(String name) {
            // TODO Auto-generated method stub
            Intent intent=new Intent("android.appwidget.action.APPWIDGET_UPDATE");

            PendingIntent pendingIntent=PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            updateViews.setTextViewText(R.id.text_view,name);
            updateViews.setOnClickPendingIntent(R.id.next, pendingIntent);

            AppWidgetManager manager = AppWidgetManager.getInstance(context.getApplicationContext());
            manager.updateAppWidget(thisWidget, updateViews);
        }

@Dororo 没有得到你。请解释。我在课堂上创建了公共变量:

public static Context context;
        public static RemoteViews updateViews;
        public static ComponentName thisWidget;
        FetchTask fetchTask=new FetchTask(); 
4

3 回答 3

0

您是否尝试过使用它:

TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(toThis);
于 2013-06-19T21:02:02.853 回答
0

您确定在创建时没有覆盖更新的值new ComponentName(context, ListViewWidget.class);吗?onUpdate将在您调用时调用manager.updateAppWidget(thisWidget, updateViews);您不需要使用,您只需要在第一次创建对象后设置为static的初始化。booleantrue

于 2013-06-19T21:09:10.720 回答
0

解决了我的异常和错误。将上下文设置为空。我为应用程序上下文分配了上下文。

于 2013-06-20T00:07:28.900 回答