如何将 Activity 中的列表数据传递给 android 中的演示者(MVP)
How to pass List data from Activity to presenter(MVP) in android
如何将列表数据从 activity 传递到 presenter.I 已尝试 activityname.listname 但它得到 NULL 。我可以知道怎么打电话吗?
MainActivity.java
public List<AppGrid> arraylist = new ArrayList<AppGrid>();
Bundle bundle = getIntent().getExtras();
if(bundle != null){
arraylist = bundle.getParcelableArrayList("mylist");
Toast.makeText(MainActivity.this,"bundle->+"+arraylist,Toast.LENGTH_LONG).show();
}
AddFavariteApps();
}
private void AddFavariteApps() {
HeaderItem HeaderInstalledApps = new HeaderItem(0, "Favourite");
GridItemPresenter1 gridFavouriteApps = new GridItemPresenter1();
ArrayObjectAdapter FavouriteAppsAdapter = new ArrayObjectAdapter(gridFavouriteApps);
FavouriteAppsAdapter.add(getString(R.string.installed_apps));
FavouriteAppsAdapter.add("Add Apps");
rowsAdapter.add(new ListRow(HeaderInstalledApps, FavouriteAppsAdapter));
}
GridItemPresenter1.java
public class GridItemPresenter1 extends Presenter {
private static Context mContext;
private static int CARD_WIDTH = 313;
private static int CARD_HEIGHT = 176;
String str_App_name;
Drawable str_App_Icon;
MainActivity mMainActivity;
@Override
public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
str_App_name = Constants.shared().getAppName();
if (((String) item).equalsIgnoreCase("Add Apps")) {
((ViewHolder) viewHolder).mCardView.setContentText("Add Apps");
((ViewHolder) viewHolder).mCardView.setMainImageDimensions(CARD_WIDTH, CARD_HEIGHT);
((ViewHolder) viewHolder).updateCardViewImage1(R.drawable.item_img_add);
}else {
if(mMainActivity.arraylist != null) {
for (int i = 0; i < mMainActivity.arraylist.size(); i++) {
viewHolder).mCardView.setContentText(mMainActivity.arraylist.get(i).getName());
}
}
}
}
假设您的 List 是一个字符串列表,使数据成为 ArrayList
并使用
intent.putStringArrayListExtra("data", data)
这是您需要的代码框架:
声明列表
private List<String> test;
在适当的地方初始化列表
test = new ArrayList<String>();
并酌情添加数据进行测试。
传递给意图如下:
Intent intent = getIntent();
intent.putStringArrayListExtra("test", (ArrayList<String>) test);
检索数据如下:
ArrayList<String> test = getIntent().getStringArrayListExtra("test");
希望对您有所帮助。
将您的数组列表作为参数传递到代码下方。
public List<AppGrid> arraylist = new ArrayList<AppGrid>();
Bundle bundle = getIntent().getExtras();
if(bundle != null){
arraylist = bundle.getParcelableArrayList("mylist");
Toast.makeText(MainActivity.this,"bundle->+"+arraylist,Toast.LENGTH_LONG).show();
}
AddFavariteApps();
}
private void AddFavariteApps() {
HeaderItem HeaderInstalledApps = new HeaderItem(0, "Favourite");
GridItemPresenter1 gridFavouriteApps = new GridItemPresenter1(arraylist); // pass as your arraylist as parameter
ArrayObjectAdapter FavouriteAppsAdapter = new ArrayObjectAdapter(gridFavouriteApps);
FavouriteAppsAdapter.add(getString(R.string.installed_apps));
FavouriteAppsAdapter.add("Add Apps");
rowsAdapter.add(new ListRow(HeaderInstalledApps, FavouriteAppsAdapter));
}
============================================= =============
public class GridItemPresenter1 extends Presenter {
private static Context mContext;
private static int CARD_WIDTH = 313;
private static int CARD_HEIGHT = 176;
String str_App_name;
Drawable str_App_Icon;
MainActivity mMainActivity;
public GridItemPresenter1 (List<AppGrid> arraylist) {
this.arraylist = arraylist;
}
@Override
public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
str_App_name = Constants.shared().getAppName();
if (((String) item).equalsIgnoreCase("Add Apps")) {
((ViewHolder) viewHolder).mCardView.setContentText("Add Apps");
((ViewHolder) viewHolder).mCardView.setMainImageDimensions(CARD_WIDTH, CARD_HEIGHT);
((ViewHolder) viewHolder).updateCardViewImage1(R.drawable.item_img_add);
}else {
if(arraylist != null) {
for (int i = 0; i <arraylist.size(); i++) {
viewHolder).mCardView.setContentText(arraylist.get(i).getName());
}
}
}
}
如何将列表数据从 activity 传递到 presenter.I 已尝试 activityname.listname 但它得到 NULL 。我可以知道怎么打电话吗?
MainActivity.java
public List<AppGrid> arraylist = new ArrayList<AppGrid>();
Bundle bundle = getIntent().getExtras();
if(bundle != null){
arraylist = bundle.getParcelableArrayList("mylist");
Toast.makeText(MainActivity.this,"bundle->+"+arraylist,Toast.LENGTH_LONG).show();
}
AddFavariteApps();
}
private void AddFavariteApps() {
HeaderItem HeaderInstalledApps = new HeaderItem(0, "Favourite");
GridItemPresenter1 gridFavouriteApps = new GridItemPresenter1();
ArrayObjectAdapter FavouriteAppsAdapter = new ArrayObjectAdapter(gridFavouriteApps);
FavouriteAppsAdapter.add(getString(R.string.installed_apps));
FavouriteAppsAdapter.add("Add Apps");
rowsAdapter.add(new ListRow(HeaderInstalledApps, FavouriteAppsAdapter));
}
GridItemPresenter1.java
public class GridItemPresenter1 extends Presenter {
private static Context mContext;
private static int CARD_WIDTH = 313;
private static int CARD_HEIGHT = 176;
String str_App_name;
Drawable str_App_Icon;
MainActivity mMainActivity;
@Override
public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
str_App_name = Constants.shared().getAppName();
if (((String) item).equalsIgnoreCase("Add Apps")) {
((ViewHolder) viewHolder).mCardView.setContentText("Add Apps");
((ViewHolder) viewHolder).mCardView.setMainImageDimensions(CARD_WIDTH, CARD_HEIGHT);
((ViewHolder) viewHolder).updateCardViewImage1(R.drawable.item_img_add);
}else {
if(mMainActivity.arraylist != null) {
for (int i = 0; i < mMainActivity.arraylist.size(); i++) {
viewHolder).mCardView.setContentText(mMainActivity.arraylist.get(i).getName());
}
}
}
}
假设您的 List 是一个字符串列表,使数据成为 ArrayList 并使用
intent.putStringArrayListExtra("data", data)
这是您需要的代码框架:
声明列表
private List<String> test;
在适当的地方初始化列表
test = new ArrayList<String>();
并酌情添加数据进行测试。 传递给意图如下:
Intent intent = getIntent();
intent.putStringArrayListExtra("test", (ArrayList<String>) test);
检索数据如下:
ArrayList<String> test = getIntent().getStringArrayListExtra("test");
希望对您有所帮助。
将您的数组列表作为参数传递到代码下方。
public List<AppGrid> arraylist = new ArrayList<AppGrid>();
Bundle bundle = getIntent().getExtras();
if(bundle != null){
arraylist = bundle.getParcelableArrayList("mylist");
Toast.makeText(MainActivity.this,"bundle->+"+arraylist,Toast.LENGTH_LONG).show();
}
AddFavariteApps();
}
private void AddFavariteApps() {
HeaderItem HeaderInstalledApps = new HeaderItem(0, "Favourite");
GridItemPresenter1 gridFavouriteApps = new GridItemPresenter1(arraylist); // pass as your arraylist as parameter
ArrayObjectAdapter FavouriteAppsAdapter = new ArrayObjectAdapter(gridFavouriteApps);
FavouriteAppsAdapter.add(getString(R.string.installed_apps));
FavouriteAppsAdapter.add("Add Apps");
rowsAdapter.add(new ListRow(HeaderInstalledApps, FavouriteAppsAdapter));
}
============================================= =============
public class GridItemPresenter1 extends Presenter {
private static Context mContext;
private static int CARD_WIDTH = 313;
private static int CARD_HEIGHT = 176;
String str_App_name;
Drawable str_App_Icon;
MainActivity mMainActivity;
public GridItemPresenter1 (List<AppGrid> arraylist) {
this.arraylist = arraylist;
}
@Override
public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
str_App_name = Constants.shared().getAppName();
if (((String) item).equalsIgnoreCase("Add Apps")) {
((ViewHolder) viewHolder).mCardView.setContentText("Add Apps");
((ViewHolder) viewHolder).mCardView.setMainImageDimensions(CARD_WIDTH, CARD_HEIGHT);
((ViewHolder) viewHolder).updateCardViewImage1(R.drawable.item_img_add);
}else {
if(arraylist != null) {
for (int i = 0; i <arraylist.size(); i++) {
viewHolder).mCardView.setContentText(arraylist.get(i).getName());
}
}
}
}