ArrayList <string>无法转换为String []

时间:2017-11-08 14:11:52

标签: android gridview arraylist

我不明白我的gridview出了什么问题,我发现了这个错误:

  

错误:(73,60)错误:不兼容的类型:ArrayList不能   转换为String []

gridview代码:

       @Override
       public View onCreateView(LayoutInflater inflater, ViewGroup     
       container, Bundle savedInstanceState) {
       View v = inflater.inflate(R.layout.first_frag, container, false);

       ArrayList<String> web = new ArrayList<String>();
       ArrayList<Integer> imageId = new ArrayList<Integer>();



    web.add("test");
    web.add("test");
    web.add("test");
    web.add("test");

    for (int i = 0; i < web.size(); i++) {
        switch (i) {
            case 0:
                if (lvl == true) {
                    imageId.add(R.drawable.ic_luncher);
                } else {
                    imageId.add(R.drawable.ic_luncher);
                }
                break;
            case 1:
                if (lvl1 == true) {
                    //set image 1
                } else {
                    //set image 2
                }
                break;
            case 2:
                if (lvl2 == true) {
                    //set image 1
                } else {
                    //set image 2
                }
                break;
            default:
        }
    }

    CustomGrid adapter = new CustomGrid(getActivity(), web, imageId);
    grid=(GridView) v.findViewById(R.id.grid);
    grid.setAdapter(adapter);
    grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

适配器活动:

public class CustomGrid extends BaseAdapter{

    private Context mContext;
    private final String[] web;
    private final int[] Imageid;

    public CustomGrid(Context c, String[] web, int[] Imageid ) {
        mContext = c;
        this.Imageid = Imageid;
        this.web = web;
    }



    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return web.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View grid;
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {

            grid = new View(mContext);
            grid = inflater.inflate(R.layout.grid_single, null);
            TextView textView = (TextView) grid.findViewById(R.id.grid_text);
            ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
            textView.setText(web[position]);
            imageView.setImageResource(Imageid[position]);
        } else {
            grid = (View) convertView;
        }

        return grid;
    }
}

谢谢

2 个答案:

答案 0 :(得分:0)

数组变量无法存储arraylist值。所以你必须明确地将arraylist转换为数组。 试试这个

E.g String[] arr = list.toArray(new String[list.size()]);

In your case : CustomGrid adapter = new CustomGrid(getActivity(), web.toArray(new String[list.size()]), imageId.toArray(new int[imageId.size()]));

ArrayList类有一个方法toArray将列表转换为数组。

答案 1 :(得分:0)

在你的适配器初始化中,你传递的是Arraylist,而它需要一个字符串数组(String [])。 您需要将Array转换为String []

 CustomGrid adapter = new CustomGrid(getActivity(), web.toArray(new String[web.size()]), imageId);

或者更好的解决方案是将适配器字段(Web)类型更改为Array以避免不必要的转换。