title: Android app widget listview onclick listener
body: <p>I am new to android development. I am trying to make a widget for my app which will contain a listview, with each listview having 2 buttons apart from heading and content.</p>
  
  <p>I am trying to hide the corresponding element of the listitem in the app widget(on homescreen) when the button of that element is pressed.</p>
  
  <p>First I tried to check if the button click is getting any response and if I could detect if I can get the position of the item. So I did this:</p>
  
  <p><strong>listprovider.java (implements remoteviewfactory)</strong> :</p>
  
  <pre><code>@Override
      public RemoteViews getViewAt(int position) {
          final RemoteViews remoteView = new RemoteViews(
                  context.getPackageName(), R.layout.list_row);
          ListItem listItem = listItemList.get(position);
          remoteView.setTextViewText(R.id.heading, listItem.heading);
          remoteView.setTextViewText(R.id.content, listItem.content);
  
          Bundle extras = new Bundle();
          extras.putInt(WidgetProvider.EXTRA_ITEM, position);
          Intent fillInIntent = new Intent();
          fillInIntent.putExtras(extras);
          // Make it possible to distinguish the individual on-click
          // action of a given item
          remoteView.setOnClickFillInIntent(R.id.buttonwidget, fillInIntent);
  
          return remoteView;
      }
  </code></pre>
  
  <p>and then in the <strong>WidgetProvider.java (extends AppWidgetProvider)</strong> :</p>
  
  <pre><code>for (int i = 0; i &lt; N; ++i) {
              RemoteViews remoteViews = updateWidgetListView(context,
                      appWidgetIds[i]);
              Intent clickIntent = new Intent(context, MainActivity.class);
              clickIntent.setAction(WidgetProvider.TOAST_ACTION);
              clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
              clickIntent.setData(Uri.parse(clickIntent.toUri(Intent.URI_INTENT_SCHEME)));
              PendingIntent clickPI = PendingIntent.getActivity(context, 0,
                      clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  
              remoteViews.setPendingIntentTemplate(com.example.markup.R.id.listViewWidget, clickPI);
              appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
          }
          super.onUpdate(context, appWidgetManager, appWidgetIds);
  </code></pre>
  
  <p>The <strong>mainActivity.java</strong> is the default activity of the program, where I did this to get the item position:</p>
  
  <pre><code>public class MainActivity extends Activity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          //Printing detail of clicked item from widget
          Intent intent = getIntent();
          if (intent.getAction().equals(WidgetProvider.TOAST_ACTION)) {
              int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                  AppWidgetManager.INVALID_APPWIDGET_ID);
              int viewIndex = intent.getIntExtra(WidgetProvider.EXTRA_ITEM, 0);
              Toast.makeText(getApplicationContext(), "Touched view " + viewIndex, Toast.LENGTH_SHORT).show();
          }else{
              Toast.makeText(getApplicationContext(), "Touched view none ", Toast.LENGTH_SHORT).show();
          }
      }
  </code></pre>
  
  <p>and I am thus able to open the main activity on a button click and getting a TOAST text in the bottom, giving exact result. </p>
  
  <p>But I wish to hide/disable the corresponding element of the app widget when clicked. What should I do for that?</p>