在Android中将arraylist从活动传递到java类
我在MainActivity中有一个arraylist,我有一个java类,它不是活动。如何将此arraylist传递给此类?我的arraylist:在Android中将arraylist从活动传递到java类,java,android,android-activity,arraylist,Java,Android,Android Activity,Arraylist,我在MainActivity中有一个arraylist,我有一个java类,它不是活动。如何将此arraylist传递给此类?我的arraylist: public List<String> types = new ArrayList<String>(); public List types=new ArrayList(); 我填写了MainActivity。然后,我必须在java类中使用ExpandableListAdapter。这里是java类的一部分,我必须对
public List<String> types = new ArrayList<String>();
public List types=new ArrayList();
我填写了MainActivity。然后,我必须在java类中使用ExpandableListAdapter。这里是java类的一部分,我必须对它进行更改,我试图创建新的activity对象,但没有成功
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;
}
公共类ExpandableListAdapter扩展了BaseExpandableListAdapter{
公共视图getChildView(int groupPosition,final int childPosition,
布尔值isLastChild、视图转换视图、视图组父级){
MainActivity m=新的MainActivity();
整数[]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}
整数[]mIcons_2=新整数[m.types.size()];
对于(int i=0;i使用构造函数传递它,如下所示:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private final List<String> typesList;
public ExpandableListAdapter(List<String> typesList) {
this.typesList = typesList;
}
...
...
}
公共类ExpandableListAdapter扩展了BaseExpandableListAdapter{
私人最终列表类型列表;
公共可扩展列表适配器(列表类型列表){
this.typesList=typesList;
}
...
...
}
或者,也可以使用setter,如:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private List<String> typesList;
public void setTypesList(List<String> typesList) {
this.typesList = typesList;
}
...
...
}
公共类ExpandableListAdapter扩展了BaseExpandableListAdapter{
私有列表类型列表;
公共无效集合类型列表(列表类型列表){
this.typesList=typesList;
}
...
...
}
然后,您可以随时通过调用适配器.setTypesList()
在适配器类中声明一个列表来设置类型
// Declaration :
public List<String> list = new ArrayList<String>();
我建议您调用非活动类的构造函数,并将数组列表作为其参数
让我们假设我的非活动类是B类
B obj=new B(Activity context,types);
别忘了在B类中声明同一个构造函数创建一个具有所需参数的构造函数,并在为该类创建对象时传递值。
new ExpandableListAdapter(types);
B obj=new B(Activity context,types);