Dynamic ArrayList object in Widget's ListView - android

I use this sample:
http://laaptu.wordpress.com/2013/07/19/android-app-widget-with-listview/
Everything is OK but I have two differences:
I use TimerTask and dynamic ArrayList.
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TasksView(context, appWidgetManager),100, 5000);
remoteViews = new RemoteViews(context.getPackageName(), R.layout.appwidget);
}
private class TasksView extends TimerTask
{
public TasksView(Context context, AppWidgetManager appWidgetManager){
remoteViews = new RemoteViews(context.getPackageName(), R.layout.appwidget);
thisWidget = new ComponentName(context, Widget.class);
}
#Override
public void run()
{
// SOME operation
}
public void updateListView()
{
// HERE I need updateListView
}
}
With delivery of the list of objects will not be a problem because I'll do it by batches. But how to call update again in AppWidgetProvider -> TasksView extends TimerTask

I don't get the exact point of your question, but if you just want to refresh a listview you can use :
ListView.invalidateViews()
or use method notifyDataSetChanged of your Adapter
Adapter.notifyDataSetChanged()

Related

notifyappwidgetviewdatachanged not working on a listview on my widget

I tried lots of solutions but after weeks i have not been able to solve this issue: why "notifyappwidgetviewdatachanged" doesn't work? how can i update a listview placed on my widget? Where am i wrong?
Here are my classes.
Widget Provider:
public class Widget_Provider extends AppWidgetProvider
{
public static final String ACTION_MOSTRAORARI = "fACTION_TOAST";
public static final String EXTRA_STRING = "EXTRA_STRING";
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds)
{
RemoteViews views = updateWidgetListView(context, appWidgetId);
final Intent onItemClick = new Intent(context, Widget_Provider.class);
onItemClick.setAction(ACTION_MOSTRAORARI);
onItemClick.setData(Uri.parse(onItemClick.toUri(Intent.URI_INTENT_SCHEME)));
final PendingIntent onClickPendingIntent = PendingIntent.getBroadcast(context, 0, onItemClick, PendingIntent.FLAG_UPDATE_CURRENT);
views.setPendingIntentTemplate(R.id.myStopList, onClickPendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
public RemoteViews updateWidgetListView(Context context, int appWidgetId)
{
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
Intent svcIntent = new Intent(context, Widget_Service.class);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter(R.id.myStopList, svcIntent);
return remoteViews;
}
#Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(ACTION_MOSTRAORARI)) {
if (MainUtils.isNewtworkAvailable(context))
{
String item = intent.getExtras().getString(EXTRA_STRING);
Intent intentOrari = new Intent(context, Diag_MostraOrari.class);
intentOrari.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intentOrari);
}
}
super.onReceive(context, intent);
}
#Override
public void onDeleted(Context context, int[] appWidgetIds) {}
#Override
public void onEnabled(Context context) {}
#Override
public void onDisabled(Context context) {}
}
Widget Service:
public class Widget_Service extends RemoteViewsService
{
#Override
public RemoteViewsFactory onGetViewFactory(Intent intent)
{
Map<String, ArrayList<String>> map = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
/* ---
* Here i fetch data from a local db and store it on "map"
*/ ---
return (new Widget_ListProvider(this.getApplicationContext(), intent, map));
}
}
ListProvider:
public class Widget_ListProvider implements RemoteViewsFactory
{
private Map<String, ArrayList<String>> map = new HashMap<>();
private ArrayList<ListItem_Widget> listItemList = new ArrayList<>();
private Context context = null;
private int appWidgetId;
public Widget_ListProvider(Context context, Intent intent, Map<String, ArrayList<String>> map)
{
this.map = map;
this.context = context;
appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
populateListItem();
}
//This function populate the arraylist "listItemList" by the data stored on "map"
private void populateListItem() { [...] }
#Override
public int getCount() {
return listItemList.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public RemoteViews getViewAt(int position)
{
final RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.listitem_widget);
ListItem_Widget listItem = listItemList.get(position);
remoteView.setTextViewText(R.id.heading, listItem.heading);
remoteView.setTextViewText(R.id.content, listItem.content);
final Intent fillInIntent = new Intent();
fillInIntent.setAction(Widget.ACTION_MOSTRAORARI);
final Bundle bundle = new Bundle();
bundle.putString(Widget.EXTRA_STRING, listItem.heading);
fillInIntent.putExtras(bundle);
remoteView.setOnClickFillInIntent(R.id.listrow, fillInIntent);
return remoteView;
}
#Override
public RemoteViews getLoadingView() {
return null;
}
#Override
public int getViewTypeCount() {
return 1;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public void onCreate() {}
#Override
public void onDataSetChanged() {}
#Override
public void onDestroy() {}
}
The xml of my listview custom item contains just two textviews: "heading" and "content".
Where am i wrong? why when i call "notifyappwidgetviewdatachanged" from another activity nothing happens?
[EDIT]
That's the activity where i need to update my widget.
public class Diag_Line extends AppCompatActivity
{
//[...]
#Override
protected void onCreate(Bundle savedInstanceState)
{
//[...]
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
ComponentName thisAppWidget = new ComponentName(getApplicationContext().getPackageName(), Widget.class.getName());
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.myStopList);
//"myStopList" is the id of my listview inside the widget xml.
//[...]
}
}
Well, the problem is a quite obvious: In your RemoteViewsFactory you have an empty method onDataSetChanged(). But when you're triggering notifyAppWidgetViewDataChanged(), you're getting callback in onDataSetChanged(). For better understanding check this pic.
If Diag_Line is a Configuration Activity just make something like this:
appWidgetManager.updateAppWidget(appWidgetIds, views); // views is a RemoteViews that you need to build
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.myStopList);
Also check official documentation here
UPDATE
Some hints for you. If you're working with widget, make all fetching data and fillings RemoteViews on background (IntentService is a nice approach). So just make IntentService and make logic for widget here. Don't forget to make him foreground (only for Android O), if you're triggering your updates by broadcasts. Also you can check library that makes all for you.
In order for notifyAppWidgetViewDataChanged() to work properly
we need to retreive the data in onDataSetChanged() method of RemoteViewSourceFactory
like this:
override fun onDataSetChanged() {
listOfArticles = newsDatabaseRepo.getSynchronouslySavedNews() as ArrayList<Articles>
}
as suggested in here
And have a method to refresh the widget if a change occured:
private fun refreshWidget() {
val appWidgetManager =
AppWidgetManager.getInstance(applicationContext)
val thisAppWidget = ComponentName(
applicationContext.packageName,
MyAppWidgetProvider::class.java.name
)
val appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget)
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.stack_view)
}
After calling the above method and once I come out of my App I am able to notice the changes in My Widget
So in order to check if onDataSetChanged is called after notifyAppWidgetViewDataChanged()
I kept a debug point and onDataSetChanged() was called and the list was updated as suggested in here here
Had a similar problem updating my widget, it was calling onDataSetChanged but not refreshing the layout. So in my case I had to set updateAppWidget to null before updating with remoteviews like this:
appWidgetManager.updateAppWidget(appWidgetId, null);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
Looks like there is some sort of caching happening here.

TextView not getting updated in android app widgets

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();
Have you tried using this:
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(toThis);
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.
Solved my exception and error. Was setting context to be null. I assigned context with the application context.

Android widget change update time

I'm using this code to schedule my widget
// Timer
Timer timer = new Timer();
// Schedule time
timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 60000);
private class MyTime extends TimerTask {
// RemoveViews
RemoteViews remoteViews;
// AppWidgetManager
AppWidgetManager appWidgetManager;
// ComponentName
ComponentName thisWidget;
public MyTime(Context context, AppWidgetManager appWidgetManager) {
this.appWidgetManager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(),
R.layout.main);
thisWidget = new ComponentName(context, WMTWidget.class);
}
#Override
public void run() {
//code to update textview
}
}
This updates a textview every minute which works fine
I'm wondering if it's possible to change this after the first run ?
it should update the textview the first time after 1 minute and then every 15 minutes
Is this possible?
Try This It may work
// Timer
int time=1; //Defined it global
Timer timer = new Timer();
// Schedule time
timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), time, 60000);
time=15; //update time for 15 minutes Now it will take 15 minute interval
private class MyTime extends TimerTask {
// RemoveViews
RemoteViews remoteViews;
// AppWidgetManager
AppWidgetManager appWidgetManager;
// ComponentName
ComponentName thisWidget;
public MyTime(Context context, AppWidgetManager appWidgetManager) {
this.appWidgetManager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(),
R.layout.main);
thisWidget = new ComponentName(context, WMTWidget.class);
}
#Override
public void run() {
//code to update textview
}
}

Set TextView in Handler And Thread Widget

How do I set TextView inside handlers?
public class DigitalClock extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
int N = appWidgetIds.length;
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.digitalclock);
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
Intent clockIntent = new Intent(context, DeskClock.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
clockIntent, 0);
views.setOnClickPendingIntent(R.id.rl, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
private static Handler mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
// update your textview here.
}
};
class TickThread extends Thread {
private boolean mRun;
#Override
public void run() {
mRun = true;
while (mRun) {
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
mHandler.sendEmptyMessage(0);
}
}
}
Im supposed to update the TextView here:
private static Handler mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
// update your textview here.
...
How do i do this? In the OnUpdate method i would use views.setTextViewText(R.id... but in the Handler RemoteViews doesnt exist. Ive tried everything I know and so far, nothing
Make a new one :) RemoteViews just attached to the remote entity and you pretty much queue up a bunch of changes that it makes when it is realized.
So when you do
appWidgetManager.updateAppWidget(appWidgetId, views);
That is when the RemoteViews actually do something.
I think the real problem is that the design being used is a little messy. So you have a thread, not sure where this gets started but it calls into a handler, this is fine, but you should probably send some structured data so the Handler knows what to do. RemoteViews instances themselves are Parcelable, which means they can be sent as part of the payload of things such as Intent and Message instances. The real problem with this design is that you can't call updateAppWidget without the AppWidgetManager instance to actually execute your changes.
You can either cache the AppWidgetManager for the lifetime of your widget or update the update frequency and move to more of a delayed queue worker. Where on next update event that you receive from the system, or a mixture of both.
private SparseArray<RemoteView> mViews;
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
....
for (int appWidgetId : appWidgetIds) {
RemoteViews v = mViews.get(appWidgetId);
if (v != null) {
appWidgetManager.updateWidget(appWidgetId, v);
} else {
enqueue(appWidgetManager, appWidgetId, new RemoteViews(new RemoteViews(context.getPackageName(),
R.layout.digitalclock)));
/* Enqueue would pretty much associate these pieces of info together
and update their contents on your terms. What you want to do is up
to you. Everytime this update is called though, it will attempt to update
the widget with the info you cached inside the remote view.
*/
}
}
}

Battery Widget in Android

Hi all I want to make a battery widget in android with animation. I think I will be able to animate but I want to know that how can I get the battery status again and again? Will it be OK to do it through thread? Or something else is required. Here is the simple code for animation. Please explain how will I get the data from battery again and again all the time.I know the functions but I don't know the mechanism to use them.
public class HelloWidget extends AppWidgetProvider{
RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName thisWidget;
ImageView img;
Bitmap icon, icon1;
int counter = 0;
#Override
public void onUpdate(Context context, final AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
this.appWidgetManager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
thisWidget = new ComponentName(context, HelloWidget.class);
remoteViews.setImageViewResource(R.id.imgv, R.drawable.icon2);
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
( new Thread()
{
public void run()
{
while(true)
{
if(counter%15>=0 && counter%15 <=7)
{
remoteViews.setImageViewResource(R.id.imgv, R.drawable.icon2);
}
else
{
remoteViews.setImageViewResource(R.id.imgv, R.drawable.icon);
}
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
counter++;
}
}
}
).start();
}
Register broadcast receiver for battery events and don't query every time.
IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
mContext.registerReceiver(batteryLevelReceiver, batteryLevelFilter);

Categories

Resources