Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

how do i handle using one arrayAdapter for 4 different listviews. I have a 4 different ArrayLists that I am passing into the arrayAdapter. Instead of copy and paste to make 4 ArrayAdapter classes that would be identical except for a few lines specific to the arrayList that was entered into the adapter. How about using the same arrayAdapter class for 4 separate arrayLists?

the problem is i need the ArrayAdapter to know which of the 4 arrayLists was entered because i have a switch statement inside of the Adapter class method.

in this case should i add one extra parameter variable to the constructor of this ArrayAdapter class, as identifier for which of the four arraylists was entered? like an int?

because my adapter class extends ArrayAdapter class how would i do this? would i just add one more variable position in the constructor parameter and then leave the super call the same as before like this?

super(context, textViewResourceId, objects);

how would you handle this problem?

  // array adapter where you enter the array
        SelectorListAdapter adapter = new SelectorListAdapter(activity, R.layout.row_layout, smallTank2listInfo1Array);
        SelectorListAdapter adapter2 = new SelectorListAdapter(activity, R.layout.row_layout, smallTank2listInfo2Array);
        SelectorListAdapter adapter3 = new SelectorListAdapter(activity, R.layout.row_layout, smallTank2listInfo3Array);
        SelectorListAdapter adapter4 = new SelectorListAdapter(activity, R.layout.row_layout, smallTank2listInfo4Array);

             // set array adapter to listview
            listViewOne.setAdapter(adapter);
            listViewTwo.setAdapter(adapter2);
            listViewThree.setAdapter(adapter3);
            listViewFour.setAdapter(adapter4);



  public class SelectorListAdapter extends ArrayAdapter<CheckBoxListInfo>{
        ArrayList<CheckBoxListInfo> objects;
        Context context;
        int textViewResourceId;
        private String tempLabel;
        private boolean isChecked;

        public SelectorListAdapter(Context context, int textViewResourceId,
            ArrayList<CheckBoxListInfo> objects) {
            super(context, textViewResourceId, objects);
            this.context = context;
            this.textViewResourceId = textViewResourceId;
            this.objects = objects;

        }
share|improve this question
    
How about using the same arrayAdapter class for 4 separate arrayLists? - don't do this. the problem is i need the ArrayAdapter to know which of the 4 arrayLists was entered - then pass an additional value to the adapter to identify the lists, an integer, an object, enum etc etc. – Luksprog Jul 11 '13 at 12:01

1 Answer 1

up vote 0 down vote accepted

Actually you have answered everything yourself

How about using the same arrayAdapter class for 4 separate arrayLists? --yes you can use.

To identify different arrayAdapters you can pass a variable to constructor which is stored in a local variable. Depending on the value of this variable you can decide what you want to do.

while calling the constuctor of super class leave this variable and call super()

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.