[英]Passing an arraylist from an activity to a java class in Android
I have an arraylist in MainActivity and I have a java class which is not an acitivity . 我在MainActivity中有一个arraylist,我有一个java类,它不是一个活动 。 How can i pass this arraylist to this class ?
我如何将这个arraylist传递给这个班级? My arraylist:
我的arraylist:
public List<String> types = new ArrayList<String>();
and i filled in MainAcitivity. 我填写了MainAcitivity。 then i have to use in a java class which is name ExpandableListAdapter.
然后我必须在一个名为ExpandableListAdapter的java类中使用。 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.
这是java类的一部分,我必须在其中进行更改,我尝试创建新的活动对象,但它不起作用。
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?
这个ExpandableListAdapter类就是这个,但我的问题是如何将MainList中的ArrayList类型传递给ExpandableListAdapter java类? 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.
我知道使用intent时将一个活动的值传递给其他活动但我不知道将一个活动传递给java类。
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()
然后你可以通过调用
adapter.setTypesList()
随时设置types
Declare a List in your adapter class 在适配器类中声明一个List
// Declaration :
public List<String> list = new ArrayList<String>();
Constructor will help you to pass the arraylist as params. 构造函数将帮助您将arraylist作为params传递。
ExpandableListAdapter(List<String> types){
this.list = types
}
You can pass the arraylist as follows : 您可以按如下方式传递arraylist:
new ExpandableListAdapter(types);
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 我建议你调用非活动类的构造函数,将数组List作为参数让我假设我的非活动类是B类
B obj=new B(Activity context,types);
And don't forget to declare the same constructor in B class 并且不要忘记在B类中声明相同的构造函数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.