1

我在 MainActivity 有一个数组列表,我有一个不是acitivity的 java 类。我怎样才能将这个数组列表传递给这个类?我的数组列表:

public List<String>  types = new ArrayList<String>();

我填写了 MainAcitivity。然后我必须在名为 ExpandableListAdapter 的 java 类中使用。这是我必须对其进行更改的 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;
}

我创建了一个带有可扩展列表视图的导航抽屉,我也尝试在每个项目上放置不同的图标。这个 ExpandableListAdapter 类就是这样,但我的问题是如何将类型 ArrayList 从 MainActivity 传递给 ExpandableListAdapter java 类?我知道在将一个活动的值传递给另一个活动时使用意图有效,但我不知道将一个活动传递给 java 类。

4

3 回答 3

2

使用构造函数传递它,如下所示:

public class ExpandableListAdapter extends BaseExpandableListAdapter {
    private final List<String> typesList;

    public ExpandableListAdapter(List<String> typesList) {
        this.typesList = typesList;
    }
    ...
    ...
}

或者,您也可以使用像这样的设置器:

public class ExpandableListAdapter extends BaseExpandableListAdapter {
    private List<String> typesList;

    public void setTypesList(List<String> typesList) {
        this.typesList = typesList;
    }
    ...
    ...
}

然后您可以types通过调用随时设置adapter.setTypesList()

于 2013-11-06T09:57:59.417 回答
0

在适配器类中声明一个列表

// Declaration :

 public List<String> list = new ArrayList<String>();

构造函数将帮助您将数组列表作为参数传递。

 ExpandableListAdapter(List<String>  types){
 this.list = types
 }

您可以按如下方式传递数组列表:

  new ExpandableListAdapter(types);
于 2013-11-06T09:58:38.327 回答
0

我建议您调用具有数组 List 作为其参数的非活动类的构造函数假设我的非活动类是 B 类

B obj=new B(Activity context,types);

并且不要忘记在 B 类中声明相同的构造函数

于 2013-11-06T10:00:43.313 回答