Reputation: 23
I have an arraylist in MainActivity and I have a java class which is not an acitivity. How can i pass this arraylist to this class ? My arraylist:
public List<String> types = new ArrayList<String>();
and i filled in MainAcitivity. then i have to use in a java class which is name ExpandableListAdapter. Here is part of the java class which i have to do change in it and i tried to create new activity object but it didn't work.
public class ExpandableListAdapter extends BaseExpandableListAdapter {
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
MainActivity m=new MainActivity();
Integer[] mIcons = {R.drawable.car_1,R.drawable.car_2,R.drawable.car_3,R.drawable.car_4,R.drawable.car_5,R.drawable.car_6,R.drawable.car_7,R.drawable.car_8,R.drawable.car_9,R.drawable.car_10}
Integer[] mIcons_2 = new Integer [m.types.size()];
for(int i=0; i<m.types.size(); i++){
mIcons_2[i]= mIcons[ Integer.parseInt(m.types.get(i))];
}
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.drawer_list_item, null);
}
ImageView iconn =(ImageView)convertView.findViewById(R.id.thumbNail);
iconn.setImageResource(mIcons_2[childPosition]);
TextView txtListChild = (TextView) convertView
.findViewById(R.id.textView);
txtListChild.setTextColor(Color.BLUE);
txtListChild.setText(childText);
return convertView;
}
I created a navigation drawer with an expandable listview also i try to put different icons on every item. This ExpandableListAdapter class is about that but my problem is about how can i pass the types ArrayList from the MainActivity to the ExpandableListAdapter java class? I know that using intent works when pass the values one activity to other activity but i didn't know about passing one activity to a java class.
Upvotes: 1
Views: 1445
Reputation: 451
I will suggest you to call the constructor of the non activity class having an array List as its parameter Lets suppose my Non-Activity class be Class B
B obj=new B(Activity context,types);
And don't forget to declare the same constructor in B class
Upvotes: 0
Reputation: 3952
Declare a List in your adapter class
// Declaration :
public List<String> list = new ArrayList<String>();
Constructor will help you to pass the arraylist as params.
ExpandableListAdapter(List<String> types){
this.list = types
}
You can pass the arraylist as follows :
new ExpandableListAdapter(types);
Upvotes: 0
Reputation: 7708
Pass it using the constructor as follows:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private final List<String> typesList;
public ExpandableListAdapter(List<String> typesList) {
this.typesList = typesList;
}
...
...
}
Alternatively, you can also use a setter like:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private List<String> typesList;
public void setTypesList(List<String> typesList) {
this.typesList = typesList;
}
...
...
}
Then you can set the types
anytime by calling adapter.setTypesList()
Upvotes: 2