TextView没有在android应用部件中得到更新

我正在使用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(); 

你有没有尝试过使用这个:

TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(toThis);

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


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

链接地址: http://www.djcxy.com/p/50619.html

上一篇: TextView not getting updated in android app widgets

下一篇: android widget stop update the code after few hours?