-2

I have two activities (Main & Sub) both contain list view to display images. I want to reuse the sub activity for displaying the images according to the cell click in the main activity (some thing like Restaurant Menu). So I have declared an array of integer arrays (Images in drawable folder) that I want to pass to sub activity and the subadapter

I tried below code, but I ended with the Int and Integer conflict.

main activity

public class AppsMenu extends AppCompatActivity {

    ListView appsmainlist;
    Integer[] images={R.drawable.res,R.drawable.clo,R.drawable.beu,R.drawable.col,R.drawable.tor,R.drawable.hot};
    Integer[][] subarray = {{R.drawable.res1,R.drawable.res2,R.drawable.res3,R.drawable.res4},{R.drawable.clo1,R.drawable.clo2,R.drawable.clo3,R.drawable.clo4}};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apps_menu);
        appsmainlist = (ListView) findViewById(R.id.appsmainlist);
        appsmenuadapter appsmenuadapter = new appsmenuadapter(this,images);
        appsmainlist.setAdapter(appsmenuadapter);
        appsmainlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                    Intent intent = new Intent(AppsMenu.this,appsview.class);
                    intent.putExtra("subarray",subarray[i]);
                    startActivity(intent);


            }
        });
    }
}

sub Activity

ListView appsviewlist;
Integer [] appsviewarray;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_appsview);
    appsviewlist = (ListView) findViewById(R.id.appsviewlist);

// Error goes here
        appsviewarray = getIntent().getIntArrayExtra("subarray");
        appsviewadapter appsviewadapter = new appsviewadapter(this,appsviewarray);
    }

Adapter :

public class subadapter extends ArrayAdapter<Integer> {
...
}

Any help will be much appreciated.

K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33
Mohammed Riyadh
  • 883
  • 3
  • 11
  • 34
  • change to primitive int instead of Integer – Nayan Srivastava Nov 21 '16 at 15:12
  • if i did so , the adapter will not except it !! adapter – Mohammed Riyadh Nov 21 '16 at 16:29
  • create another array and then simply pass values if so, however you can also use adapter constructor for this instead of using generics – Nayan Srivastava Nov 21 '16 at 16:38
  • If this is the only functionality of your app, you can have the applications class hold the data, and have the activity retrieve it from the Application class. Another quick and dirty way is to use a static variable in the parent activity, and depending on the design of your app, it may be a safe choice. – lionscribe Nov 21 '16 at 17:02

2 Answers2

2

Do Arrays keep resource id? Did you think about to use Resource Array?

Something like this:

<integer-array name="resoruces"> <item>@drawable/amex</item> <item>@drawable/mastercard</item> <item>@drawable/mastercard_white</item> <item>@drawable/visa</item> </integer-array>

In this way you can get them using context.

hooloovoo
  • 825
  • 9
  • 12
0

Create Bundle and save is as parcelable array

Alex Shutov
  • 3,217
  • 2
  • 13
  • 11