How to recreate app widget in android

319 Views Asked by At

I'm writting "Time table app" which have to show today lessons as a listView in app widget.

The all data (for all week days) are stored in one file called Data. The app widget get Data onCreate and then fillter them to get data for current day.

But what should i do when i add new lesson to the data file? Is there any way to recreate app widget, to get updated data?

Sorry for my bad English. Thanks for Yours answers

The code of my RemoteViewsFactory is below:

public class ViewsFactory implements RemoteViewsService.RemoteViewsFactory {
    List data;
    int day;
    private Context ctxt;
    private int appWidgetId;

    public ViewsFactory(Context ctxt, Intent intent) {
        this.ctxt=ctxt;
        appWidgetId=intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
    }

    @Override
    public void onCreate() {
        File file = new File(ctxt.getFilesDir(),"default.txt");

            if(file.exists()){

                ReadFile readFile = new ReadFile(file);
                data = readFile.read();

                Collections.sort(data, new AlphanumComparator());

            }
        }



    @Override
    public void onDestroy() {
        // no-op
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public RemoteViews getViewAt(int i) {
        RemoteViews row=new RemoteViews(ctxt.getPackageName(), R.layout.row2);
        SharedPreferences settings = ctxt.getSharedPreferences("UserPref", 0);
        if(i==0) { //get week day only once
            Calendar calendar = Calendar.getInstance();
            day = calendar.get(calendar.DAY_OF_WEEK);
            if (day == 1) day = 7;
            else day -= 1;

            SharedPreferences.Editor editor = settings.edit();
            editor.putInt("currentDay", (day - 1)); //save current day for heading
            editor.commit();
        }
        if(settings.getString("Days","").contains(Integer.toString(day-1))&&
           data.get(i).toString().startsWith(Integer.toString(day))&&
           (data.get(i).toString().endsWith(settings.getString("OddEven",""))||data.get(i).toString().endsWith(",X1X2"))) { //filtering


            String[] dataLine = data.get(i).toString().split(",");
            row.setTextViewText(R.id.text1, dataLine[1]); //set texsts
            row.setTextViewText(R.id.text3, dataLine[2]);
            row.setTextViewText(R.id.text4, dataLine[4]);
            row.setTextViewText(R.id.text5, dataLine[6]);


        }
        return(row);
    }

    @Override
    public RemoteViews getLoadingView() {
        return(null);
    }

    @Override
    public int getViewTypeCount() {
        return(1);
    }

    @Override
    public long getItemId(int position) {
        return(position);
    }

    @Override
    public boolean hasStableIds() {
        return(true);
    }

    @Override
    public void onDataSetChanged() {
        // no-op
    }
}
1

There are 1 best solutions below

0
On
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        int appWidgetIds[] = appWidgetManager.getAppWidgetIds(
                                   new ComponentName(context, YourWidgetProviderClass.class));
        appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.<YourListView>);

add these lines when you are adding new record