title: How can i get item data when clicked widget ListView
body: <p>I created a <code>ListView</code> on homescreen widget but I clicked the item of <code>ListView</code> I can't get correct position data from <code>ListView</code>. How can I do it?</p>
  
  <p><strong>WidgetProvider.class</strong></p>
  
  <pre><code>public class WidgetProvider extends AppWidgetProvider {
  
      public static final String TOAST_ACTION = "com.mobistech.mobistechsmart.Widget.TOAST_ACTION";
      public static final String MERCHANT_ITEM = "com.mobistech.mobistechsmart.Widget.MERCHANT_ITEM";
  
      @Override
      public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                           int[] appWidgetIds) {
          final int N = appWidgetIds.length;
  
          for (int i = 0; i &lt; N; ++i) {
              RemoteViews remoteViews = updateWidgetListView(context,
                      appWidgetIds[i]);
              appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
          }
          super.onUpdate(context, appWidgetManager, appWidgetIds);
      }
  
  
  
          private RemoteViews updateWidgetListView(Context context, int appWidgetId) {
  
          //which layout to show on widget
          RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                  R.layout.widget_layout);
  
          Intent svcIntent = new Intent(context, WidgetService.class);
          svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
  
          svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
          remoteViews.setRemoteAdapter(appWidgetId, R.id.listViewWidget,
                  svcIntent);
          remoteViews.setEmptyView(R.id.listViewWidget, R.id.empty_view);
  
  
          //When item is clicked on ListView
  
  
  
          Intent startActivityIntent = new Intent(context, BroadCast.class);
          PendingIntent startActivityPendingIntent = PendingIntent.getBroadcast(context, 0, startActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
          remoteViews.setPendingIntentTemplate(R.id.listViewWidget, startActivityPendingIntent);
  
          return remoteViews;
      }
  
  }
  </code></pre>
  
  <p><strong>WidgetService.class</strong></p>
  
  <pre><code>public class WidgetService extends RemoteViewsService {
      /*
       * So pretty simple just defining the Adapter of the listview
       * here Adapter is ListProvider
       * */
  
      @Override
      public RemoteViewsFactory onGetViewFactory(Intent intent) {
          int appWidgetId = intent.getIntExtra(
                  AppWidgetManager.EXTRA_APPWIDGET_ID,
                  AppWidgetManager.INVALID_APPWIDGET_ID);
  
          return (new ListProvider(this.getApplicationContext(), intent));
      }
  }
  </code></pre>
  
  <p><strong>ListProvider.class</strong></p>
  
  <pre><code>public class ListProvider implements RemoteViewsFactory {
      private List&lt;Device&gt; listItemList = new ArrayList&lt;Device&gt;();
      private Context context = null;
      private int appWidgetId;
  
      public ListProvider(Context context, Intent intent) {
          this.context = context;
          appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                  AppWidgetManager.INVALID_APPWIDGET_ID);
  
          populateListItem();
      }
  
      private void populateListItem() {
          DatabaseHandler db=new DatabaseHandler(context);
          listItemList=db.getAllDevices();
      }
  
      @Override
      public int getCount() {
          return listItemList.size();
      }
  
      @Override
      public long getItemId(int position) {
          return position;
      }
  
      /*
       *Similar to getView of Adapter where instead of View
       *we return RemoteViews
       *
       */
      @Override
      public RemoteViews getViewAt(int position) {
  
          final RemoteViews remoteView = new RemoteViews(
                  context.getPackageName(), R.layout.list_row);
          Device device = listItemList.get(position);
          remoteView.setTextViewText(R.id.row_txt,device.getName());
          remoteView.setImageViewResource(R.id.row_image, context.getResources().getIdentifier(device.getIconName(), "drawable", context.getPackageName()));
  
  
          Intent fillInIntent = new Intent();
          fillInIntent.putExtra(WidgetProvider.TOAST_ACTION, position);
          remoteView.setOnClickFillInIntent(R.id.row_txt, 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() {
      }
  }
  </code></pre>