两个Activity间传递list<bean>对象

本文介绍如何在Android应用中两个Activity之间传递一个包含自定义Bean对象的List。Bean对象需实现Serializable接口,List类型应为ArrayList。通过Intent的putExtra方法进行数据传递,并在接收方使用getSerializableExtra获取。
两个activity直接需要传一个list<Bean> 有几点需要注意

1、Bean 要 implements Serializable或者继承Parcelable,我这里举得例子是前者, 
2、list本身要指定成ArrayList  如: ArrayList<bean> list = null;
3、传值的activity端  i.putExtra("aaa", Arraylist实例); 
4、接受段activity端  (ArrayList<Bean>) getIntent().getSerializableExtra("aaa"); 
package com.st.systemsettings.presenter; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.provider.Settings; import android.util.Log; import com.st.systemsettings.base.BasePresenter; import com.st.systemsettings.bean.UserManagerBean; import com.st.systemsettings.contract.UserManagerContract; import com.st.systemsettings.interfa.AppListChangeListener; import com.st.systemsettings.manager.ApplicationManager; import com.st.systemsettings.manager.SystemNotifyManager; import java.util.List; public class ApplicationPresenterImpl implements BasePresenter<UserManagerContract.View> { private static final String TAG = "ApplicationPresenterImpl"; private UserManagerContract.View mView; private Context mContext; private ApplicationManager mApp; @Override public void registerView(UserManagerContract.View view) { this.mView = view; this.mContext = view.getContext(); mApp = ApplicationManager.getInstance(); mApp.registerAppListChangeListener(appListChangeListener); } @Override public void unRegisterView() { mApp.registerAppListChangeListener(null); mView = null; mApp = null; } @Override public void init() { } public void clickUnInstall(String pkg) { if (mApp != null) { mApp.unInstallApp(pkg); } } public void clickClearCache(String pkg) { if (mApp != null) { mApp.clearAppCache(pkg); } } public void clickClearAllItem() { if (mApp != null) { mApp.clearAllCache(); } } public void updateSettingFocus(int type) { Log.i(TAG, "updateSettingFocus type : " + type); SystemNotifyManager.getInstance().onSettingsClearFocus(type); } public boolean getAppAllowVersion(String pkg) { int result = Settings.System.getInt(mContext.getContentResolver(), pkg, 0); Log.i(TAG, "getAppAllowVersion pkg : " + pkg + " result: " + result); return result == 1; } public void updateAppAllowUpdate(String pkg, boolean allow) { int value = allow ? 0 : 1; Log.i(TAG, "updateAppAllowUpdate pkg : " + pkg + " value: " + value); Settings.System.putInt(mContext.getContentResolver(), pkg, value); } public void unInstallForPkg(String pkg) { try { Intent intent = new Intent(Intent.ACTION_DELETE); intent.setData(Uri.parse("package:" + pkg)); mContext.startActivity(intent); } catch (Exception e) { e.printStackTrace(); } } AppListChangeListener appListChangeListener = new AppListChangeListener() { @Override public void onAppListChange(List<UserManagerBean> list) { Log.i(TAG, "onAppListChange: "); if (mView != null) { mView.onUpdate(list); } } @Override public void onAppDelete(List<UserManagerBean> list) { Log.i(TAG, "onAppDelete: "); if (mView != null) { mView.onDelete(list); } } }; } -------------------- package com.st.systemsettings.presenter; import com.st.systemsettings.presenter.ApplicationPresenterImpl; import com.st.systemsettings.contract.UserManagerContract; import com.st.systemsettings.bean.UserManagerBean; import com.st.systemsettings.manager.ApplicationManager; import com.st.systemsettings.manager.SystemNotifyManager; import android.content.Context; import android.provider.Settings; import android.content.Intent; import android.net.Uri; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.*; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import java.util.Arrays; import java.util.List; import static org.junit.Assert.assertNull; import static org.mockito.Mockito.*; @RunWith(PowerMockRunner.class) @PrepareForTest({Settings.System.class, Intent.class, Uri.class, SystemNotifyManager.class, ApplicationManager.class}) public class ApplicationPresenterImplTest { private ApplicationPresenterImpl presenter; @Mock private UserManagerContract.View mockView; @Mock private ApplicationManager mockAppManager; @Mock private Context mockContext; @Before public void setUp() { MockitoAnnotations.initMocks(this); presenter = new ApplicationPresenterImpl(); PowerMockito.mockStatic(Settings.System.class); // 用 PowerMockito 处理静态调用 PowerMockito.mockStatic(Uri.class); PowerMockito.mockStatic(Intent.class); PowerMockito.mockStatic(SystemNotifyManager.class); // 模拟 View 和 Context when(mockView.getContext()).thenReturn(mockContext); presenter.registerView(mockView); ApplicationManager mockAppSingleton = mock(ApplicationManager.class); PowerMockito.mockStatic(ApplicationManager.class); PowerMockito.when(ApplicationManager.getInstance()).thenReturn(mockAppSingleton); mockAppManager = mockAppSingleton; } @Test public void testRegisterView_Success() { verify(mockView, times(1)).getContext(); } @Test public void testUnRegisterView_Success() { presenter.unRegisterView(); } @Test public void testClickUnInstall_ValidPackage() { presenter.clickUnInstall("com.example.test"); } @Test public void testClickClearCache_ValidPackage() { presenter.clickClearCache("com.example.test"); } @Test public void testClickClearAllItem_Success() { presenter.clickClearAllItem(); } @Test public void testUpdateSettingFocus_Success() { SystemNotifyManager mockNotifyManager = mock(SystemNotifyManager.class); PowerMockito.when(SystemNotifyManager.getInstance()).thenReturn(mockNotifyManager); presenter.updateSettingFocus(1); } @Test public void testGetAppAllowVersion_Allow() { PowerMockito.when(Settings.System.getInt(mockContext.getContentResolver(), "com.example.test", 0)).thenReturn(1); boolean result = presenter.getAppAllowVersion("com.example.test"); } @Test public void testGetAppAllowVersion_Deny() { PowerMockito.when(Settings.System.getInt(mockContext.getContentResolver(), "com.example.test", 0)).thenReturn(0); boolean result = presenter.getAppAllowVersion("com.example.test"); } } 上面是我的被测试代码和单元测试代码,请帮我继续完善我的单元测试代码,目的是覆盖报告中的 public void updateAppAllowUpdate(String pkg, boolean allow) { int value = allow ? 0 : 1; Log.i(TAG, "updateAppAllowUpdate pkg : " + pkg + " value: " + value); Settings.System.putInt(mContext.getContentResolver(), pkg, value); } public void unInstallForPkg(String pkg) { try { Intent intent = new Intent(Intent.ACTION_DELETE); intent.setData(Uri.parse("package:" + pkg)); mContext.startActivity(intent); } catch (Exception e) { e.printStackTrace(); } } AppListChangeListener appListChangeListener = new AppListChangeListener() { @Override public void onAppListChange(List<UserManagerBean> list) { Log.i(TAG, "onAppListChange: "); if (mView != null) { mView.onUpdate(list); } } @Override public void onAppDelete(List<UserManagerBean> list) { Log.i(TAG, "onAppDelete: "); if (mView != null) { mView.onDelete(list); } } };
07-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值