Note that there are some explanatory texts on larger screens.

plurals
  1. POhow use button in homescreen widget like an activity
    text
    copied!<p>I have a widget with button , text View and DataBase. the data base already have some data the textView show the first data from database when I click on the button I want the text view changed and show the next data for example my DB is like this : </p> <pre><code> {(id=1,name="First")(id=2,name="second")(id=3,name="third")} textview="First" </code></pre> <p>after click on button</p> <pre><code> textview="second" </code></pre> <p>this is my code</p> <pre><code>@Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.onUpdate(context, appWidgetManager, appWidgetIds); for(int i=0;i&lt;appWidgetIds.length;i++){ int appWidgetId=appWidgetIds[i]; ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;(); DataBaseHelper db = new DataBaseHelper(context); RemoteViews(context.getPackageName(),R.layout.activity_main); RemoteViews rmtview = new RemoteViews(context.getPackageName(),R.layout.widget); list=db.getData(1); String s=list.get(GET) list=db.getData(1); rmtview.setTextViewText(R.id.textView1, s); db.close(); </code></pre> <p><strong>EDIT:</strong></p> <p>appwidet provider</p> <pre><code>public class widget extends AppWidgetProvider { public static String MyACTION = "MyAction"; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // TODO Auto-generated method stub super.onUpdate(context, appWidgetManager, appWidgetIds); RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget); remoteViews.setOnClickPendingIntent(R.id.button3, buildButtonPendingIntent(context)); for(int i=0;i&lt;appWidgetIds.length;i++){ int appWidgetId=appWidgetIds[i]; ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;(); DataBaseHelper db = new DataBaseHelper(context); RemoteViews rmtview = new RemoteViews(context.getPackageName(),R.layout.widget); list=db.getData(1); String s=list.get(0); rmtview.setTextViewText(R.id.textView1, s); db.close(); /////////////////////// /////////////////////// appWidgetManager.updateAppWidget(appWidgetId, rmtview); // RemoteViews un = new RemoteViews(context.getPackageName(),R.layout.widget); } } public static PendingIntent buildButtonPendingIntent(Context context) { Log.d("me","pending entent launched"); Intent intent = new Intent(); intent.setAction("pl.looksok.intent.action.CHANGE_TEXT"); return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); } public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) { ComponentName myWidget = new ComponentName(context, widget.class); Log.d("me","push launched"); AppWidgetManager manager = AppWidgetManager.getInstance(context); manager.updateAppWidget(myWidget, remoteViews); } } </code></pre> <p>MyWidgetIntentReceiver.java</p> <pre><code>public class MyWidgetIntentReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.d("me", "OnRecive ..."); if(intent.getAction().equals("pl.looksok.intent.action.CHANGE_TEXT")){ updateWidgetPictureAndButtonListener(context); } } public static void updateWidgetPictureAndButtonListener(Context context) { Log.d("me", "update widget... "); RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget); // ..Get Text from DB // remoteViews.setTextViewText(R.id.textView1, "...text"); //REMEMBER TO ALWAYS REFRESH YOUR BUTTON CLICK LISTENERS!!! remoteViews.setOnClickPendingIntent(R.id.button3, widget.buildButtonPendingIntent(context)); widget.pushWidgetUpdate(context.getApplicationContext(), remoteViews); } } </code></pre> <p><strong>Edit2</strong></p> <p>I removed every things </p> <pre><code> public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // TODO Auto-generated method stub super.onUpdate(context, appWidgetManager, appWidgetIds); RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget); remoteViews.setOnClickPendingIntent(R.id.button3, buildButtonPendingIntent(context)); appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload