片段中的自定义ListView - 构造函数未定义

时间:2016-02-20 15:24:37

标签: android listview

我正在学习PageSlider并希望在我的幻灯片段中实现ListViews,我的代码如下,但在CustomAdapter上出错:

The constructor FragmentA.CustomAdapter(FragmentA, String[], String[], int[]) is undefined.

我可以添加TextView,但ListView会返回错误,如上所述。

FragmentA:

public class FragmentA extends Fragment {

    String [] arrayName;
    String [] arrayDescription;
    int[] images = {R.drawable.img1, R.drawable.img2, R.drawable.img3};

    ListView lv;

    public FragmentA() {
        //
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) {

            View view = inflater.inflate(R.layout.fragment_a, container, false);

            ListView lv =(ListView)view.findViewById(R.id.listView);

            CustomeAdapter adapter = new CustomeAdapter (this, arrayName, arrayDescription, images);    //Error: The constructor FragmentA.CustomAdapter(FragmentA, String[], String[], int[]) is undefined

            lv.setAdapter(adapter);

            return view;

    }

CustomAdapter:

public class CustomeAdapter extends ArrayAdapter<String> 
    {
    Context context;
    String[] arraynames;
    String[] arrayDescription;
    int[] images;
    CustomeAdapter(Context c, String [] name, String [] description, int img[])
    {
        super(c, R.layout.activity_lv_single, R.id.textname, name);
        this.context=c;
        this.arraynames=name;
        this.arrayDescription=description;
        this.images=img;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View rowView = convertView;
        if (rowView == null)
        {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.activity_lv_single, parent, false)
        }

        TextView tvName = (TextView) rowView.findViewById(R.id.textName);
        TextView tvDescription = (TextView) rowView.findViewById(R.id.textDesc);
        ImageView ivImage = (ImageView) rowView.findViewById(R.id.img);

        tvName.setText(arrayNames[position]);
        tvDescription.setText(arrayDescription[position]);
        ivImage.(images[positionl

        return rowView;

    }   
}

1 个答案:

答案 0 :(得分:1)

由于Fragment类不会扩展Context,因此您无法将Fragment传递给需要Context的适配器。 而不是this,只需使用getActivity(),因为Activity扩展了Context

CustomeAdapter adapter = new CustomeAdapter (getActivity(), arrayName, arrayDescription, images);
相关问题