androidandroid-activitypresenter

How to pass List data from Activity to presenter(MVP) in android


How to pass list data from activity to presenter.I have tried activityname.listname but its getting NULL . Could I know how to call?

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());
            }
        }
    }

}

Solution

  • pass your arraylist as the parameter chek out below code.

       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());
                    }
                }
        }
    
    }