0

I am using asynctask to do my feature function and getting the values there. But my text view is not getting updated, though my log statements giving me the result.

I am not using custom intent here, just the basic one.

Here are my code snippets:

    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

What I am missing here? Please please guide..

UPDATE: Is this fine? Async task to be called both in onReceive and onUpdate??

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

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

Update 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 Didn't get you. Please explain. I have created public var in my class:

public static Context context;
        public static RemoteViews updateViews;
        public static ComponentName thisWidget;
        FetchTask fetchTask=new FetchTask(); 
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Atihska
  • 4,803
  • 10
  • 56
  • 98
  • Is the intent received? Can you post the code of the receiving end? – Saren Inden Jun 19 '13 at 21:01
  • I haven't written onReceive() method as I am not getting what to call there out of these methods above. DO onReceive() needs to called for every intent even for this android.appwidget.action.APPWIDGET_UPDATE? – Atihska Jun 19 '13 at 21:03

3 Answers3

0

Have you tried using this:

TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(toThis);
KingsInnerSoul
  • 1,373
  • 4
  • 20
  • 49
  • I am using android app widget. It does not use findViewById. It has REmote Views and its set of calls. – Atihska Jun 19 '13 at 21:06
0

Are you sure you're not overwriting the updated value when you create new ComponentName(context, ListViewWidget.class);? onUpdate will be called when you call manager.updateAppWidget(thisWidget, updateViews); You shouldn't need to use static, you just need an initialisation boolean which is set to true after you've created the objects for the first time.

Dororo
  • 3,420
  • 2
  • 30
  • 46
  • resultStuff(name) is getting the value in my debug mode. So do I need to use onReceive. I am setting Intent's Action in onPostExecute(). And textView.invalidate(), should I put before setting my textView ?? – Atihska Jun 19 '13 at 21:14
  • Are you sure you aren't getting into a loop? Shouldn't `android.appwidget.action.APPWIDGET_UPDATE` call `onUpdate()` which will reset your view (because you create a `new` one)? – Dororo Jun 19 '13 at 21:19
  • Didn't get you. Please explain. I have created public var in my class for RemoteViews object and ComponentName. – Atihska Jun 19 '13 at 21:31
  • After updating your text view in `onPostExecute(String name)`, you need to update the app widget by calling the code in the answer. – Dororo Jun 19 '13 at 21:34
  • I am updating in my onPostExecute..check above..Overwriting how? – Atihska Jun 19 '13 at 21:39
  • Yes but surely what you are doing is `onUpdate` -> `FetchTask.doInBackground` -> `FetchTask.onPostExecute` -> UPDATE -> `updateAppWidget` -> `onUpdate` -> remaking the view (so it will be set to the default value) – Dororo Jun 19 '13 at 21:42
  • In `onUpdate`, set the text view to be the value of a public var called `name` (set it to "unknown" or something initially). When you update in `onPostExecute` just set the `name` var and call `updateAppWidget` – Dororo Jun 19 '13 at 21:46
  • I did what you told but not getting updated..Even the name value that's default in .java class not printed but the one in the layout file. – Atihska Jun 19 '13 at 22:08
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32043/discussion-between-atihska-and-dororo) – Atihska Jun 19 '13 at 22:27
0

Solved my exception and error. Was setting context to be null. I assigned context with the application context.

Atihska
  • 4,803
  • 10
  • 56
  • 98