Skip to content Skip to sidebar Skip to footer

Asyntask With Remoteviews In Stackview Widget

I've tried to search for information about StackView Widget and i've found a lot , but nobody used to get data from url . So the problem is , while i'm processing url , my RemoteVi

Solution 1:

You can use the method onDataSetChanged in class RSSRemoteViewsFactory. This is triggered when you call AppWidgetManager notifyAppWidgetViewDataChanged on the collection view corresponding to this factory. You can do heaving lifting in here, synchronously. For example, if you need to process an image, fetch something from the network, etc., it is ok to do it here, synchronously. The widget will remain in its current state while work is being done here, so you don't need to worry about locking up the widget.

classStackRemoteViewsFactoryimplementsRemoteViewsService.RemoteViewsFactory {
privatefinalStringTAG= StackRemoteViewsFactory.class.getSimpleName();

private Context mContext;
privateint mAppWidgetId;

private List<Game> mGameItems = newArrayList<Game>();

publicStackRemoteViewsFactory(Context context, Intent intent) {
    mContext = context;
    mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
            AppWidgetManager.INVALID_APPWIDGET_ID);
}

publicvoidonCreate() {
}

publicvoidonDestroy() {
    mGameItems.clear();
}

publicintgetCount() {
    return mGameItems.size();
}

public RemoteViews getViewAt(int position) {
    Gamegame= mGameItems.get(position);

    Bundleextras=newBundle();
    extras.putString(StackWidgetProvider.EXTRA_ITEM, game.getGameUrl());
    IntentfillInIntent=newIntent();
    fillInIntent.putExtras(extras);
    rv.setOnClickFillInIntent(R.id.widget_item, fillInIntent);


    Bitmapbitmap=null;
    try {
        URLimageUrl=newURL(game.getImageUrl());
        bitmap = BitmapFactory.decodeStream(imageUrl.openConnection().getInputStream());
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    rv.setImageViewBitmap(R.id.widget_item, bitmap);
    return rv;
}

public RemoteViews getLoadingView() {
    returnnull;
}

publicintgetViewTypeCount() {
    return1;
}

publiclonggetItemId(int position) {
    return position;
}

publicbooleanhasStableIds() {
    returntrue;
}

publicvoidonDataSetChanged() {

    finalStringuri= <set your url>

    DefaultHttpClienthttpClient=newDefaultHttpClient();
    HttpGethttpGet=newHttpGet(uri);

    StringjsonData=null;

    /**
     * Fetch JSON from web service
     * TODO: try to call a AsyncTask
     */try {
        HttpResponsehttpResponse= httpClient.execute(httpGet);
        HttpEntityhttpEntity= httpResponse.getEntity();
        InputStreaminputStream= httpEntity.getContent();

        BufferedReaderbufferedReader=newBufferedReader(newInputStreamReader(inputStream));
        StringBuilderdata=newStringBuilder();
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            data.append(line);
        }
        jsonData = newString(data);
        Log.d(TAG, String.format("JSON: %s", jsonData));

    } catch (IOException e) {
        e.printStackTrace();
    }

    /**
     * Mapping JSON String to objects
     * TODO: Probably move the code to a dedicated class and use GSON to do the mapping?????
     */if(jsonData != null && !jsonData.equals("")) {
        try {
            JSONArrayjsonArray=newJSONArray(jsonData);
            for(intindex=0; index < jsonArray.length(); index++) {
                JSONObjectjsonObject= jsonArray.getJSONObject(index);
                Stringid= jsonObject.getString("id");
                StringimageUrl= jsonObject.getString("imageUrl");
                StringgameUrl= jsonObject.getString("gameUrl");

                mGameItems.add(
                        newGame(id, imageUrl, gameUrl)
                );
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    Log.d(TAG, String.format("List of games: %s", mGameItems));
}

}

In the following example, I'm fetching a json from a webserver and then I fetch the images from each item. The onDataSetChanged is used to fetch the json content and the in the getViewAt I fetch the image.

Check the comments at the StackView example provided by Google - http://docs.huihoo.com/android/3.0/resources/samples/StackWidget/index.html

Hope it helps you

Post a Comment for "Asyntask With Remoteviews In Stackview Widget"