我是Kotlin的新手,正在尝试制作对象列表的副本。我遇到的问题是,当我更改新副本中的项目时,旧列表也会被更改。这是对象:
class ClassA(var title: String?, var list: ArrayList<ClassB>, var selected: Boolean)
class ClassB(val id: Int, val name: String)
Run Code Online (Sandbox Code Playgroud)
我尝试这样做,但是不起作用:
val oldList:ArrayList<ClassA>
val newList :ArrayList<ClassA> = ArrayList()
newList.addAll(oldList)
Run Code Online (Sandbox Code Playgroud) 当我尝试更新应用程序中的内容时,我的App Widget出现问题.我的小部件包含列表视图.
listview工作正常,每隔30分钟更新一次,但是当我在应用程序中进行更改时,我还需要更新它.这是我的代码:
public class WidgetListProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
ComponentName component;
for (int i = 0; i < N; ++i) {
RemoteViews remoteViews = updateWidgetListView(context,
appWidgetIds[i]);
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
component=new ComponentName(context,WidgetListProvider.class);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listViewWidget);
appWidgetManager.updateAppWidget(component, remoteViews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
private RemoteViews updateWidgetListView(Context context, int appWidgetId) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_list_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(R.id.listViewWidget, svcIntent);
remoteViews.setEmptyView(R.id.listViewWidget, R.id.empty_view); …
Run Code Online (Sandbox Code Playgroud)