我在主屏幕小部件上创建了ListView
,但我点击了ListView
的项目,我无法从ListView
获取正确的位置数据。我该怎么办?
WidgetProvider.class
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 < 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;
}
}
WidgetService.class
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));
}
}
ListProvider.class
public class ListProvider implements RemoteViewsFactory {
private List<Device> listItemList = new ArrayList<Device>();
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() {
}
}
答案 0 :(得分:0)
ListView.setOnItemClickListener(new SingleItemClickListener() {
@Override
public void onItemClicked(AdapterView<?> parent, View view, int position, long id) {
}
});
此功能的位置字段是所需的全部